In mathematics, the divisibility rule for 5 states that if a number ends in 0 or 5, then it is divisible by 5. There is another way to determine the divisibility rule for 5, if the remainder is 0, then return the number divisible by 5. mod(%) operator is usually used in programming for integer division.
Let's give an example.
The given number is 525, the number ends with 5 and is divisible by 5.
The given number is 7050 which ends with 0 and is divisible by 5.
The given number is 678, which does not end with 0 and 5 and is not divisible by 5.
In this article, we will address the question of whether the number is divisible by 5.
The following steps are
We will use the java.util.* packages to get user input of primitive data types.
Start from the main class and initialize integer variables to store the input integers.
Then use the mod(%) operator to divide the integer variable by 5.
Finally, print the results.
In this program, we will use if-else statement to check if a number is divisible by 5.
import java.util.*; public class Check_Divisiblity_of_five { public static void main(String[] args) { int number =590; if (number % 5 == 0) System.out.println(number+ "\tis the number is divisible by 5"); else System.out.println(number+ "\tThe number is not divisible by 5"); } }
590 is the number is divisible by 5
In this program, we will take an input variable and check it under the condition of an if statement using the "!=" operator. If an integer variable with the mod(%) operator is not equal to 0, it will cause the number to be divisible by 5.
import java.util.*; public class Check_Divisible_five { public static void main(String[] args) { int n=109; if(n % 5 != 0) { System.out.println(n+"\t is the number not divisible by 5"); } else { System.out.println(n+"\t is the number divisible by 5"); } } }
109 is the number not divisible by 5
In this program, we set a number range between 50-100 and check whether the number is divisible by 5.
import java.util.Scanner; public class Divisible_by_five { public static void main(String []args) { for(int n = 50; n <= 100; n++){ if(n % 5 == 0) { System.out.println("The number is divisible by 5 :\t"+ n); } } } }
The number is divisible by 5 : 50 The number is divisible by 5 : 55 The number is divisible by 5 : 60 The number is divisible by 5 : 65 The number is divisible by 5 : 70 The number is divisible by 5 : 75 The number is divisible by 5 : 80 The number is divisible by 5 : 85 The number is divisible by 5 : 90 The number is divisible by 5 : 95 The number is divisible by 5 : 100
We explored all three examples related to divisibility by 5. Then use the mod(%) operator to check the conditions of the integer division rule. Once the number is divisible it prints the result, otherwise it doesn't. By referring to this program we can also know the divisibility rules for other numbers.
The above is the detailed content of Java program to check if a number is divisible by 5. For more information, please follow other related articles on the PHP Chinese website!