Java Program to Check Number Divisible is by 5

In Java, we can check if a number is divisible by 5 using either an if-else condition or a ternary operator. Here, we focus on learning this concept through definitions, examples, an algorithm, detailed logic, and a program explanation for better understanding.

Required Knowledge

  • Java programming basics
  • Java Operators
  • Conditional Statements
  • Ternary Operator

Problem Statement

We need to write a Java program to check if a number is divisible by 5 or not. The program should take an integer as input and output whether it is divisible by 5 or not. This above action should be done in our program. Let’s look at an example,

Example 1:

Input: 15
Output: It is divisible by 5

Here, for the input 15, Output is “It is divisible by 5”, as 15 is divisible by 5.

Example 2:

Input: 18
Output: It is not divisible by 5

Here, for the input 18, Output is “It is not divisible by 5”, as 18 is not divisible by 5

Algorithm to Check Number is divisible by 5

  1. Start : Begin the algorithm with input number (num)
  2. Operator usage: We use modulus(%) operator.
    • This operator returns the remainder when one number is divided by 5.
  3. Check Divisibility: When the number is divided by 5, if we get remainder as Zero, then it says number is divisible by 5.
    • num%5==0, then number is divisible by 5
    • If we didn’t get remainder as zero, then number is not divisible by 5
  4. End: The algorithm terminates with the result of the divisibility check.

Logic to Check Number is divisible by 5

There are multiple logics to check a given input is divisible by 5 or not in Java Programing language.

Logic 1: Using if-else

  • Here, we are checking the remainder of the given input number.
  • When a number is divided with 5, It will give the remainder as Zero. Then condition is satisfied and prints “Number is divisible by 5”.
  • If remainder is not Zero, else block will execute and print “Number is not divisible by 5”.
if (number % 5 == 0)
	System.out.println("Number is divisible by 5");
else
	System.out.println("Number is not divisible by 5");

Logic 2: Ternary Operator

  • This operator takes three operands and works similar to the if else statements. If condition true, it prints first expression and if condition becomes false then it prints second expression.
  • When a number is divided with 5 and get the remainder as Zero, then it is divisible by 5 otherwise not.
result=(number%5==0)? number+" is divisible by 5":number+" not divisible by 5";
		System.out.println(result);

Java Program to Check number is divisible by 5

Program 1: Using if-else

In the below program we are using if else to check number is divisible by 5 in java.

import java.util.Scanner;

public class DivisbleByFive {

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		System.out.println("enter the number");
		int number = s.nextInt();
		if (number % 5!= 0)
			System.out.println("Number is not divisible by 5");
		else
			System.out.println("Number is divisible by 5");
		s.close();
	}
}

Output:

enter the number
650
Number is divisible by 5

Program Explanation to check num divisible by 5 using if-else

  • The Program imports the Scanner class to facilitate user input.
  • Defines a class named DivisibleByFive.
  • In the main method:
    • Creates a Scanner object ‘s’ to read user input.
    • Asks the user to enter a number.
    • Reads the integer input and stores it in ‘number’.
    • Checks if the number is not divisible by 5 (i.e., has a remainder when divided by 5).
    • If the condition is true, it prints “Number is not divisible by 5”.
    • If the condition is false, it prints “Number is divisible by 5”.
    • Closes the Scanner object to release system resources.

Program 2: Using Ternary Operator

In the below program we are using Ternary Operator to check number is divisible by 5 in java.

import java.util.Scanner;
public class DivisbleByFive{

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		System.out.println("enter the number");
		int number = s.nextInt();
		String result;
		result=(number%5==0)? 
		             number+" is divisible by 5":
		             number+" not divisible by 5";
		System.out.println(result);
		s.close();
	}
}

Output:

enter the number
345
345 is divisible by 5

Program Explanation to check num divisible by 5 using Ternary Operator

  • The program imports the Scanner class to facilitate user input.
  • Defines a class named DivisibleByFive.
  • In the main method:
    • Creates a Scanner object ‘s’ to read user input.
    • Asks the user to enter a number.
    • Reads the integer input and stores it in ‘number’.
    • Uses a ternary operator to assign the appropriate message to the variable ‘result’ based on whether the number is divisible by 5 or not.
    • Prints the ‘result’ message.
    • Closes the Scanner object to release system resources.

Conclusion

In this article, we explored how to check if a number is divisible by 5 using if-else statement and the ternary operator. We understood the logic behind this and moved on to writing the program.

For more information on related topics, you can find these articles which might be helpful:

  • Understanding the concept of Conditional statements in Java
  • Java program to check divisibility by 11
  • Concepts of operators in Java

We hope you find this tutorial helpful. If you have any issues or need any clarifications, feel free to leave a comment below. Don’t forget to subscribe our newsletter to get more updates related to Java programming tips and tutorials.

Stay focused and Happy learning!

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.