16. 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.

C++ How to Program (8th Edition)

Chapter 2

Introduction to C++ Programming

Exercise

16. 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.

#include <iostream> // allows program to perform input and output
// function main begins program execution
using std::cout; // program uses cout
using std::cin; // program uses cin
using std::endl; // program uses endl
int main()
{
 int number1; // first integer
 int number2; // second integer
 int sum; //holds sum
 int product; //holds product
 int difference; //holds difference
 float quotient; //holds quotient
 cout << "Enter two integers: "; // prompt user for data
 cin >> number1 >> number2; // read two integers from user
 
 sum=number1+number2;
 cout<<"Sum is: "<<sum<<endl;
 
 product=number1*number2;
 cout<<"Product is: "<<product<<endl;
 
 difference=number1-number2;
 cout<<"Dofference is: "<<difference<<endl;
 
 if(number2!=0){
  quotient=(float)number1/number2;
  cout<<"Quotient is: "<<quotient<<endl;
 }
} // end function main

Let me know in the comment section if you have any question.

Similar Posts:

Chapter 2

  1. Program that prints the numbers 1 to 4 on the same line with each pair of adjacent numbers separated by one space.
  2. Program that asks the user to enter two integers, obtains the numbers from the user, then prints the larger number followed by the words "is larger." If the numbers are equal, print the message "These numbers are equal."
  3. Program that inputs three integers from the keyboard and prints the sum, average, product, smallest and largest of these numbers.
  4. Program that reads in the radius of a circle as an integer and prints the circle’s diameter, circumference and area.Use the constant value 3.14159 for π. Do all calculations in output statements.
  5. Program that prints a box, an oval, an arrow and a diamond.
  6. Program that reads in five integers and determines and prints the largest and the smallest integers in the group.
  7. Program that reads an integer and determines and prints whether it’s odd or even.
  8. Program that reads in two integers and determines and prints if the first is a multiple of the second.
  9. Display checkerboard pattern with eight output statements, then display the same pattern using as few statements as possible.
  10. Program that prints the integer equivalent of a character typed at the keyboard.
  11. Program that inputs a five-digit integer, separates the integer into its digits and prints them separated by three spaces each.
  12. Program that calculates the squares and cubes of the integers from 0 to 10.

 Chapter 3: 

Comments