The number which is only
divisible by itself and 1 is known as prime
number, for example 7 is a prime number because it is only
divisible by itself and 1.
This program takes the number (entered by user) and then checks whether the input number is prime or not. The program then displays the result. If you are looking for a program that displays the prime number between two intervals then see:
This program takes the number (entered by user) and then checks whether the input number is prime or not. The program then displays the result. If you are looking for a program that displays the prime number between two intervals then see:
import java.util.Scanner;
class Prime1
{
public static void main(String args[])
{
int temp;
boolean isPrime=true;
Scanner scan= new Scanner(System.in);
System.out.println("Enter any nuber:");
//capture the input in an integer
int n=scan.nextInt();
scan.close();
for(int i=2;i<=n/2;i++)
{
temp=n%i;
if(temp==0)
{
isPrime=false;
break;
}
}
//If isPrime is true then the nber is prime else not
if(isPrime)
System.out.println(n + " is a Prime Nber");
else
System.out.println(n + " is not a Prime Nber");
}
}
Enter any number:
5
5 is a Prime Number
BUILD SUCCESSFUL (total
time: 11 seconds)
0 comments:
Post a Comment