In this post we will discuss a java program to take input of a number from the user and check whether the input number is a prime number or not. For each case print appropriate message.
import java.util.Scanner;
class PRIME
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n=sc.nextInt(); // the entered number will store in variable n
int f=0; //variable f is used as a counter
if(n<=1)
{
System.out.println("Number is not Prime");
}
else if(n==2||n==3)
{
System.out.println("Number is prime");
}
else
{
for(int i=2;i<=n/2;i++)
{
if(n%i==0)
{
System.out.print("Number is not prime");
f++;
break;
}
}
if(f==0)
System.out.println("Number is prime");
}
}
}
In this post i have shared a java program which is mostly asked by students of computer science.
THANK YOU FOR VISITING OUR BLOG.For more related posts and anime,programing,gaming post please regularly check our blog.
THANK YOU!!!!!!!!!!!!
Making money as a coder in 2025 is not much of a big or tough…
Lloyds Technology Centre, A tech and data company located in Hyderabad, India. They are part of…
How to Increase Height at Any Age Rise Above: How to Increase Height at Any…
In the fast-paced digital world, we often forget the timeless beauty that nature offers. Today,…
Top 10 Deadliest Kicks in Martial Arts: Master the Art of Knockouts Martial arts is…
How to Optimize Battery Life of Your Smartphone: Tips and Tricks for Prolonging Battery Health…