You are here

Armstrong number in Java

Java program to check if a number is Armstrong or not, it is a number that is equal to the sum of digits raise to the power total number of digits in the number. Some of them are: 0, 1, 4, 5, 9, 153, 371, 407, 8208.

Armstrong number program in Java

import java.util.Scanner;
 
class ArmstrongNumber
{
  public static void main(String args[])
  {
    int n, sum = 0, t, remainder, digits = 0;
 
    Scanner in = new Scanner(System.in);
    System.out.println("Input a number to check if it's an Armstrong number");    
    n = in.nextInt();
 
    t = n;

    // Count number of digits

    while (t != 0) {
      digits++;
      t = t/10;
    }
 
    t = n;
 
    while (t != 0) {
      remainder = t%10;
      sum = sum + power(remainder, digits);
      t = t/10;
    }
 
    if (n == sum)
      System.out.println(n + " is an Armstrong number.");
    else
      System.out.println(n + " isn't an Armstrong number.");
  }

  static int power(int n, int r) {
    int c, p = 1;
 
    for (c = 1; c <= r; c++)
      p = p*n;
 
    return p;
  }
}

Output of program:
Armstrong number Java program output

Download Armstrong number program class file.

Using one more loop in the above program, you can generate Armstrong numbers from 1 to n or between two integers, i.e., from a to b where a and b are integers.

Share this