Skip to content

Commit ac2fd3b

Browse files
committed
feat: Add Questions2 class with prime and Armstrong number checks
WHAT the code does: - Defines a `Questions2` class that checks whether a given number (`num = 153`) is: 1. **Prime** → has no divisors except 1 and itself. 2. **Armstrong** → sum of digits raised to the power of number of digits equals the number itself. - Implements both checks without external methods, directly inside `main()`. WHY this matters: - Demonstrates fundamental **number theory problems** in Java. - Reinforces logic building with loops, conditions, and arithmetic operations. - Avoids using `Math.pow` by implementing power calculation manually, showing low-level control. HOW it works: 1. **Prime check**: - If `num <= 1` → not prime. - Otherwise, loops from `2` to `num/2` checking divisibility. - If divisible, sets `isPrime = false` and breaks loop. - Prints `"Prime"` if no divisors found. 2. **Armstrong check**: - Counts number of digits. - For each digit, calculates `digit^digits` by repeated multiplication. - Sums powered digits and compares with original number. - Prints `"Armstrong"` if equal. Tips & gotchas: - Prime check could be optimized to loop only till `√num` instead of `num/2`. - Armstrong logic works for any digit length, not just 3-digit numbers. - If `num = 1`, code prints nothing (not prime, not Armstrong). - Using manual power calculation avoids floating-point issues from `Math.pow`. Use-cases: - Classic **interview practice problems**. - Educational demo for beginners learning loops and conditionals. - Basis for building utility functions for prime/Armstrong checks. - Can be extended to list all primes or Armstrong numbers in a range. Short key: algo-prime-armstrong-inline. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 5ca41ba commit ac2fd3b

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
public class Questions2 {
2+
public static void main(String[] args) {
3+
int num = 153; // number to check
4+
5+
// ---------------- Prime Check ----------------
6+
boolean isPrime = true;
7+
if (num <= 1) {
8+
isPrime = false;
9+
} else {
10+
for (int i = 2; i <= num / 2; i++) {
11+
if (num % i == 0) {
12+
isPrime = false;
13+
break;
14+
}
15+
}
16+
}
17+
if (isPrime) {
18+
System.out.println("Prime");
19+
}
20+
21+
// ---------------- Armstrong Check ----------------
22+
int original = num;
23+
int sum = 0, temp = num, digits = 0;
24+
25+
while (temp > 0) {
26+
digits++;
27+
temp /= 10;
28+
}
29+
30+
temp = num;
31+
while (temp > 0) {
32+
int digit = temp % 10;
33+
int power = 1;
34+
35+
// multiply digit "digits" times (instead of Math.pow)
36+
for (int i = 0; i < digits; i++) {
37+
power *= digit;
38+
}
39+
40+
sum += power;
41+
temp /= 10;
42+
}
43+
if (sum == original) {
44+
System.out.println("Armstrong");
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)