File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ //https://www.facebook.com/permalink.php?story_fbid=2750473708542571&id=100007399066161
2+ Subscribed by tharindu Rewatha
3+
4+ class PrimeNumberDemo
5+ {
6+ public static void main (String args [])
7+ {
8+ int n ;
9+ int status = 1 ;
10+ int num = 3 ;
11+ //For capturing the value of n
12+ Scanner scanner = new Scanner (System .in );
13+ System .out .println ("Enter the value of n:" );
14+ //The entered value is stored in the var n
15+ n = scanner .nextInt ();
16+ if (n >= 1 )
17+ {
18+ System .out .println ("First " +n +" prime numbers are:" );
19+ //2 is a known prime number
20+ System .out .println (2 );
21+ }
22+
23+ for ( int i = 2 ; i <=n ; )
24+ {
25+ for ( int j = 2 ; j <= Math .sqrt (num ) ; j ++ )
26+ {
27+ if ( num %j == 0 )
28+ {
29+ status = 0 ;
30+ break ;
31+ }
32+ }
33+ if ( status != 0 )
34+ {
35+ System .out .println (num );
36+ i ++;
37+ }
38+ status = 1 ;
39+ num ++;
40+ }
41+ }
42+ }
You can’t perform that action at this time.
0 commit comments