Prime Number Java Program – Java Program to Check Whether a Number is Prime or Not using different methods. The compiler has also been added so that you can execute the programs yourself, alongside suitable examples and sample outputs added for each program. Also, we will see the method of how to find out prime numbers between 1 to n, 1 to 100 The methods used aforementioned are:
- Using Static Method
- Using While Loop
- Using For Loop
- Using Recursion
A PRIME NUMBER is any number that is divisible by 1 and itself only. Certain examples of prime numbers are 2, 3, 5, 7, 11 etc. However, 1 is neither a prime nor composite number.
Using Static Method
1) A prime number is a number which has no positive divisors other than 1 and itself.
2) We are finding the given number is prime or not using the static method primeCal(int num). For loop iterates from i=0 to i=given number, if the remainder of number/i =0 then increases the count by 1. After all the iterations, if count=2, then that number is a prime number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import java.util.Scanner; class Prime { public static void main(String arg[]) { System.out.println("Enter a number "); Scanner sc=new Scanner(System.in); int n=sc.nextInt(); primeCal(n); } static void primeCal(int num) { int count=0; for(int i=1;i<=num;i++) { if(num%i==0) { count++; } } if(count==2) System.out.println("prime number "); else System.out.println("Not a prime number "); } } |
Output:
1 2 3 |
Enter a number 7 prime number |
Find Prime Numbers Between 1 to n
1) We are finding the prime numbers within the limit.
2) Read the “n” value using scanner object sc.nextInt()and store it in the variable n.
3) The for loop iterates from j=2 to j=given number. then count assigned to 0, the inner loop finds the divisors of each j value, count value represents no.of divisors. If count=2, then that number is a prime number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.util.Scanner; class Prime { public static void main(String arg[]) { int i,count; System.out.print("Enter n value : "); Scanner sc=new Scanner(System.in); int n=sc.nextInt(); System.out.println("Prime numbers between 1 to "+n+" are "); for(int j=2;j<=n;j++) { count=0; for(i=1;i<=j;i++) { if(j%i==0) { count++; } } if(count==2) System.out.print(j+" "); } } } |
Output:
1 2 3 4 |
Enter n value : 100 Prime numbers between 1 to 100 are 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 |
1 2 3 |
Enter n value : 25 Prime numbers between 1 to 25 are 2 3 5 7 11 13 17 19 23 |
1 2 3 |
Enter n value : 50 Prime numbers between 1 to 25 are 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 |
Prime Number Java Program – Using While Loop
1) In this program, the while loop is present in the constructor. If we instantiate the class then automatically constructor will be executed.
2) Read the “n” value using scanner class object sc.nextInt(). FindPrime class is initiated in the class Prime as new FindPrime(n); then the constructor of FindPrime will be executed.
The while loop iterates until i<=num is false. The remainder of number/i=0 then count will be increased by 1, “i” value increased by 1. If count=2, then print “number is a prime number”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import java.util.Scanner; class FindPrime { FindPrime(int num) { int count=0,i=1; while(i<=num) { if(num%i==0) { count++; } i++; } if(count==2) System.out.println(num+" is a prime number "); else System.out.println(num+" is a Not a prime number "); } } class Prime { public static void main(String arg[]) { System.out.println("Enter a number "); Scanner sc=new Scanner(System.in); int n=sc.nextInt(); new FindPrime(n); } } |
Output:
1 2 3 |
Enter a number 97 97 is a prime number |
1 2 3 |
Enter a number 93 93 is a Not a prime number |
Using For Loop
1) To find divisors of the given number
- for loop iterates from i=1 to n.
- If remainder of n,i is 0 then count value increased by 1. Count represents total no of divisors.
- if count=2 then the given number is prime.
2) Example n=53, i=1, 53%1=0,count=1: i=2,53%2=1!=0, count=1:i=3, 53%3=2!=0: repeat until i=53 ,53%53=0,count=2. Count=2 so 53 is prime number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.Scanner; class Prime { public static void main(String arg[]) { int count=0; System.out.println("Enter a number "); Scanner sc=new Scanner(System.in); int n=sc.nextInt(); for(int i=1;i<=n;i++) { if(n%i==0) { count++; } } if(count==2) System.out.println("prime number "); else System.out.println("Not a prime number "); } } |
Output:
1 2 3 |
Enter a number 57 Not a prime number |
Using Recursion
1) Read the entered number n.
2) The object for Prime class will be created in the main method. Call the method primeOrNot(n) using the object as p.primeOrNot(n);
3) The method primeOrNot(int num) will be executed and calls itself as primeOrNot(num); until the condition if(i<=num) is false. If the condition is false then this method returns the count and assigned to the variable c. If count=2, then print “prime number” otherwise print “not a prime number”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import java.util.Scanner; class Prime { static int count=0,i=1; int primeOrNot(int num) { if(i<=num) { if(num%i==0) { count++; } i++; primeOrNot(num); } return count; } public static void main(String arg[]) { System.out.println("Enter a number "); Scanner sc=new Scanner(System.in); int n=sc.nextInt(); Prime p=new Prime(); int c=p.primeOrNot(n); if(c==2) System.out.println("prime number "); else System.out.println("Not a prime number "); } } |
Output:
1 2 3 |
Enter a number 9 Not a prime number |
More Java Programs: