Modulus Operator
Title
Question
How to use Modulus Operator?
C-and-Cpp Arithmetic-Operators 05-06 min 20-30 sec
Answers:
If we want to calculate remainder then we should use modulus operator.It is denoted as %.For example if we want to find the number is even or odd the we find that by using %.
number%2==0
#include<stdio.h>
void main()
{
int i;
if(n%2==0)
{
prinntf("yes");
}
else{
printf("no");
}
}
If you want to calculate remainder then you should use modulus (%) instead of division. For eg. U want to find out the even and odd number
Then you may write like this
Include<stdio.h>
void main()
{
int n;
Printf("enter the number:);
Scanf(%d,'&n);
if(n%2==0)
{
prinntf("number i.e. %d is even");
}
else
{
printf("number i.e. %d is odd ");
}
#include<stdio.h>
void main()
{
int i;
if(n%2==0)
{
printf("yes");
}
else{
printf("no");
}
}
<font size="4">Program in c to obtain the remainder;</font>
#include <stdio.h>
int main()
{
int a,b;
float c;
a = 5;
b = 2;
c = a % b;
printf("Remainder of division %d and %d is %.2f\n",a,b,c);
return 0;
}
Output:
Remainder of division 5 and 2 is 1.00
<font size="4">Program in cpp to obtain the remainder:</font>
#include <iostream>
using namespace std;
int main()
{
int a,b;
float c;
a = 5;
b = 2;
c = a + b;
c = a % b;
cout<<"Remainder of division "<<a<<"and "<<b<<"is "<<c<<"\n";
return 0;
}
Output:
Remainder of 5and 2is 1
Login to add comment