Multiplication of two numbers
Table of Contents
In this tutorial, we will discuss the C++ program to multiply of two numbers
In this post, we are going to learn how to find multiplication of two numbers via different 6 ways in C++ language
Program 1
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int num1=50,num2=20, product;
product=num1*num2;
cout << "Product of "<<num1<<" and "<<num2<<" is :"<< product<<endl;
getch();
return 0;
} When the above code is executed, it produces the following result
Product of 50 and 20 is: 1000
In this program,
Program 2
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int num1,num2;
cout<<"Enter two numbers: ";
//The program ask to enter two numbers
cin>>num1>>num2;
//store two integer numbers in variable num1 and num2 respectively
//performs multiplication and stores the result in variable product
int product=num1*num2;
cout << "Product of "<<num1<<" and "<<num2<<" is :"<< product<<endl;
//display the product of two numbers
getch();
return 0;
} When the above code is executed, it produces the following result
Enter two numbers 24 12 product of 24 and 12 is: 288
in this program,
Program 3
#include <iostream>
#include <conio.h>
using namespace std;
int multiply(int x, int y);//function prototype
int main()
{
int num1,num2;
cout<<"Enter two numbers: ";
//The program ask to enter two numbers
cin>>num1>>num2;
//store two integer numbers in variable num1 and num2 respectively
//performs multiplication and stores the result in variable product
int product=multiply(num1,num2);
cout << "Product of "<<num1<<" and "<<num2<<" is :"<< product<<endl;
//display the product of two numbers
getch();
return 0;
}
int multiply(int x, int y)
{
return(x*y);//return the result to main
}
When the above code is executed, it produces the following result
Enter two numbers 40 30 product of 40 and 30 is: 1200
Approach
Program 4
#include <iostream>
#include <conio.h>
using namespace std;
int multiply(int,int);//function prototype
int main()
{
int num1,num2;
cout<<"Enter two numbers: ";
//The program ask to enter two numbers
cin>>num1>>num2;
//store two integer numbers in variable num1 and num2 respectively
//performs multiplication and stores the result in variable product
int product=multiply(num1,num2);
cout << "Product of "<<num1<<" and "<<num2<<" is :"<< product<<endl;
//display the product of two numbers
getch();
return 0;
}
int multiply(int x, int y)
{
if(x<y){
return multiply(y,x);
}
else if(y!=0){
return(x+multiply(x,y-1));//return the result to main
}
else {
return 0;
}
}
When the above code is executed, it produces the following result
Enter two numbers 23 34 product of 40 and 30 is: 782
Program 5
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int num1,num2,mul;
int *ptr1,*ptr2;
cout<<"Enter two numbers: ";
//The program ask to enter two numbers
cin>>num1>>num2;
//store two integer numbers in variable num1 and num2 respectively
//performs multiplication and stores the result in variable product
ptr1=&num1;
ptr2=&num2;
mul=*ptr1 * *ptr2;
cout << "Product of "<<num1<<" and "<<num2<<" is :"<< mul<<endl;
//display the product of two numbers
getch();
return 0;
}
When the above code is executed, it produces the following result
Enter two numbers 54 43 product of 40 and 30 is: 2322
Program 6
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int num1,num2,product=0;
cout << "Enter two numbers for multiply" << endl;
cin>>num1>>num2;
for(int i=1; i<=num2; i++){
product=product+num1;
}
cout<<"The product of "<<num1<<" and "<<num2<<" is :"<<product<<endl;
getch();
return 0;
}
When the above code is executed, it produces the following result
Enter two numbers for multiply 25 35 The product of 25 and 35 is: 875
Suggested for you
Data type in C++ language
Variable in C++ language
Similar post
Find product of two numbers in Java language|5ways
Multiply two numbers in Java using scanner| 5 different ways In this article, we will…
5 Different ways to Divide two numbers in Java using scanner In this article, we…
Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…
10 ways to subtract two numbers in Java In this article, we will discuss the…
Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…
How to Divide two numbers in Java| 5 different ways In this article, we will…
This website uses cookies.