Armstrong Number in Java

An Armstrong number is a number that is equal to the sum of its own digits raised to the power of the number of digits. For example, 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153.

Here’s a Java program to check if a given number is an Armstrong number or not:

  import java.util.Scanner;

public class ArmstrongNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
int originalNumber, remainder, result = 0, n = 0;
originalNumber = number;

  // Count number of digits
  while (originalNumber != 0) {
     originalNumber /= 10;
     ++n;
  }

  originalNumber = number;

  // Calculate sum of nth power of digits
  while (originalNumber != 0) {
     remainder = originalNumber % 10;
     result += Math.pow(remainder, n);
     originalNumber /= 10;
  }

  // Check if number is an Armstrong number
  if (result == number)
     System.out.println(number + " is an Armstrong number.");
  else
     System.out.println(number + " is not an Armstrong number.");
}
}


This program takes a number as input from the user, counts the number of digits in the number, calculates the sum of nth power of each digit, and checks if the resulting sum is equal to the original number.