Java Program to Check Whether Number is Divisible by 5
Last Updated :
26 Nov, 2022
Generic rules in mathematics to test divisibility by 5 in a number system is followed as numbers that end in 5 or 0 are divisible by 5. It doesn't matter how big the number is. Modulo operator will be used most frequently in programming when it comes down to divisibility.
Knowledge of the range of numbers that primitive datatypes can store is necessary for the user to get a better understanding while entering custom input as different data types hold a different range of values which is already defined. Here is a quick glimpse of data types that are used more frequently.
Datatype | Size | Range Of Storing Whole Numbers |
---|
short | 2 Bytes | -32,768 to 32,767 |
int | 4 Bytes | -2,147,483,648 to 2,147,483,647 |
long | 8 Bytes | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
Approach: When it comes down to check divisibility arithmetic operator '%' is used because it returns the remainder of two numbers. Now for a given number be it any random number say 'n' be divided by some random number 'm' where 'm' should be smaller than 'n'. Let n=10 and m=3, after using the above approach- n%m that is 10%3 will give remainder equal to unity say it 'r' which is obtained when 10 is divided by 3.
Math: 10 % 5 = 0
Example:
Remainder = Dividend % Divisor
1 = 10 % 3
Example:
Input n : 464565625
Processing : n % 5 == 0
Output : Yes
Input n : 57867456354524524524635602
Processing : n % 5 == 0
Output : No
Input n : 346356 56474675364354243220
Processing : n % 5 == 0
Output : Yes
Approach 1: Input number is not very large
Let us first assume that the number not very large, we can thus we can take the input as an integer and use the Modulo Arithmetic Operator to check if a number is divisible by 5 or not. Thus, if n % 5 == 0, the number is divisible by 5.
Below is the implementation of the above idea.
Java
// Java program to find if a number
// is divisible by 5 or not
// importing Classes/Files
import java.util.*;
class GFG {
// Main Driver function
public static void main(String[] args)
{
// Taking any random number to test
int n = 464565625;
// Checking if remainder is 0 or not
// when divided by 5
if (n % 5 == 0)
// Print yes if no is divisible by 5
System.out.println("Yes");
else
// Print no if no is not divisible by 5
System.out.println("No");
}
}
Time Complexity: O(1)
Auxiliary Space: O(1)
What if the number is very large user hardcoded input. The program will compile but will throw an exception during runtime. There is an issue using the above same approach while dealing with big numbers when it comes downs to the divisibility of big numbers as the existing data types discussed above has a specific size over which the range is fixed for what they can store. If the above approach is used where data types are forced to store big integers in primitive data types error is thrown at the compile time itself: 'Integer number too large.'

Approach 2: Input number is not very large
- Use Java Big Integers and check divisibility by using modulo operator, similar to the above mentioned.
- Use the fact that a number is divisible by 5 if its last digit is either 0 or 5.
Using the above-mentioned fact we just have to check if the last digit is 0 or 5. Below is the implementation of the above idea.
Java
// Java program to find if a number
// is divisible by 5 or not
// Importing Classes/Files
import java.util.*;
public class GFG {
// Function for divisibility by 5
static boolean isDivisibleBy5(String num)
{
// Number of Digits
int sz = num.length();
// Checking if last digit is 5
// Checking if last digit is 0
return (
(Integer.parseInt(num.substring(sz - 1)) == 5)
|| (Integer.parseInt(num.substring(sz - 1))
== 0));
}
// Main Driver function
public static void main(String[] args)
{
// Considering any big random number
String num = "464565625";
// Condition check for divisibility
if (isDivisibleBy5(num))
// Print yes if no is divisible
System.out.println("Yes");
else
// Print no if no is not divisible
System.out.println("No");
}
}
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Java Program for Smallest K digit number divisible by X Integers X and K are given. The task is to find the smallest K-digit number divisible by X. Examples: Input : X = 83, K = 5 Output : 10043 10040 is the smallest 5 digit number that is multiple of 83. Input : X = 5, K = 2 Output : 10 An efficient solution would be : Compute MIN : smallest K-digit num
2 min read
Java Program to Check if a Given Number is Perfect Number A number is said to be a perfect number if the sum of its proper divisors ( i.e. all positive divisors excluding the number itself )is equal to that number itself. Aliquot sum is the sum of divisors of a number, excluding the number itself. Hence, a number is a perfect number only if it is equal to
5 min read
Java Program to Check if a Given Integer is Odd or Even A number that is divisible by 2 and generates a remainder of 0 is called an even number. All the numbers ending with 0, 2, 4, 6, and 8 are even numbers. On the other hand, number that is not divisible by 2 and generates a remainder of 1 is called an odd number. All the numbers ending with 1, 3, 5,7,
7 min read
Java Program to Check if count of divisors is even or odd Given a number "n", find its total number of divisors is even or odd. Examples: Input: n = 10 Output: EvenInput: n = 100Output: OddInput: n = 125Output: EvenA naive approach would be to find all the divisors and then see if the total number of divisors is even or odd. The time complexity for such a
4 min read
Prime Number Program in Java A prime number is a natural number greater than 1, divisible only by 1 and itself. Examples include 2, 3, 5, 7, and 11. These numbers have no other factors besides themselves and one.In this article, we will learn how to write a prime number program in Java when the input given is a Positive number.
6 min read
Fizz Buzz Program in Java FizzBuzz is a game popular amongst kids that also teaches them the concept of division. In recent times it has become a popular programming question. Following is the problem statement for the FizzBuzz problem. Examples: Input: 9Output: FizzExplanation: The number is divisible by 3 only. Input: 25Ou
2 min read
Modulo or Remainder Operator in Java Modulo or Remainder Operator returns the remainder of the two numbers after division. If you are provided with two numbers, say A and B, A is the dividend and B is the divisor, A mod B is there a remainder of the division of A and B. Modulo operator is an arithmetical operator which is denoted by %.
2 min read
Java Program to Check if all digits of a number divide it Given a number n, find whether all digits of n divide it or not.Examples: Input : 128 Output : Yes 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0. Input : 130 Output : No We want to test whether each digit is non-zero and divides the number. For example, with 128, we want to test d != 0 && 128
2 min read
Java Program to check whether it is possible to make a divisible by 3 number using all digits in an array Given an array of integers, the task is to find whether it's possible to construct an integer using all the digits of these numbers such that it would be divisible by 3. If it is possible then print "Yes" and if not print "No". Examples: Input : arr[] = {40, 50, 90} Output : Yes We can construct a n
2 min read