-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindAllPrimeNumer2.java
More file actions
44 lines (41 loc) · 974 Bytes
/
findAllPrimeNumer2.java
File metadata and controls
44 lines (41 loc) · 974 Bytes
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
Q2.3 Take a Number N from the user as input and find all prime numbers upto N?
*/
package nazimacadviw;
import java.util.Scanner;
/**
*
* @author Nazim
*/
public class findAllPrimeNumer2
{
public static void main (String[] args)
{
Scanner scanner = new Scanner(System.in);
int i =0;
int num =0;
//Empty String
String primeNumbers = "";
System.out.println("Enter the value of n:");
int n = scanner.nextInt();
// scanner.close();
for (i = 1; i <= n; i++)
{
int counter=0;
for(num =i; num>=1; num--)
{
if(i%num==0)
{
counter = counter + 1;
}
}
if (counter ==2)
{
//Appended the Prime number to the String
primeNumbers = primeNumbers + i + " ";
}
}
System.out.println("Prime numbers from 1 to"+ n +"are :");
System.out.println(primeNumbers);
}
}