What is a Prime Number?
A prime number is a number that is only divisible by 1 or itself. For example, 11 is only divisible by 1 or itself. Other Prime numbers 2, 3, 5, 7, 11, 13, 17....
Note: 0 and 1 are not prime numbers. 2 is the only even prime number.
Java Program to check whether number is prime or not
Program Logic:
- We need to divide an input number, say 19 from values 2 to 19 and check the remainder. If remainder is 0 number is not prime.
- No number is divisible by more than half of itself. So we need to loop through just number To Check/2 . If input is 19, half is 9.5 and the loop will iterate through values 2 to 9
- If a number To Check is completely divisible by other number, flag isPrime is set to true and the loop is exited.
import java.util.*;
public class Prime_number
{
public static void main(String[] args)
{
int remainder;
boolean isPrime=true;
int numberToCheck; // Enter the numberToCheckber you want to check for prime
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number:");
numberToCheck=sc.nextInt();
//Loop to check whether the numberToCheckber is divisible any numberToCheckber other than 1 and iteself
for(int i=2;i<=numberToCheck/2;i++)
{
//numberToCheckber is dived by itself
remainder=numberToCheck%i;
System.out.println(numberToCheck+" Divided by "+ i + " gives a remainder "+remainder);
//if remainder is 0 than numberToCheckber is not prime and break loop. Elese continue loop
if(remainder==0)
{
isPrime=false;
break;
}
}
// Check value true or false,if isprime is true then numberToCheckber is prime otherwise not prime
if(isPrime)
System.out.println(numberToCheck + " is a Prime numberToCheckber");
else
System.out.println(numberToCheck + " is not a Prime numberToCheckber");
}
}
public class Prime_number
{
public static void main(String[] args)
{
int remainder;
boolean isPrime=true;
int numberToCheck; // Enter the numberToCheckber you want to check for prime
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number:");
numberToCheck=sc.nextInt();
//Loop to check whether the numberToCheckber is divisible any numberToCheckber other than 1 and iteself
for(int i=2;i<=numberToCheck/2;i++)
{
//numberToCheckber is dived by itself
remainder=numberToCheck%i;
System.out.println(numberToCheck+" Divided by "+ i + " gives a remainder "+remainder);
//if remainder is 0 than numberToCheckber is not prime and break loop. Elese continue loop
if(remainder==0)
{
isPrime=false;
break;
}
}
// Check value true or false,if isprime is true then numberToCheckber is prime otherwise not prime
if(isPrime)
System.out.println(numberToCheck + " is a Prime numberToCheckber");
else
System.out.println(numberToCheck + " is not a Prime numberToCheckber");
}
}
Output:
Enter the number:
19
19
19 Divided by 2 gives a remainder 1
19 Divided by 3 gives a remainder 1
19 Divided by 4 gives a remainder 3
19 Divided by 5 gives a remainder 4
19 Divided by 6 gives a remainder 1
19 Divided by 7 gives a remainder 5
19 Divided by 8 gives a remainder 3
19 Divided by 9 gives a remainder 1
19 is a Prime numberToCheckber
19 Divided by 3 gives a remainder 1
19 Divided by 4 gives a remainder 3
19 Divided by 5 gives a remainder 4
19 Divided by 6 gives a remainder 1
19 Divided by 7 gives a remainder 5
19 Divided by 8 gives a remainder 3
19 Divided by 9 gives a remainder 1
19 is a Prime numberToCheckber