Write a Java Program to Check entered input is Prime Number or Not

By | 20.01.2017

Java Program to Check entered input is Prime Number or Not


A Prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. It means it is only divisible by 1 and itself, and it start from 2. The smallest prime number is 2.

Here i will show you how to write this program. You can also download below code nad extract file, after extract file you can easily run the program.

SOURCE CODE::

import java.util.Scanner;

public class JavaProgram {

    public static void main(String[] args) {
       
        int n,i=2;
        Scanner sc = new Scanner(System.in);
        System.out.println("Please Enter any No. : ");
        n=sc.nextInt();
        
        while(true)
        {   
            if(n==1)
            {
            System.out.println("Smallest Prime number is 2");
            break;
            }
           if(n%i==0)
           {
               break;
           }
           else
           {
               i++;
           }
        }
        if(n==i)
        {
            System.out.println("Prime No.");
        }
        else
        {
            System.out.println("Not Prime No.");
        }
    }
    
}

 

OUTPUT ::

 

Please Enter any No. : 
97
Prime No.

BUILD SUCCESSFUL (total time: 1 second)

 

5 1 vote
Article Rating
Category: Basic Programs Java Programming Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments