Q:

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

belongs to collection: Java Basic Solved Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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)

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Basic Solved Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a Java Program to Check input number is Even... >>
<< Write a Java Program to Swap two numbers without u...