Check Armstrong Number
This program checks if a number is Armstrong using while loop.
Problem Statement
Write a Java program that checks if 153 is an Armstrong number using while loop.
Source Code
| 1 | public class ArmstrongCheck { |
| 2 | public static void main(String[] args) { |
| 3 | int num = 153, temp = num, sum = 0; |
| 4 | while(temp > 0) { |
| 5 | int digit = temp % 10; |
| 6 | sum += digit*digit*digit; |
| 7 | temp /= 10; |
| 8 | } |
| 9 | if(sum == num) { |
| 10 | System.out.println(num + " is Armstrong - Eduinq"); |
| 11 | } else { |
| 12 | System.out.println(num + " is not Armstrong - Eduinq"); |
| 13 | } |
| 14 | } |
| 15 | } |
Program Output
153 is Armstrong - Eduinq
Explanation
The program extracts digits, cubes them, and adds to sum. - After processing all digits, it checks if sum equals original number. - This demonstrates how while loops can check Armstrong numbers. Conclusion: Shows how while loop can check Armstrong numbers.