C++ Program to Multiply two numbers


January 16, 2023, Learn eTutorial
1245

How to find the product of two numbers?

This is a C++ program that asks the user to enter two integers. The entered integers are then stored in two different variables. These two variables are then multiplied together by using the mathematical operator ‘*’ and the resultant value is stored in another variable. Finally, the value of the variable where the result is stored is displayed on the screen.

How can we implement a C++ program to multiply two numbers?

The program always starts with the main function. Within the main body declare 3 double type variables to store the two numbers to be multiplied and to store the value of the product of the entered numbers. ‘ num1, num2, product’.

Double is a data type used in c++ for storing double-precision floating-point values or decimal values. When we perform the division or multiplication operation, both the numbers should be of the double data type or else there could be a loss of some data.

Display a message to enter the numbers to be multiplied using the object cout.Read the numbers using the object cin. The first number is to the variable num1 and the second number is to the variable num2. Multiply the numbers using the arithmetic operator ‘*’. And store the result in the variable product. product=num1*num2.

Print the value of the variable product on the screen using cout. Finally, exit the program.

Algorithm

Step 1:  Call the header file iostream.

Step 2: Use the namespace std.

Step 3: Open the integer type main function; int main().

Step 4: Declare boolean type variables; num1, num2, product.

Step 5: Print a message on the screen to “Enter two numbers:” using cout.

Step 6: Read the first number to variable num1 and the second number to variable num2 using cin.

Step 7: Multiply the two numbers and store the result in the productproduct=num1*num2.

Step 8: Display the value of the variable ‘product ‘ on the screen using cout.

Step 9: Exit.

C++ Source Code

                                          #include <iostream>
using namespace std;
int main() {
      double num1, num2, product;
      cout << "Enter two numbers: ";
      cin >> num1 >> num2; // stores two floating point numbers in num1 and num2 respectively

     // performs multiplication and stores the result in product variable and Print the result
     product = num1 * num2;  
     cout << "Product = " << product;    
     return 0;
}
                                      

OUTPUT

RUN 1
--------
Enter two numbers: 10
2
Product = 20

RUN 2
--------
Enter two numbers: 2.5
.5
Product = 1.25