Repository navigation
Expand file tree
/
Copy pathPrimeNumber.java
More file actions
70 lines (58 loc) · 1.66 KB
/
Copy pathPrimeNumber.java
File metadata and controls
70 lines (58 loc) · 1.66 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package Assignments;
import java.util.Scanner;
public class PrimeNumber {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the Number:");
int n = input.nextInt();
System.out.println(isPrimeNumber(n));
printAllPrimesTillTheNumber(-5);
}
static boolean isPrimeNumber(int number) {
int count = 0;
for(int i = 1; i <= number; i++){
// System.out.println("Count is " + count + " and i value is " + i);
if (number % i == 0){
// System.out.println("i value is " + i);
count ++;
}
if (count >= 3){
// System.out.println("Exited the loop with count: " + count);
break;
}
}
if(count == 2){
// System.out.println("YES");
return true;
}else{
// System.out.println("NO");
return false;
}
}
static boolean isPrime(int n){
if (n <= 1){
return false;
}
int c = 2;
while (c * c <= n){
if (n % c == 0){
return false;
}
c++;
}
return c * c > n;
}
static void printAllPrimesTillTheNumber(int number){
int count = 0;
for (int i = 1; i < number; i++) {
boolean isPrime = isPrimeNumber(i);
if (isPrime){
count++;
System.out.print(i + " ");
}
}
if (count == 0){
System.out.println("There are no prime numbers less than n");
}
}
}