In Java, to check if a number is divisible by 3, we can use either an if-else conditional statement or the ternary operator. This article focuses on learning through examples, detailed logic, and program explanations 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 3. The program should take an integer as input and output whether the number is divisible by 3 or not. The above action should be done in our java program. Let’s look at an example what we have to achieve.
Example 1:
Input number: 5
Using the modulo operation: 5 % 3 = 2
Since the remainder is not 0, 5 is not divisible by 3.
Here for the input 5, Output is “It is not divisible by 3”, As 5 is not divisible by 3.
Example 2:
Input number: 6
Using the modulo operation: 6 % 3 = 0
Since the remainder is 0, 6 is divisible by 3.
Here for the input 6, Output is “It is divisible by 3”, As 6 is divisible by 3.
Logic to check number is divisible by 3
There are multiple logics to check a given input is divisible by 3 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 by 3, if the remainder is zero, the condition is satisfied, and the program prints “Number is divisible by 3.”
- If the remainder is not zero, the else block will execute and print “Number is not divisible by 3.”
if (number % 3 == 0)
System.out.println("Number is divisible by 3");
else
System.out.println("Number is not divisible by 3");
Logic 2: Ternary Operator
- This operator takes three operands and works similarly to if-else statements. If the condition is true, it prints the first expression; if the condition is false, it prints the second expression.
- When a number is divided by 3, if the remainder is zero, then the number is divisible by 3. Otherwise, it is not.
result=(number%3==0)? number+" is divisible by 3":number+" not divisible by 3";
System.out.println(result);
Java Program to check number divisible by 3
Program 1: Using if-else
In the below program we are using if else to check number is divisible by 3 in Java.
import java.util.Scanner;
public class DivisbleByThree {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("enter the number");
int number = s.nextInt();
if (number % 3 == 0)
System.out.println("Number is divisible by 3");
else
System.out.println("Number is not divisible by 3");
s.close();
}
}
Output:
enter the number
15
Number is divisible by 3
Program Explanation to check num divisible by 3
- The program imports the Scanner class to enable user input.
- It defines a class named
DivisibleByThree
. - 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 divisible by 3 (i.e., has no remainder when divided by 3).
- If the condition is true, it prints “Number is divisible by 3”.
- If the condition is false, it prints “Number is not divisible by 3”.
- 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 3 in Java.
import java.util.Scanner;
public class DivisbleByThree {
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%3==0)? number+" is divisible by 3":number+" not divisible by 3";
System.out.println(result);
s.close();
}
}
Output:
enter the number
3217
3217 not divisible by 3
Program Explanation to check num divisible by 3
- The program imports the Scanner class to allow user input.
- It defines a class named
DivisibleByThree
. - 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 the variable
number
. - Uses a ternary operator to assign the appropriate message to the variable
result
based on whether the number is divisible by 3 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 3 using conditional statements like if-else and the ternary operator. We understood the logic behind this and moved on to writing the program.
For more information on related topics, you might find these articles helpful:
- Understanding the concepts of Conditional statements in Java.
- Concepts of operators in Java
- Top 10 Programming tips in Java
We hope you find this article helpful. If you have any questions or need clarification, please leave a comment below. Don’t forget to subscribe to our newsletter for more updates on Java programming tips and tutorials.
Happy Learning!!