In this post, I will be sharing the difference between / and % in Java. Both / and % are operators in Java. In simple words, / and % are mathematical operators and both have different use cases.

Read Also: What does % mean in Java

Difference between / and % in Java

/ is known as division operator whereas % is known as modulus operator.

Syntax:


Division Operator:
 (operand1 / operand2)


Modulus Operator:
 (operand1 % operand2)


/ performs the division operation and returns results as quotient. On the other side, % returns the remainder from the division.
For example:
1. int quotient = 17/3
2. int remainder = 17%3
The first operator division results in 5 whereas the second operator modulus results in 2.

More examples:
 15%2=1(remainder) 15/2=7(quotient)

 25%5=0(remainder) 25/5=5(quotient)


Java Program Example

 public class DivisionModulusOperator {
public static void main(String args[]) {
int num1 = 5;
int num2 = 31;
// Division operator
int quotient = num2 / num1;
// Modulus operator
int remainder = num2 % num1;
// Printing the result
System.out.println("num2/num1 = " + quotient);
System.out.println("num2%num1 = " + remainder);
}
}


Output:
num2/num1 = 6
num2%num1 = 1

That's all for today. Please mention in the comments in case you have any questions related to the difference between / and % in Java.