Skip to content

Commit 069f47e

Browse files
author
codehouseindia
authored
Merge pull request codehouseindia#109 from TharinduRewatha/patch-1
Show_PrimeNumbers.java
2 parents fdaa5a6 + a08f32f commit 069f47e

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

Show_PrimeNumbers.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
}

0 commit comments

Comments
 (0)