Here are the C++ programs to multiply two numbers .Here I have given different methods like multiply two numbers using multiplication operator, without using multiplication operator, using function, using recursion and using bitwise operator etc.
C++ program to multiply two numbers
#include<iostream>
using namespace std;
int main()
{
int a,b,c;
cout<<"Enter Multiplicand and Multiplier : ";
cin>>a>>b;
c=a*b;
cout<<"Product = "<<c<<endl;
return 0;
}
using namespace std;
int main()
{
int a,b,c;
cout<<"Enter Multiplicand and Multiplier : ";
cin>>a>>b;
c=a*b;
cout<<"Product = "<<c<<endl;
return 0;
}
C++ program to multiply two numbers without using third variable
#include<iostream>
using namespace std;
int main()
{
int a,b;
cout<<"Enter Multiplicand and Multiplier : ";
cin>>a>>b;
a=a*b;
cout<<"Product = "<<a<<endl;
return 0;
}
using namespace std;
int main()
{
int a,b;
cout<<"Enter Multiplicand and Multiplier : ";
cin>>a>>b;
a=a*b;
cout<<"Product = "<<a<<endl;
return 0;
}
C++ program to multiply two numbers using function
#include<iostream>
using namespace std;
int mul(int a,int b)
{
return a*b;
}
int main()
{
int a,b,c;
cout<<"Enter Multiplicand and Multiplier : ";
cin>>a>>b;
c=mul(a,b);
cout<<"Product = "<<c<<endl;
return 0;
}
using namespace std;
int mul(int a,int b)
{
return a*b;
}
int main()
{
int a,b,c;
cout<<"Enter Multiplicand and Multiplier : ";
cin>>a>>b;
c=mul(a,b);
cout<<"Product = "<<c<<endl;
return 0;
}
C++ program to multiply two numbers using pointers
#include<iostream>
using namespace std;
int main()
{
int a,b,c;
int *p,*q;
p=&a;
q=&b;
cout<<"Enter Multiplicand and Multiplier : ";
cin>>a>>b;
c=*p**q;
cout<<"Product = "<<c<<endl;
return 0;
}
using namespace std;
int main()
{
int a,b,c;
int *p,*q;
p=&a;
q=&b;
cout<<"Enter Multiplicand and Multiplier : ";
cin>>a>>b;
c=*p**q;
cout<<"Product = "<<c<<endl;
return 0;
}
C++ program to multiply two numbers using bitwise operators
#include<iostream>
using namespace std;
int main()
{
int a,b,temp,xo,sum=0;
cout<<"Enter Multiplicand and Multiplier : ";
cin>>a>>b;
for(int i=0;i<b;i++)
{
temp=a;
while(temp)
{
xo=sum^temp;
temp=sum&temp;
temp<<=1;
sum=xo;
}
}
cout<<"Product = "<<sum<<endl;
return 0;
}
using namespace std;
int main()
{
int a,b,temp,xo,sum=0;
cout<<"Enter Multiplicand and Multiplier : ";
cin>>a>>b;
for(int i=0;i<b;i++)
{
temp=a;
while(temp)
{
xo=sum^temp;
temp=sum&temp;
temp<<=1;
sum=xo;
}
}
cout<<"Product = "<<sum<<endl;
return 0;
}
C++ program to multiply two numbers without using multiplication operator
#include<iostream>
using namespace std;
int main()
{
int a,b,c=0;
cout<<"Enter Multiplicand and Multiplier : ";
cin>>a>>b;
for(int i=0;i<b;i++)
c+=a;
cout<<"Product = "<<c<<endl;
return 0;
}
using namespace std;
int main()
{
int a,b,c=0;
cout<<"Enter Multiplicand and Multiplier : ";
cin>>a>>b;
for(int i=0;i<b;i++)
c+=a;
cout<<"Product = "<<c<<endl;
return 0;
}
C++ program to multiply two numbers using recursion
#include<iostream>
using namespace std;
int mult(int n1,int n2,int res)
{
if(n2==0)
return res;
mult(n1,n2-1,res+n1);
}
int main()
{
int a,b;
cout<<"Enter Multiplicand and Multiplier : ";
cin>>a>>b;
cout<<"Product = "<<mult(a,b,0)<<endl;
return 0;
}
using namespace std;
int mult(int n1,int n2,int res)
{
if(n2==0)
return res;
mult(n1,n2-1,res+n1);
}
int main()
{
int a,b;
cout<<"Enter Multiplicand and Multiplier : ";
cin>>a>>b;
cout<<"Product = "<<mult(a,b,0)<<endl;
return 0;
}
No comments:
Post a Comment