Assignment: Module 03- Part 01
![]() |
Assignment: Module 03- Part 01 |
05. Write a program to find the Armstrong number using control structures.
e.g (1*1*1)+(5*5*5*)+(3*3*3) = 153
Input : 153
Output: It is a Armstrong number
Input: 210
Output: It is not a Armstrong number
Here is a Java program that finds whether a given number is an Armstrong number or not:
Copy code:
import java.util.Scanner;
public class ArmstrongNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int originalNum = num;
int result = 0;
int n = 0;
// Counting number of digits
for (; num != 0; n++) {
num /= 10;
}
num = originalNum;
for (; num != 0; num /= 10) {
int digit = num % 10;
result += Math.pow(digit, n);
}
if (result == originalNum) {
System.out.println(originalNum + " is an Armstrong number.");
} else {
System.out.println(originalNum + " is not an Armstrong number.");
}
}
}
You can run this program and input the number you want to check. The program will then output whether the number is an Armstrong number or not.