Skip to content

Commit d39b04e

Browse files
committed
feat: Check if a number is an Armstrong number with digit-wise cube breakdown
🧠 Logic: - Read integer input from the user. - Store the original number to compare later. - Loop through each digit of the number: - Extract last digit using `% 10`. - Calculate its cube and add to running sum. - Print the digit and its cube for traceability. - After loop, compare sum of cubes with original number: - If equal, it is an Armstrong number. Signed-off-by: Somesh diwan <[email protected]>
1 parent f736005 commit d39b04e

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

Section8LoopAB/src/ArmNumber.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import java.util.Scanner;
2+
3+
public class ArmNumber {
4+
public static void main(String[] args) {
5+
Scanner scanner = new Scanner(System.in);
6+
7+
System.out.print("Enter a number: ");
8+
int number = scanner.nextInt();
9+
10+
int originalNumber = number;
11+
int sum = 0;
12+
13+
System.out.println("\nCalculating sum of cubes of digits:");
14+
System.out.println("-----------------------------------");
15+
16+
while (number > 0) {
17+
int digit = number % 10;
18+
int cube = digit * digit * digit;
19+
20+
System.out.printf("Digit: %d, Cube: %d%n", digit, cube);
21+
22+
sum += cube;
23+
number = number / 10;
24+
}
25+
26+
System.out.println("-----------------------------------");
27+
System.out.println("Total sum: " + sum);
28+
29+
if (sum == originalNumber) {
30+
System.out.println(originalNumber + " is an Armstrong number!");
31+
}
32+
33+
scanner.close();
34+
}
35+
}

0 commit comments

Comments
 (0)