Skip to content
Forum Navigation
You need to log in to create posts and topics.

Coding & Programming

Armstrong number

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 number = sc.nextInt();
sc.close();

int originalNumber = number;
int numDigits = (int) Math.log10(number) + 1;
int sum = 0;

while (number > 0) {
int digit = number % 10;
sum += Math.pow(digit, numDigits);
number /= 10;
}

if (sum == originalNumber) {
System.out.println(originalNumber + " is an Armstrong number.");
} else {
System.out.println(originalNumber + " is not an Armstrong number.");
}
}
}

Suniti Singh has reacted to this post.
Suniti Singh