In the previous article, we have seen Java Program to Find Product of Even and Odd Digits of a Number
In this article we will see how we can check if a number is divisible by both 5 and 11 by using Java programming language.
Java Program to Check if Number is Divisible 5 and 11
We have to check number which is divisible by 5 and 11.
For example: A number is 110
110 % 5 = 22
110 % 11 =10
Let’s see different ways to check the number is divisible by 5 and 11.
Method-1: Java Program to Check if Number is Divisible 5 and 11 By Using Static Input Value
Approach:
- Declare a number.
- Inside if condition check if it is divisible by both 5 and 11, if it is divisible by both then print the number is divisible.
- Else print the number is not divisible by both 5 and 11.
Program:
import java.util.*; class Main { public static void main(String args[]) { //a number declared int num=110; //checking if number is divisible by both 5 and 11 //then print is divisbile by both 5 and 11 if((num%5==0)&&(num%11==0)) { System.out.println("Number " +num+ " is divisible by both 5 and 11"); } //else print the number is not divisible by both 5 and 11 else { System.out.println("Number " +num+ " is not divisible by both 5 and 11"); } } }
Output: Number 110 is divisible by both 5 and 11
Method-2: Java Program to Check if Number is Divisible 5 and 11 By Using User Input Value
Approach:
- Take a number as user input.
- Inside if condition check if it is divisible by both 5 and 11, if it is divisible by both then print the number is divisible.
- Else print the number is not divisible by both 5 and 11.
Program:
import java.util.*; class Main { public static void main(String args[]) { //Scanner class object created Scanner sc=new Scanner(System.in); System.out.print("Enter a number: "); //taking a number as user input int num=sc.nextInt(); //checking if number is divisible by both 5 and 11 //then print is divisbile by both 5 and 11 if((num%5==0)&&(num%11==0)) { System.out.println("Number " +num+ " is divisible by both 5 and 11"); } //else print the number is not divisible by both 5 and 11 else { System.out.println("Number " +num+ " is not divisible by both 5 and 11"); } } }
Output: Case-1 Enter a number: 55 Number 55 is divisible by both 5 and 11 Case-2 Enter a number: 120 Number 120 is not divisible by both 5 and 11
Method-3: Java Program to Check if Number is Divisible 5 and 11 By Using User Defined Method
Approach:
- Take a number as user input.
- Then call user defined method
checkDivisible()
by passing the entered number as parameter. - Inside method in the if condition check if it is divisible by both 5 and 11, if it is divisible by both then print the number is divisible.
- Else print the number is not divisible by both 5 and 11.
Program:
import java.util.*; class Main { public static void main(String args[]) { //Scanner class object created Scanner sc=new Scanner(System.in); System.out.print("Enter a number: "); //taking a number as user input int num=sc.nextInt(); //calling user defined method checkDivisible() checkDivisible(num); } public static void checkDivisible(int num) { //checking if number is divisible by both 5 and 11 //then print is divisbile by both 5 and 11 if((num%5==0)&&(num%11==0)) { System.out.println("Number " +num+ " is divisible by both 5 and 11"); } //else print the number is not divisible by both 5 and 11 else { System.out.println("Number " +num+ " is not divisible by both 5 and 11"); } } }
Output: Enter a number: 55 Number 55 is divisible by both 5 and 11
Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language.
Related Java Programs: