You are on page 1of 2

4 June21

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:

10%2=0 as the remainder is zero.


11%2 will give the output as 1 which is the remainder of above performed modulus operation.

The / (division) operator is used to find the quotient when a number is divided by another.

For example:

10/2=5 as the quotient is 5.

Number based programs – 15 Marks


th
Q.1 Write a program to add digits of a given no. (page 185 class 9 computer book.)
Example :no=1582
sum of digits(16)=1+5+8+2
% remainder -> 2=1582%10
/ gives quotient -> 158=1582/10
import java.util.*;
class p1{
public static void main(String args[])
{
Scanner sc=new Scanner (System.in);
int sum=0;
System.out.println(“Enter the number”);
int no=sc.nextInt();

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

You might also like