Numbers

VSBrown 0 Tallied Votes 485 Views Share

A program that asks the user to enter two numbers, obtains the two numbers from the user and prints the sum,product,difference, and quotient of the two numbers.

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

** Name: 

** Filename: numbers.cpp

** Project #: Deitel & Deitel 1.23

** Project Description: Write a program that asks the user to enter 
   two numbers, obtains the two numbers from the user and prints 
   the sum,product,difference, and quotient of the two numbers.

** Output:
	Sum of two numbers
	Product of two numbers
	Quotient of two numbers
	Difference of two numbers


** Input:
	Two whole numbers of the users choice

** Algorithm:
	Explain function to be performed
	Direct user to choose two whole numbers
	Take the two numbers that are input and calculate
	 Sum
	 Product
	 Quotient
	 Difference between two numbers
	Print sum
	Print product
	Print quotient
	Print difference


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

// 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 num1, num2, sum, product, quotient; 

	 // Executable section
	 instruct ();
   cout << "Enter first number: ";  
   cin >> num1;                  
   cout << "Enter second number: "; 
   cin >> num2;                  
   sum = num1 + num2;
   product = num1 * num2;
   quotient = num1 / num2;
   cout << "\nThe sum of " << num1 << " and " << num2 << " is " 
	   << sum 
	   << "\nThe product of " << num1 << " multiplied by " << num2 << " is " 
	   << product 
	   << "\nThe quotient of " << num1 << " divided by " << num2 << " is " 
	   << quotient << endl;
   cout << "\nThe difference of the two numbers is as follows: " 
	   << endl;
   if ( num1 == num2 )
	   cout << num1 << " is equal to " << num2 << endl;
   if ( num1 != num2 ) 
	   cout << num1 << " is not equal to " << num2 << endl;
   if ( num1 < num2 )
	   cout << num1 << " is less than " << num2 << endl;
   if ( num1 > num2 )
	   cout << num1 << " is greater than " << num2 << endl;
   if ( num1 <= num2 )
	   cout << num1 << " is less than or equal to " 
	   << num2 << endl;
   if ( num1 >= num2 )
	   cout << num1 << " is greater than or equal to "
	   << num2 << endl;
    pause ();
	return 0;
	 
}

void instruct (void)
{
	  // Declaration section

	  // Executable section
 cout << "Choose two whole numbers to input below. The program will\n"
	  << "then calculate the sum, the product, the difference, and the\n" 
	  << "quotient of the two numbers 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 two whole numbers to input below. The program will
then calculate the sum, the product, the difference, and the
quotient of the two numbers that have been input.

Enter first number: 6
Enter second number: 4

The sum of 6 and 4 is 10
The product of 6 multiplied by 4 is 24
The quotient of 6 divided by 4 is 1.5

The difference of the two numbers is as follows:
6 is not equal to 4
6 is greater than 4
6 is greater than or equal to 4

Press any key to continue...


*/

Dani AI

Generated

's example implements the basic operations correctly, but a few practical changes make the program more robust and portable: remove the nonstandard <conio.h>/getch usage, validate input so non-numeric entries don't break the run, guard against division by zero, and avoid direct equality checks on floating‑point values (use an epsilon). Also prefer explicit std:: qualifiers instead of a global using namespace std; in real code.

Minimal, portable example that demonstrates those points:

#include <iostream>
#include <iomanip>
#include <cmath>
#include <limits>

int main() {
    long double x = 0.0L, y = 0.0L;
    std::cout << "Enter two numbers: ";
    while (!(std::cin >> x >> y)) {
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        std::cout << "Invalid input - try again: ";
    }

    std::cout << std::fixed << std::setprecision(6);
    std::cout << "Sum: " << (x + y) << '\n'
              << "Product: " << (x * y) << '\n';

    if (std::abs(y) < 1e-18L)
        std::cout << "Quotient: undefined (division by zero)\n";
    else
        std::cout << "Quotient: " << (x / y) << '\n';

    std::cout << "Absolute difference: " << std::abs(x - y) << '\n';

    long double eps = 1e-12L;
    if (std::abs(x - y) <= eps)
        std::cout << "Numbers are equal within epsilon\n";
    else if (x < y)
        std::cout << "First is less than second\n";
    else
        std::cout << "First is greater than second\n";

    return 0;
}

Notes: use integer types if the assignment really requires "whole numbers" (and remember integer division truncates - cast to double when you want a fractional quotient). Choose an epsilon appropriate to the magnitude of values you expect. Test edge cases (zero divisor, very large/small magnitudes, invalid input) and keep I/O and computation separated if the program grows beyond a simple exercise.

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.