In this java program, you’ll learn how to check if the given number is an Armstrong number or not. In this program, we used the following java basics such as for
loop, while
loop, and if..else
condition.
An Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits.
For Example, 153 is an Armstrong number.
(1)3 + (5)3 + (3)3= 153.
Here is the code of the program to check the given number is Armstrong Number or Not:
//Java Program to Check Armstrong Number
import java.util.Scanner;
public class JavaPrograms {
public static void main(String[] args) {
int number, originalNumber, remainder, result = 0, n = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a Number: ");
number = sc.nextInt();
originalNumber = number;
for (; originalNumber != 0; originalNumber /= 10, ++n)
;
originalNumber = number;
for (; originalNumber != 0; originalNumber /= 10) {
remainder = originalNumber % 10;
result += Math.pow(remainder, n);
}
if (result == number)
System.out.println(number + " is an Armstrong number.");
else
System.out.println(number + " is not an Armstrong number.");
}
}
Enter a Number:
153
153 is an Armstrong number.
Enter a Number:
156
156 is not an Armstrong number.
Comments