first commit
This commit is contained in:
34
PrimeNo.java
Normal file
34
PrimeNo.java
Normal file
@@ -0,0 +1,34 @@
|
||||
// Program to display first n Prime number (Dynamic Method).
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class PrimeNo {
|
||||
public static void main(String[] args) {
|
||||
int n;
|
||||
int status = 1;
|
||||
int num = 3;
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
System.out.println("Enter the number of Prime numbers to be printed:");
|
||||
n = scanner.nextInt();
|
||||
if (n >= 1) {
|
||||
System.out.println("First "+n+" prime numbers are:");
|
||||
System.out.println(2);
|
||||
}
|
||||
|
||||
for ( int i = 2 ; i <=n ; ) {
|
||||
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++;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user