Check Prime Numbers in Java Programming
Here, we have listed How To Check Prime Numbers in Java Programming Language using For Loop. We have also listed How To Find Prime Numbers till a Range(Limit) in Java.
What is a Prime Number?
A Prime Number is a Natural Number Greater than 1 and it should have No Positive Divisors other than 1and the Number itself. A Natural Number greater than 1 but not a Prime-Number is known as a Composite Number.
Example
2, 3, 5, 17 ,19
These numbers are evenly divided by 1 and the number itself only.
Program To Check Prime Numbers in Java Programming
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.io.*; import java.util.*; class PrimeNumbers { public static void main(String args[]) { int count = 2, num; Scanner sc = new Scanner(System.in); System.out.println("nEnter A Number:"); num = sc.nextInt(); for(count = 2;count <= num - 1;count++) { if(num%count == 0) { System.out.println(num + " is not a Prime Numbern"); break; } } if(count == num) { System.out.println(num + " is a Prime Numbern"); } } } |
Also Read: Java Program To Find Perfect Numbers
Output

Code To Find Prime Numbers from 1 To N in Java Programming
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 | import java.io.*; import java.util.*; class PrimeNumbers { public static void main(String args[]) { int count = 2, i, limit; Scanner sc = new Scanner(System.in); System.out.println("nEnter A Limit:"); limit = sc.nextInt(); System.out.println("nResultsn"); for(i = 0;i <= limit;i++) { for(count = 2;count <= i-1;count++) { if(i%count == 0) { System.out.println(i + " is not a Prime Numbern"); break; } } if(count == i) { System.out.println(i + " is a Prime Numbern"); } } } } |
Also Read: Convert Decimal Integer To Binary Java Program
Output

To know more details about Prime Numbers, Refer FactMonster. In case you get any Compilation Errors with this Java Program To Check Prime Integers or you have any doubt about it, mention it in the Comment Section.