- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Check Whether Number is Divisible by 5
Introduction
This program is a simple Java program that checks whether a number entered by the user is divisible by 5 or not. The program prompts the user to enter a number, reads the input using the Scanner class, and then checks whether the number is divisible by 5 using the modulus operator %. If the remainder of the division is 0, then the number is divisible by 5, and the program prints a message to the console indicating so. If the remainder is not 0, then the number is not divisible by 5, and the program prints a message to the console indicating that as well.
The program uses basic Java concepts such as variables, user input, conditional statements, and console output. It also demonstrates the use of the Scanner class to read user input from the console.
Commonly Used Primitive Data Types
Having a basic understanding of the range of values that different data types can store can be helpful when writing programs that involve user input. Here's a quick overview of some of the commonly used primitive data types in Java and the range of values they can store −
Data Type | 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 |
It's important to note that the above ranges are for the primitive data types themselves, and do not take into account any constraints or limitations that may be imposed by the context or the program itself. For example, a program may restrict input to a certain range of values, or impose additional constraints on the data type.
Formula
number % 5 == 0
Example 1
Approach
First, we import the Scanner class to read input from the user.
We then create a Scanner object to read input from the console.
We prompt the user to enter a number.
We read the number entered by the user using the nextInt() method of the Scanner class and store it in an integer variable number.
We then check whether the number is divisible by 5 or not using the modulus operator %. If the remainder of the number divided by 5 is 0, then the number is divisible by 5. If the remainder is not 0, then the number is not divisible by 5.
We then print a message to the console indicating whether the number is divisible by 5 or not.
Finally, we close the Scanner object to release any resources associated with it.
Here's a Java program to check whether a number is divisible by 5 or not.
import java.util.Scanner; public class DivisibleBy5 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); int number = scanner.nextInt(); if (number % 5 == 0) { System.out.println(number + " is divisible by 5."); } else { System.out.println(number + " is not divisible by 5."); } scanner.close(); } }
Explanation
In this program, we first import the Scanner class to read input from the user. We then prompt the user to enter a number and read it using the nextInt() method of the Scanner class.
We then check whether the number is divisible by 5 or not using the modulus operator %. If the remainder of the number divided by 5 is 0, then the number is divisible by 5. If the remainder is not 0, then the number is not divisible by 5.
We then print a message to the console indicating whether the number is divisible by 5 or not. Finally, we close the Scanner object to release any resources associated with it.
Output
Enter a number: 55 55 is divisible by 5.
Example 2
Approach
Create a Scanner object to read input from the console.
Prompt the user to enter a number.
Read the input using the nextBigInteger() method of the Scanner object, and store it in a BigInteger variable.
Use the mod() method of the BigInteger class to compute the remainder of the division of the input number by 5.
Compare the result of mod() with BigInteger.ZERO to check whether the remainder is equal to 0.
If the remainder is 0, print a message to the console indicating that the number is divisible by 5.
If the remainder is not 0, print a message to the console indicating that the number is not divisible by 5.
Close the Scanner object to release any resources associated with it.
Here's a Java program to check whether a number is divisible by 5, assuming the input number is not very large −
import java.math.BigInteger; import java.util.Scanner; public class DivisibleBy5 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); BigInteger number = scanner.nextBigInteger(); if (number.mod(BigInteger.valueOf(5)).equals(BigInteger.ZERO)) { System.out.println(number + " is divisible by 5."); } else { System.out.println(number + " is not divisible by 5."); } scanner.close(); } }
Explanation
In this program, we use the BigInteger class from the java.math package to handle large integers. The program prompts the user to enter a number, reads the input using the Scanner class, and then creates a BigInteger object to store the input number.
To check whether the number is divisible by 5, we use the mod() method of the BigInteger class to compute the remainder of the division of the input number by 5. We compare the result of mod() with BigInteger.ZERO to check whether the remainder is equal to 0. If the remainder is 0, then the number is divisible by 5, and the program prints a message to the console indicating so. If the remainder is not 0, then the number is not divisible by 5, and the program prints a message to the console indicating that as well.
Note that we use BigInteger.valueOf(5) to create a BigInteger object representing the value 5, since the % operator cannot be used with BigInteger objects directly.
Output
Enter a number: 56 56 is not divisible by 5.
Conclusion
We have explored two different approaches to check whether a number is divisible by 5 in Java.
The first approach uses a simple modulo operation to compute the remainder of the division of the input number by 5, while the second approach uses the BigInteger class to handle large integers and performs the same operation using the mod() method.