
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check Divisibility by 5 in Java
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.
Following are some examples which helps you to understand the divibility rule of 5 ?
- 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 using Java.
Algorithm
Following are the steps that determine how to solve this problem statements ?
- 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 divide the integer variable by 5 using the mod(%) operator.
- Finally, print the result.
Example 1
In this program, we are going to use an if-else statement to check whether the number divisibility 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"); } }
Output
The above code produces the following output ?
590 is the number is divisible by 5
Example 2
Below the program check the divisibility rule of 5 with the help of '!=' operator.
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
The above code produces the following 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.
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 above code produces the following 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
The above different codes of Java illustrated the divisibility rule of 5. It shows the importance of various operators like mod(%) and not equal to(!=). Thus, while solving these types of problem statements you must be aware of the divisibility rules.