Here are the C++ programs to find remainder of division of one number from another. You can find remainder using modulus operator or without using modulus operator etc. Those are discussed below.
Method 1 : C++ programs to find remainder of division of one number from another
Method 2 : C++ programs to find remainder without using modulus operator
Method 3 : C++ programs to find remainder using bitwise operator
Method 4 : C++ programs to find remainder using recursion
Method 1 : C++ programs to find remainder of division of one number from another
#include<iostream>
using namespace std;
int main()
{
int a,b,c=0;
cout<<"Enter Dividend and Divisor : ";
cin>>a>>b;
c=a%b;
cout<<"Remainder = "<<a<<endl;
return 0;
}
using namespace std;
int main()
{
int a,b,c=0;
cout<<"Enter Dividend and Divisor : ";
cin>>a>>b;
c=a%b;
cout<<"Remainder = "<<a<<endl;
return 0;
}
Method 2 : C++ programs to find remainder without using modulus operator
#include<iostream>
using namespace std;
int main()
{
int a,b,c=0;
cout<<"Enter Dividend and Divisor : ";
cin>>a>>b;
while(a>=b)
{
a-=b;
c++;
}
cout<<"Remainder = "<<a<<endl;
return 0;
}
using namespace std;
int main()
{
int a,b,c=0;
cout<<"Enter Dividend and Divisor : ";
cin>>a>>b;
while(a>=b)
{
a-=b;
c++;
}
cout<<"Remainder = "<<a<<endl;
return 0;
}
Method 3 : C++ programs to find remainder using bitwise operator
#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
int main()
{
int a,temp,b,xo,comb,sub=0;
cout<<"Enter Dividend ";
cin>>a;
cout<<"Enter Divisor ";
cin>>b;
sub=a;
comb=~b+1;
while(sub>=b)
{
temp=comb;
while(temp)
{
xo=sub^temp;
temp=sub&temp;
temp<<=1;
sub=xo;
}
}
cout<<"Remainder = "<<sub;
}
#include<stdio.h>
#include<math.h>
using namespace std;
int main()
{
int a,temp,b,xo,comb,sub=0;
cout<<"Enter Dividend ";
cin>>a;
cout<<"Enter Divisor ";
cin>>b;
sub=a;
comb=~b+1;
while(sub>=b)
{
temp=comb;
while(temp)
{
xo=sub^temp;
temp=sub&temp;
temp<<=1;
sub=xo;
}
}
cout<<"Remainder = "<<sub;
}
Method 4 : C++ programs to find remainder using recursion
#include<iostream>
using namespace std;
int divi(int n1,int n2,int &que)
{
if(n2>n1)
return n1;
divi(n1-n2,n2,++que);
}
int main()
{
int n1,n2,que=0;
cout<<"Enter dividend and divisor : ";
cin>>n1>>n2;
cout<<"Remainder : "<<divi(n1,n2,que);
return 0;
}
using namespace std;
int divi(int n1,int n2,int &que)
{
if(n2>n1)
return n1;
divi(n1-n2,n2,++que);
}
int main()
{
int n1,n2,que=0;
cout<<"Enter dividend and divisor : ";
cin>>n1>>n2;
cout<<"Remainder : "<<divi(n1,n2,que);
return 0;
}
No comments:
Post a Comment