Skip to content

Commit f05b5ec

Browse files
committed
feat: Add Prime class to check whether a number is prime using optimized loop
Add a program that: - Accepts an integer input from the user - Handles edge cases like numbers ≤ 1 - Uses an optimized loop to check divisibility up to √n - Prints whether the number is Prime or Not Prime - Demonstrates control flow, loop efficiency, and conditional logic in Java.
1 parent a6557c6 commit f05b5ec

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

Section5OperatorExpression/src/Prime.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ public class Prime {
44
public static void main(String[] args) {
55
Scanner in = new Scanner(System.in);
66
System.out.print("Please enter a number: ");
7+
78
int n = in.nextInt();
9+
810
if(n <= 1) {
911
System.out.println("Neither prime nor composite");
1012
return;
1113
}
14+
1215
int c = 2;
1316
while (c * c <= n) {
1417
if (n % c == 0) {
@@ -21,6 +24,5 @@ public static void main(String[] args) {
2124
if (c * c > n) {
2225
System.out.println("Prime");
2326
}
24-
2527
}
2628
}

0 commit comments

Comments
 (0)