Prem Tiwari - - Java Programming
This is a very simple Java program to print first 10 prime numbers. If you are a Java beginners or want to start learning Java programming language, then this program will help you to understand the basic Java programming.
In this post, I will explain about a Java program to print first 10 prime numbers with an example and output.
Before executing this program I assume you have configured Java in your computer. Copy the below program and create a file “PrimeNumberProgram.java” and execute it to print first 10 prime numbers.
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 | class PrimeNumberProgram { public static void main(String args[]) { int n; int status = 1; int num = 3; System.out.println("First 10 prime numbers are:"); System.out.println(2); for ( int i = 2 ; i <=10 ; ) { for ( int j = 2 ; j <= Math.sqrt(num) ; j++ ) { if ( num%j == 0 ) { status = 0; break; } } if ( status != 0 ) { System.out.println(num); i++; } status = 1; num++; } } } |
First 10 prime numbers are:
2
3
5
7
11
13
17
19
23
29
Liked this tutorial? Do Like & share with your friends 🙂
Prem Tiwari is the founder of FreeWebMentor.com and also a professional developer who has vast experience in PHP and open source technologies. Apart from this, he is a blogger by hobby and also he has been a regular speaker of WordPress sessions in various IT Companies. View all posts by Prem Tiwari
Tags: java prime number, Java prime number program, Java program, Java programming, Java programs, Java programs with examples, Java programs with output, prime number program in java