Write a java statement to print “Number is divisible by 5” or
“Number is not divisible by 5” after checking the value of a variable name num.
Answers
Answered by
1
Longer method (if statement)
int num = // write the value of the variable here;
if(num % 5 == 0) { //checks if the number divided by 5 leaves a remainder of 0
System.out.println("Number is divisible by 5")
} else {
System.out.println("Number is not divisible by 5")
}
Shorter method(ternary operator)
int num = ; // write the value of num here
System.out.println(num % 5 == 0 ? "Number is divisible by 5" : "Number is not divisible by 5");
it works like:
System.out.println(if num % 5 == 0 then if true, print "Number is divisible by 5" else print "Number is not divisible by 5
Similar questions