Java program to check whether the number is divisible by 5


In mathematics, the divisibility rule of 5 states that if the number ends with 0 or 5 then it will divisible by 5. There is another way to identify the divisibility rule of 5, if the remainder comes to 0 it returns the number is divisible by 5. The mod(%) operator is normally used in programming when it comes to divisibility.

Let’s take an example of this.

  • Given number is 525, the number ends with 5 and it is divisible by 5.

  • Given number is 7050, the number ends with 0 and it is divisible by 5.

  • Given number is 678, the number does not end with 0 and 5 and it is not divisible by 5.

In this article, we are going to solve whether the number is divisible by 5.

Algorithm

The following steps are

  • We will use the package java.util.* to take user input of primitive data type.

  • Start with the main class, and initialize the integer variable to store the input integer.

  • Then make the integer variable divided by 5 using the mod(%) operator.

  • Finally, print the result.

Example 1

In this program, we are going to use the if-else statement to check whether the number is divisible by 5 or not.

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");
   }
}

Output

590 is the number is divisible by 5

Example 2

In this program, We will take an input variable and check it in the condition of the if statement with ‘!=’ operator. If an integer variable with the mod(%) operator is not equivalent to 0 it will result in the number being 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");
      }
   }
}

Output

109 is the number not divisible by 5

Example 3

In this program, we set the number range between 50-100 and check whether the number is divisible by 5 or not.

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);
         }
      }
   }
}

Output

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

Conclusion

We explored all three examples related to the divisibility of 5. Then using the mod(%) operator checks the condition of the divisibility rule. Once the number is divisible then it prints the result otherwise not. We will know the divisibility rule of other numbers also by taking references from this program.

Updated on: 20-Apr-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements