tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Java > Core > Special Numbers > Armstrong Number

Armstrong Number 

An Armstrong number has sum of its digits raised to the power of number of digits equals the number. i.e. An n-digit number equal to the sum of the nth powers of its digits. The following example shows how to check whether the given number is Armstrong.

File Name  :  
com/bethecoder/tutorials/core/special_numbers/ArmstrongNumber.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.core.special_numbers;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * An Armstrong number has sum of digits raised to the power of number of digits 
 * equals the number
 
 @author Sudhakar KV
 *
 */
public class ArmstrongNumber {

  /**
   @param args
   */
  public static void main(String[] args) {
    isArmstrong(2);
    isArmstrong(6);
    isArmstrong(153);
    isArmstrong(371);
    isArmstrong(407);
    isArmstrong(1634);
    
    System.out.println();
    
    isArmstrong(60);
    isArmstrong(138);
  }

  private static int getNumOfDigits(int number) {
    return String.valueOf(number).length();
  }
  
  private static boolean isArmstrong(int number) {
    int orgNumber = number;
    int sum = 0;
    int digit = 0;
    int digitPow = 0;
    int numOfDigits = getNumOfDigits(number);
    List<Integer> digits = new ArrayList<Integer>();
    List<Integer> digitPows = new ArrayList<Integer>();
    
    while (number > 0) {
      digit = number % 10;
      digitPow = (intMath.pow(digit, numOfDigits);
      sum += digitPow;
      number = number /10;
      
      digits.add(digit);
      digitPows.add(digitPow);
    }
    
    boolean armstrong = (sum == orgNumber);
    System.out.println(orgNumber + " is " 
        (armstrong ? "an armstrong number" "not an armstrong number"
        " " + getSum(digits, digitPows, numOfDigits, sum));
    return armstrong;
  }
  
  private static String getSum(List<Integer> digits, 
      List<Integer> digitPows, int numOfDigits, int sum) {
    
    Collections.reverse(digits);
    Collections.reverse(digitPows);
    StringBuilder sb = new StringBuilder("[ ");
    
    for (int i = ; i < digits.size(); i ++) {
      
      if (i > 0) {
        sb.append(" + ");
      }
      
      sb.append(digits.get(i"^" + numOfDigits);
    }
    
    sb.append(" = ");
    
    for (int i = ; i < digitPows.size(); i ++) {
      
      if (i > 0) {
        sb.append(" + ");
      }
      
      sb.append(digitPows.get(i));
    }

    sb.append(" = ").append(sum).append(" ]");
    return sb.toString();
  }
}
   

It gives the following output,
2 is an armstrong number [ 2^1 = 2 = 2 ]
6 is an armstrong number [ 6^1 = 6 = 6 ]
153 is an armstrong number [ 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153 ]
371 is an armstrong number [ 3^3 + 7^3 + 1^3 = 27 + 343 + 1 = 371 ]
407 is an armstrong number [ 4^3 + 0^3 + 7^3 = 64 + 0 + 343 = 407 ]
1634 is an armstrong number [ 1^4 + 6^4 + 3^4 + 4^4 = 1 + 1296 + 81 + 256 = 1634 ]

60 is not an armstrong number [ 6^2 + 0^2 = 36 + 0 = 36 ]
138 is not an armstrong number [ 1^3 + 3^3 + 8^3 = 1 + 27 + 512 = 540 ]



 
  


  
bl  br