Professional Documents
Culture Documents
th
/ and % are two different mathematical operators in Java and both have different uses as well.
/ will simply perform the division operation as used in mathematics and gives result as quotient
whereas % is known as modulus and gives the result as remainder of the division performed.
Example
The % (modulus) operator is used for finding the remainder when one number is divided by another.
For example:
The / (division) operator is used to find the quotient when a number is divided by another.
For example:
while (no>0)
{
int r=no%10;
sum=sum+r;
no=no/10;
}
System.out.println(“Sum of digits=”+sum);
}
}
Dry run
no>0 r=no%10 sum=sum+r no=no/10
1582>0 2=1582%10 2=0+2 158=1582/10
158>0 8=158%10 10=2+8 15=158/10
15>0 5=15%10 15=10+5 1=15/10
1>0 1=1%10 16=15+1 0=1/10
0>0