Prime Number Program in Java: This is a simple Java program to find the prime number. A prime number is a number which is greater than 1 and only divisible by 1 or itself.
In the below example, we have shared the first 10 prime number which you can test using below Java program to confirm the Prime number program in Java programming language.
1 | 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 |
In this program, I have taken number
as variable to hold the number which will be checked whether it is a prime number or not.
Copy the below java program and execute it with the help of Java compiler to print the prime number program in java with the output for your better under standing the concept of prime number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public class PrimeNumberExample{ public static void main(String args[]){ int i,m=0,flag=0; int number=11;//it is the number to be checked m=number/2; if(number==0||number==1){ System.out.println(number+" is not prime number"); }else{ for(i=2;i<=m;i++){ if(number%i==0){ System.out.println(number+" is not prime number"); flag=1; break; } } if(flag==0) { System.out.println(number+" is prime number"); } }//end of else } } |
11 is prime number
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.
Article Tags: java prime number, Java prime number program, Java program, Java programs, Java programs with examples, Java programs with output, java tutorial, Java tutorials, java tutorials with examples, prime number program in java