We can check, if a number is divisible by eleven or not in java by using if-else condition or Ternary Operator. Here, we focus on learning through definition, examples, algorithm, logic and program explanation for better understanding.
Java Concept that we are using
If-else: If the condition in if statement true, then prints the statement in the if block. When the condition becomes false, it executes the statements in the else block.
if(condition){
System.out.println("print the statement");
}
else{
System.out.println("print this statement");
}
Ternary Operator: It is the only conditional operator that takes 3 operands. It is a one-liner replacement if else statements, we can also replace this operator with switch.
Variable = Expression ? Expression1 : Expression3;
if condition is true,it executes expression 1 and when condition becomes false then it executes expression 2.
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 11 or not. The program should print the result whether a number is divisible by 11 or not divisible by 11. This above action should be done in our program. Let’s look at an example,
Example:
If the input number is 12, then the output should be ” 12 is not divisible by 11″.
Explanation:
Input number: 12
Modulo operation: 12%11 = 1 (remainder is 1)
As we didn't get the remainder 0, 12 is not divisible by 11.
Algorithm to check Number is divisible by Eleven
- Start: Take input number
- Check the Condition:
- if-else: If(number%11==0)
- print number is divisible by 11, else not divisible by 11
- Ternary Operator: Take one variable to store result
- (number%11==0) ? number + ” divisible by 11″ : number +”not divisible by 11″;
- Print the result.
- if-else: If(number%11==0)
- End the Program.
Logic to check Number is divisible by eleven in Java
- Take an input value from user
- Read and Store it
- Check Condition:
- if-else: if(number%11==0)
- When a number is divided with 11 and get the remainder as Zero, then it is divisible by 11 otherwise not.
- Ternary Operator: When a number is divided with 11 and get the remainder as Zero, then it is divisible by 11 otherwise not.
- if-else: if(number%11==0)
- print the result
Java Program to check Num divisible by 11
Program 1: Using if-else
In the below program we are using if else to check Num divisible by 11.
import java.util.Scanner;
public class DivisbleByEleven {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("enter the number");
int number = s.nextInt();
if (number % 11 == 0)
System.out.println("Number is divisible by 11");
else
System.out.println("Number is not divisible by 11");
s.close();
}
}
Output:
enter the number
1324
Number is not divisible by 11
Program Explanation to check num divisible by 11 using if-else
- This Java program checks whether a given number entered by the user is divisible by 11.
- It imports the Scanner class to facilitate user input.
- Defines a class named DivisibleByEleven.
- 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 ‘number’ is divisible by 11 using the modulo operation (%).
- If the remainder is 0, prints “Number is divisible by 11”.
- If the remainder is not 0, prints “Number is not divisible by 11”.
- Closes the Scanner object to release system resources.
Program 2: Using Ternary Operator
In the below program we are using Ternary Operator to check Num divisible by 11.
import java.util.Scanner;
public class DivisbleByEleven {
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%11==0) ? number +" is divisible by 11": number+ " is not divisible by 11";
System.out.println(result);
s.close();
}
}
Output:
enter the number
220
220 is divisible by 11
Program Explanation to check num divisible by 11 using Ternary Operator
- This Java program checks whether a given number entered by the user is divisible by 11.
- It imports the Scanner class to facilitate user input.
- Defines a class named DivisibleByEleven.
- 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’.
- Initializes a String variable named ‘result’ to hold the output message.
- Uses a ternary operator to assign the appropriate message to ‘result’ based on whether ‘number’ is divisible by 11 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 11 or not in Java using if-else statement, and the ternary operator. We have learnt the basic logic and moved on to writing the Java program.
For more information on related topics, you might find these articles helpful:-
- Understanding Conditional statements in Java
- Top 10 Java Programing Tips
- Understanding Operators in Java
We hope this tutorial has been helpful. If you have any questions or need any further clarification, feel free to leave a comment below and do not forget to subscribe to our newsletter for more java programming tips and tutorials!
Happy Coding!