This program will prompt user to enter a number and then **it will check and display whether the input number is prime or not**.

import java.util.Scanner; class PrimeCheck { public static void main(String args[]) { int temp; boolean isPrime=true; Scanner scan= new Scanner(System.in); System.out.println("Enter a number for check:"); //capture the input in an integer int num=scan.nextInt(); for(int i=2;i<=num/2;i++) { temp=num%i; if(temp==0) { isPrime=false; break; } } //If isPrime is true then the number is prime else not if(isPrime) System.out.println(num + " is Prime Number"); else System.out.println(num + " is not Prime Number"); } }

Output:

Enter a number for check: 19 19 is Prime Number

Output 2:

Enter a number for check: 6 6 is not Prime Number

venkata kalluri says

The program fails, when i provided the number ‘ 893 ‘ . It is a prime number but the above program shows that it is not a prime. The following program gives the correct answer. However, The loop runs 8 times, irrespective of prime or not prime. In case of the above program, It exit the loop as soon as it is an even number but for odd numbers, the loop will run more number of times.

Please provide me your feedback(pros/cons) on this program.

public class PrimeNumber {

public static void main(String args[]) {

PrimeNumber p = new PrimeNumber();

Scanner scan = new Scanner(System.in);

System.out.println(“Enter a number for check in main method:”);

int n = scan.nextInt();

int count = 0;

int number = 0;

for (int i = 2; i <= 9; i++) {

if (n % i == 0) {

System.out.println(" count called when " + i);

count = count + 1;

}

number = number + 1;

System.out.println(" The loop called "+number+" times");

}

if (count == 0) {

System.out.println(n + " is a prime number ");

}

System.out.println(" ================================ ");

p.checkPrime();

}

}

lit-el Jon says

893 is not a prime number it is divisible by 19…

Sankar says

Hi friend try this program and give a feedback please,

public class PrimeNumber {

public static void main(String args[]) {

int num=0;

int prime=0;

Scanner sc=new Scanner(System.in);

System.out.println(“Enter the numbe to check”);

num=sc.nextInt();

if(num%2==0 && num>=2)

{

prime=0;

}

else

{

prime=1;

}

if(prime==0)

{

System.out.println(” prime number”);

}

else

{

System.out.println(” not prime number”);

}

}

}

Siddhabrata Mohapatra says

Pls add “import java.util.*;” in the begening

arun says

This program is wrong

sanjeev says

sorry man 893 divided by 19 by 47 times

venkata kalluri says

I realized, my program has a bug in it. :-(

Sayed Asad says

your example is to complex lets simplify the example of prime

public static void main(String args[]) {

int num = new Scanner(System.in).nextInt();

if (num % 2 == 0) {

System.out.println(“not prime”);

} else {

System.out.println(“Prime”);

}

}

Ashved says

Hello i am new to java programming, can somebody explain me this code please ??

Hrishi Joshi says

The program is perfect it runs and also no wrong o/p

harrisonthu says

No, I think it does not work if the input number is 1. OP, you need to hardcode to fix the code. if the input is 1.

Guest says

Why do you iterate the for loop to num/2 ?

“for(int i=2;i<=num/2;i++)"

Should probably be Math.sqrt(num) instead.

Guest says

it is so the program doesn’t allow numbers bellow 4 to enter the for loop. testing the numbers 2 and 3 wouldn’t make sense, because they are prime. However this program allows 1 to be a prime number which is demonstrably false as per the definition of a prime number, so a change to the logic would benefit the people reading this. For example you can get around this by adding the following code:

if(isPrime == 1)

{

isPrime=false;

}

this code must be placed before the last if structure and after the for loop.

hitesh kumar says

what is the role of boolean function here ? please explain .thank you.

Degaleesan Pugazhenthi says

How to identify the prime number count from 1 to 1000 without using the below code. (Required alternative method – should not go one by one and count it)

int Count=0;

for(int i=1;i<=1000;i++)

{

temp=num%i;

if(temp==0)

{

isPrime=false;

break;

}

}

//If isPrime is true then the number is prime else not

if(isPrime){

System.out.println(num + " is Prime Number");

Count++;

}

Thanks in Advance

Degaleesan

bharti mane says

for (i=2;i<=num/2;i++)

i just want yo know why this has been used?

as per my understanding i=2 means prime has two factors that is 1 and itself.

i dont know about next part..sir can you please explain