Commit 5ca41ba
committed
feat: Add Questions class with prime check and Armstrong number listing
WHAT the code does:
- Defines a `Questions` class for practicing number-related problems.
- Prompts user for input and checks if the number is prime (`isPrime()`).
- Prints all 3-digit Armstrong numbers (100–999) using `isArmstrong()`.
WHY this matters:
- Demonstrates **user input handling** with `Scanner`.
- Shows **prime number check** using efficient loop up to √n.
- Implements **Armstrong number check** for 3-digit numbers.
- Useful educational example combining loops, conditionals, and modular arithmetic.
HOW it works:
1. `isPrime(int n)`:
- Returns false if `n <= 1`.
- Loops from `2` to `√n`.
- If divisor found, returns false; otherwise true.
2. `isArmstrong(int n)`:
- Extracts digits of `n`.
- Cubes each digit and sums them.
- Returns true if sum equals original number.
3. `main()`:
- Takes a number from user input and prints prime status.
- Iterates from 100–999 and prints Armstrong numbers.
Tips & gotchas:
- `Scanner` should be closed after use to prevent resource leaks (`in.close()`).
- Prime check optimized with `c * c <= n` → faster than looping to `n/2`.
- `isArmstrong()` works only for 3-digit numbers; for n-digit, use `(int)Math.pow(rem, digits)`.
- Input validation for non-integer entries not handled (will throw exception).
Use-cases:
- Competitive programming or coding interviews.
- Teaching examples for **loops, conditionals, math operations**.
- Can be extended to find primes/Armstrong numbers in any range.
- Base utility for number theory problems.
Short key: algo-prime-armstrong-check.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 04c8e35 commit 5ca41ba
1 file changed
+7
-4
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
7 | | - | |
8 | | - | |
| 6 | + | |
9 | 7 | | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
10 | 13 | | |
11 | 14 | | |
12 | 15 | | |
| |||
24 | 27 | | |
25 | 28 | | |
26 | 29 | | |
27 | | - | |
28 | 30 | | |
29 | 31 | | |
30 | 32 | | |
| 33 | + | |
31 | 34 | | |
32 | 35 | | |
33 | 36 | | |
| |||
0 commit comments