In this tutorial, you are going to learn to write a java program to check whether a given number is divisible by 3 or not.
For example:
Take a number as input from the user.
Suppose a user has given
Input 1:
27
Output will be
Divisible by 3
Input 2:
22
Output will be
Not Divisible by 3
Java program to check given number is divisible by 3
import java.util.*;
class Main
{
public static void main(String args[])
{
int num;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the input Number:");
num=sc.nextInt();
if(num%3==0){
System.out.println("Given number is divisible by 3");
}
else
{
System.out.println("Given number is not divisible by 3");
}
}
}
Output
Enter the input Number:
24
Given number is divisible by 3