-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsPrime.java
More file actions
29 lines (25 loc) · 723 Bytes
/
Copy pathIsPrime.java
File metadata and controls
29 lines (25 loc) · 723 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
//Write a program to display PRIME NUMBERS from 1 to n?
import java.util.Scanner;
public class IsPrime {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("enter range: ");
int n=sc.nextInt(); //1 is not a prime no. since a no. should be divided by min of 2 numbers
for(int i=2;i<=n;i++) //range of numbers
{
boolean prime = true;
for(int j=2;j<=i/2;j++) //range of divisors
{ //the numbers could be divided by 1 or itself and not any other number
if(i%j==0)
{ //so the divisor starts from 2 to n-1
prime=false;
}
}
if(prime==true)
{
System.out.print(i+" ");
}
}
sc.close();
}
}