/ / Prime Number Program in Java to Check Whether a Number is Prime Number or Not using Scanner

Prime Number Program in Java to Check Whether a Number is Prime Number or Not using Scanner

In this example, I shared a java program that checks whether a number is prime or not. Below java program for prime number program in java using scanner. In this prime in java, we take value from users using scanner and while loop concept in it. 

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

Prime Number Program in Java to Check Whether a Number is Prime Number or Not using Scanner
Prime Number Program in Java using Scanner

Print prime numbers from 1 to 100 in java

Java code for prime number 


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");
 }
   }
}

OUTPUT



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


This is all about the prime number program in java to check whether a number is prime number or not using scanner. 
Happy Coding 😊
Prime Number Program in Java to Check Whether a Number is Prime Number or Not using Scanner Prime Number Program in Java to Check Whether a Number is Prime Number or Not using Scanner Reviewed by Veer Bhadra Singh on May 06, 2020 Rating: 5

No comments:

Powered by Blogger.