Home >Java >javaTutorial >Java program to check if a number is divisible by 5

Java program to check if a number is divisible by 5

王林
王林forward
2023-09-11 17:17:021723browse

Java program to check if a number is divisible by 5

Introduction

This program is a simple Java program that checks whether the number entered by the user is divisible by 5. The program prompts the user for a number, uses the Scanner class to read the input, and then uses the modulo operator % to check whether the number is divisible by 5. 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 this. If the remainder is not 0, the number is not divisible by 5, and the program also prints a message to the console to indicate this.

The program uses basic Java concepts such as variables, user input, conditional statements, and console output. It also demonstrates how to use the Scanner class to read user input from the console.

Commonly used primitive data types

When writing programs that involve user input, it is helpful to have a basic understanding of the range of values ​​that different data types can store. Here is a quick overview of some commonly used primitive data types in Java and the range of values ​​they can store -

type of data size Range of storing integers
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 is important to note that the above ranges apply to the primitive data type itself 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 might restrict inputs to a specific range of values ​​or impose additional constraints on data types.

formula

number % 5 == 0

Example 1

method

  • First, we import the Scanner class to read user input.

  • Then we create a Scanner object to read input from the console.

  • We prompt the user to enter a number.

  • We use the nextInt() method of the Scanner class to read the number entered by the user and store it in the integer variable number.

  • We then use the modulo operator % to check if the number is divisible by 5. A number is divisible by 5 if the remainder when divided by 5 is 0. 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.

  • Finally, we close the Scanner object to release all resources associated with it.

This is a Java program to check if a number is divisible by 5.

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

illustrate

In this program, we first import the Scanner class to read user input. We then prompt the user to enter a number and read it using the nextInt() method of the Scanner class.

Then we use the modulo operator % to check if the number is divisible by 5. A number is divisible by 5 if the remainder when divided by 5 is 0. 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. Finally, we close the Scanner object to release all resources associated with it.

Output

Enter a number: 55
55 is divisible by 5.

Example 2

method

  • Create a Scanner object to read input from the console.

  • Prompts the user to enter a number.

  • Use the Scanner object's nextBigInteger() method to read the input and store it in a BigInteger variable.

  • Use the mod() method of the BigInteger class to calculate the remainder of the input number divided by 5.

  • Compare the result of mod() with BigInteger.ZERO and 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 all resources associated with it.

Here is a Java program for checking 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();
   }
}

illustrate

In this program, we use the BigInteger class in the java.math package to handle large integers. The program prompts the user to enter a number, uses the Scanner class to read the input, and then creates a BigInteger object to store the entered number.

To check whether a number is divisible by 5, we use the mod() method of the BigInteger class to calculate the remainder of the input number divided by 5. We compare the result of mod() with BigInteger.ZERO to check if 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 to indicate this. If the remainder is not 0, the number is not divisible by 5, and the program also prints a message to the console to indicate this.

Please note that we use BigInteger.valueOf(5) to create a BigInteger object representing the value 5 because the % operator cannot be used directly with BigInteger objects.

Output

Enter a number: 56
56 is not divisible by 5.

in conclusion

  • We explored two different ways to check if a number is divisible by 5 in Java.

  • The first method uses a simple modulo operation to calculate the remainder of the input number divided by 5, while the second method uses the BigInteger class to handle large integers and uses the mod() method to perform the same operation.

The above is the detailed content of Java program to check if a number is divisible by 5. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete