Skip to content

Commit 0e677d1

Browse files
committed
Solution to print prime numbers till a number
1 parent 363a4d0 commit 0e677d1

1 file changed

Lines changed: 22 additions & 5 deletions

File tree

‎src/Assignments/PrimeNumber.java‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,29 @@ public static void main(String[] args) {
99
System.out.println("Enter the Number:");
1010
int n = input.nextInt();
1111
System.out.println(isPrimeNumber(n));
12+
printAllPrimesTillTheNumber(-5);
1213
}
1314

1415
static boolean isPrimeNumber(int number) {
1516
int count = 0;
1617

1718
for(int i = 1; i <= number; i++){
18-
System.out.println("Count is " + count + " and i value is " + i);
19+
// System.out.println("Count is " + count + " and i value is " + i);
1920
if (number % i == 0){
20-
System.out.println("i value is " + i);
21+
// System.out.println("i value is " + i);
2122
count ++;
2223
}
2324

2425
if (count >= 3){
25-
System.out.println("Exited the loop with count: " + count);
26+
// System.out.println("Exited the loop with count: " + count);
2627
break;
2728
}
2829
}
2930
if(count == 2){
30-
System.out.println("YES");
31+
// System.out.println("YES");
3132
return true;
3233
}else{
33-
System.out.println("NO");
34+
// System.out.println("NO");
3435
return false;
3536
}
3637
}
@@ -50,4 +51,20 @@ static boolean isPrime(int n){
5051
}
5152
return c * c > n;
5253
}
54+
55+
static void printAllPrimesTillTheNumber(int number){
56+
57+
int count = 0;
58+
for (int i = 1; i < number; i++) {
59+
60+
boolean isPrime = isPrimeNumber(i);
61+
if (isPrime){
62+
count++;
63+
System.out.print(i + " ");
64+
}
65+
}
66+
if (count == 0){
67+
System.out.println("There are no prime numbers less than n");
68+
}
69+
}
5370
}

0 commit comments

Comments
 (0)