C++ Program to Multiply two Numbers

C++ProgrammingServer Side Programming

Multiplication of two numbers a and b yields their product. Value of a is added as many times as the value of b to get the product of a and b.

For example.

5 * 4 = 20
7 * 8 = 56
9 * 9 = 81

Program to Multiply two Numbers using * Operator

A program to multiply two numbers using the * operator is given as follows −

Example

 Live Demo

#include <iostream>
using namespace std;
int main() {
   int a = 6, b = 8;
   cout<<"Product of "<<a<<" and "<<b<<" is "<<a*b<<endl;
   return 0;
}

Output

Product of 6 and 8 is 48

In the above program, the product of a and b is simply displayed using the * operator. This is demonstrated by the following code snippet.

cout<<"Product of "<<a<<" and "<<b<<" is "<<a*b<<endl;

Program to Multiply two Numbers Without Using * Operator

A program to multiply two numbers without using the * operator is given as follows −

Example

 Live Demo

#include<iostream>
using namespace std;
int main() {
   int a=7, b=8, product=0;
   for(int i=1; i<=b; i++)
   product = product + a;
   cout<<"The product of "<<a<<" and "<<b<<" is "<<product<<endl;
   return 0;
}

Output

The product of 7 and 8 is 56

In the above program, a for loop is used to add the value of a total of b times. This yields the product of a and b.

This is demonstrated by the following code snippet.

for(int i=1; i<=b; i++)
product = product + a;

After this, the product of a and b is displayed. This is shown as follows −

cout<<"The product of "<<a<<" and "<<b<<" is "<<product<<endl;
raja
Published on 27-Sep-2018 10:44:05
Advertisements