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: Java program to display prime numbers between 1 to n.
Example: Program to check whether input number is prime or not
To understand this program you should have the knowledge of for loop, if-else statements and break statement.
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 any number:"); //capture the input in an integer int num=scan.nextInt(); scan.close(); 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 a Prime Number"); else System.out.println(num + " is not a Prime Number"); } }
Output:
Enter any number: 19 19 is a Prime Number
Output 2:
Enter any number: 6 6 is not a Prime Number
You can also use while loop to check the prime number:
Just replace this part of the code in above program:
for(int i=2;i<=num/2;i++) { temp=num%i; if(temp==0) { isPrime=false; break; } }
with this:
int i=2; while(i<= num/2) { if(num % i == 0) { isPrime = false; break; } i++; }
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();
}
}
893 is not a prime number it is divisible by 19…
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”);
}
}
}
Pls add “import java.util.*;” in the begening
This program is wrong
sorry man 893 divided by 19 by 47 times
Umm… actually, 893 is not a prime number mate. It is divisible by 19 at 47 times. So maybe that’s why you’re getting an error.
I realized, my program has a bug in it. :-(
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”);
}
}
This only checks if the number is even, if you enter 2 which is a prime number, it will consider it as not a prime number..
Hello i am new to java programming, can somebody explain me this code please ??
The program is perfect it runs and also no wrong o/p
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.
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.
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.
what is the role of boolean function here ? please explain .thank you.
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
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