Numbers1

VSBrown 0 Tallied Votes 216 Views Share

A program that inputs three integers from the keyboard and prints the sum, average, product, smallest and largest of these numbers.

/******************************************************

** Name: 

** Filename: numbers1.cpp

** Project #: Deitel & Deitel 1.26

** Project Description: Write a program that inputs three integers 
   from the keyboard and prints the sum, average, product, smallest 
   and largest of these numbers.

** Output:
	Sum of three integers
	Average of three intergers
	Product of three integers
	Smallest of three integers
	Largest of three integers


** Input:
	Three integers from the keyboard

** Algorithm:
	Explain function to be performed
	Direct user to input three integers
	Take the three integers that are input and calculate
	 Sum
	 Average
	 Product
	 Smallest integer
	 Largest integer
	Print sum
	Print average
	Print product
	Print smallest integer
	Print largest integer

******************************************************/

// Include files
#include <iostream>  // used for cin, cout
#include <conio.h>
using namespace std;

// Global Type Declarations

// Function Prototypes
void instruct (void);
void pause ();

//Global Variables - should not be used without good reason.

int main ()
{
	 // Declaration section
	  double integer1, integer2, integer3, sum, average, product; 

	 // Executable section
	 instruct ();
   cout << "Input three different integers: ";  
   cin >> integer1 >> integer2 >> integer3;
   sum = integer1 + integer2 + integer3;
   average = (integer1 + integer2 + integer3) / 3;
   product = integer1 * integer2 * integer3;
   cout << "Sum is " << sum 
	   << "\nAverage is " << average 
	   << "\nProduct is " << product << endl;
   if ( integer1 < integer2 )
   if ( integer1 < integer3 )
	   cout << "Smallest is " << integer1 << endl;
   if ( integer1 == integer2 )
   if ( integer1 < integer3 )
	   cout << "Smallest is " << integer1 << endl;
   if ( integer2 < integer1 )
   if ( integer2 < integer3 )
	   cout << "Smallest is " << integer2 << endl;
   if ( integer2 == integer3 )
   if ( integer2 < integer1 )
	   cout << "Smallest is " << integer2 << endl;
   if ( integer3 < integer1 )
   if ( integer3 < integer2 )
	   cout << "Smallest is " << integer3 << endl;
   if ( integer3 == integer1 )
   if ( integer3 < integer2 )
	   cout << "Smallest is " << integer3 << endl;
   if ( integer1 > integer2 )
   if ( integer1 > integer3 )
	   cout << "Largest is " << integer1 << endl;
   if ( integer1 == integer2 )
   if ( integer1 > integer3 )
	   cout << "Largest is " << integer1 << endl;
   if ( integer2 > integer1 )
   if ( integer2 > integer3 )
	   cout << "Largest is " << integer2 << endl;
   if ( integer2 == integer3 )
   if ( integer2 > integer1 )
	   cout << "Largest is " << integer2 << endl;
   if ( integer3 > integer1 )
   if ( integer3 > integer2 )
	   cout << "Largest is " << integer3 << endl;
   if ( integer3 == integer1 )
   if ( integer3 > integer2 )
	   cout << "Largest is " << integer3 << endl;
   if ( integer1 == integer2 )
   if ( integer1 == integer3 )
	   cout << "All integers are equal\n";

    pause ();
	return 0;
	 
}

void instruct (void)
{
	  // Declaration section

	  // Executable section
 cout << "Choose three integers to input below. The program will\n"
	  << "then print the sum, the average, the smallest, and the\n" 
	  << "largest of the three integers that have been input.\n "<< endl;
}

void pause ()
{
    // Declaration section

    // Executable section
    cout << "\nPress any key to continue...";
    getch();
    cout << "\r";
    cout << "                            ";
    cout << "\r";
}



/*
Program Output

Choose three integers to input below. The program will
then print the sum, the average, the smallest, and the
largest of the three integers that have been input.

Input three different integers: 12 19 25
Sum is 56
Average is 18.6667
Product is 5700
Smallest is 12
Largest is 25

Press any key to continue...

*/

Dani AI

Generated

Nice start, . The program implements the exercise, but it can be made more robust, portable, and easier to read. Key issues to address: the many nested ifs for smallest/largest are verbose and fragile, conio.h/getch() is non-portable, and the product can overflow typical int ranges. The average calculation should explicitly force floating-point division when inputs are integers.

A simple, safe approach is to read integers, compute sum/product with appropriate types, and determine min/max with a small sequence of comparisons (or std::min/std::max). Example pattern (keeps logic compact and easy to audit):

int a, b, c;
if (!(cin >> a >> b >> c)) { /* handle bad input */ }

long long product = 1LL * a * b * c;        // wider type to reduce overflow risk
double average = (a + b + c) / 3.0;         // ensure floating division

int smallest = a;
if (b < smallest) smallest = b;
if (c < smallest) smallest = c;

int largest = a;
if (b > largest) largest = b;
if (c > largest) largest = c;

Notes and suggestions:

  • For readability use std::min({a,b,c}) / std::max({a,b,c}) (need <algorithm>, C++11+), or the iterative approach above.
  • Avoid using namespace std; at global scope in real projects.
  • Replace conio.h/getch() with a portable alternative (or just end the program).
  • If inputs may be large, consider checking for overflow before multiplying, or use 128-bit where available.
  • Add input validation and try edge cases: equal values, negatives, zeros, and very large values.

These changes keep the program correct, portable, and easier to maintain while handling common edge cases.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.