Java Program to Check Whether a Number is Prime Number

In this tutorial, we learn how to write a Java Program to Check Whether a Number is Prime Number?. We will write the Java Program to Check Whether a Number is Prime Number. Write a java program to input the number from the user and find the number is prime number. How to find century for given years in java programming. Logic to check whether a number is prime number.

Learn about while loop, do-while loop, and for loop.



Java Program to Check Whether a Number is Prime Number
Prime Number Program in Java

Related Post: Java Program to Print Prime Numbers from 1 to 100

Java Program to Check Whether a Number is Prime Number

Program:

import java.util.*; 
class prime {
    public static void main(String args[])
    {
     Scanner s=new Scanner(System.in);
     int x=s.nextInt();
     int n=2,count=0;
   while(n<x)
   {
   if(x%n==0 && x!=n)
  {
  System.out.println("Working");
  count=count+1;
  }
     n=n+1;
 }
     if(count>0)
 {
   System.out.println(x+" is not prime Number"); 
 }
 else
 {
   System.out.println(x+" is prime Number");
 }
   }
}
Program Output:

  C:\jdk-12.0.2\bin>java prime
  17
  17 is prime Number


Happy Coding ๐Ÿ˜Š

No comments:

If you have any doubts, Please let me know

Powered by Blogger.