Skip to content

Commit c5f9e54

Browse files
committed
feat: Add FindPrimeNumber class with prime check using loop
WHAT the code does: - Defines `FindPrimeNumber` class with static method `isPrime(int n)`. - `isPrime()`: - Loops from `2` to `n/2 - 1`. - If any number divides `n`, returns `false`. - Otherwise returns `true`. - `main()`: - Uses `Scanner` to read an integer from the user. - Calls `isPrime()` and prints `true` if prime, `false` otherwise. WHY this matters: - Demonstrates a classic algorithm for **prime number detection**. - Combines user input handling with logic-based computation. - Introduces conditionals and loops for factor checking. HOW it works: 1. User enters a number (e.g., `19`). 2. Loop checks divisibility from `2` up to `n/2`. 3. If divisor found, immediately returns `false`. 4. If no divisor found, returns `true`. Tips & gotchas: - Algorithm has inefficiency: - Checking up to `n/2` is unnecessary; prime check only requires testing divisors up to `√n`. - Edge cases not handled: - `n <= 1` should return `false` (not prime). - `n = 2` or `n = 3` should return `true`, but loop may fail correctly for `2`. - Always close `Scanner` (`scanner.close()`). - Output is just `true`/`false` → could be more user-friendly messages. Use-cases: - Educational demo for prime-checking logic. - Good interview practice for optimizing loop bounds (`√n` check). - Foundation for prime-related problems (prime factors, sieve of Eratosthenes). - Useful for competitive programming warm-ups. Short key: algo-prime-check-basic-loop. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 280e692 commit c5f9e54

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
/* A prime number is a positive integer having exactly two factors, i.e. 1 and the number itself.
22
If p is a prime, then its only factors are necessarily 1 and p itself.
3-
Any number that does not follow this is termed a composite number, which can be factored into other positive integers.*/
3+
Any number that does not follow this is termed a composite number, which can be factored into other positive integers.
4+
*/
45

56
import java.util.Scanner;
67

7-
public class FindPrimeNumber
8-
{
8+
public class FindPrimeNumber {
99
static boolean isPrime(int n)
10+
//true or false. initialize the method.
1011
{
11-
for(int i=2; i<n/2;i++)
12+
for(int i = 2; i<n/2; i++) //assign a value of i=2 and increment by 1 everytime when loop is running.
1213
{
13-
if(n%i==0)
14+
if(n % i == 0) //means n has a factor other than 1 and itself → not prime.
1415
return false;
15-
}
16-
return true;
16+
} return true;
1717
}
18-
public static void main(String[] args)
19-
{
20-
18+
public static void main(String[] args) {
2119
Scanner scanner = new Scanner(System.in);
2220
System.out.println("Enter a number to check prime or not: ");
21+
2322
System.out.println(isPrime(scanner.nextInt()));
2423
/* System.out.println(isPrime((19)));*/
2524
}
26-
}
25+
}

0 commit comments

Comments
 (0)