Monday, July 23, 2012

Java Program to find Armstrong numbers - Example Code

Armstrong number Example in Java
How to check if a number is Armstrong number or not or write a Java program to find Armstrong number is common Java  interview question asked on campus interviews or fresher level interview. This is also a popular Java programming exercise on various school and colleges. An Armstrong number is a 3 digit number for which sum of cube of its digit is equal to the number. Example of Armstrong number is 153 as 153= 1+ 125+27 which 1^3+5^3+3^3. Another Armstrong number is 371. In this Java program example we will see complete code example of Java program to check if any 3 digit number is Armstrong number or not. If you are going for Java interview, then be prepare for some follow question e.g. finding prime numbers,  finding Armstrong number of more than 3 digit or calculating factorial of a number using recursion in Java.

By the way this Java program is in continuation of our earlier programming exercise like  
Java program to reverse String in Java,  
Java program to reverse number in Java and 
Java program to print Fibonacci number in Java

If you haven't read them already you may find them useful as programming exercise or interview question.

How to check if number is Armstrong number or not in Java

Armstrong number example in Java programHere is complete code for checking if a number is Armstrong number or not. It uses a method called isArmstrong(int number) to implement logic for checking number as Armstrong.

import java.util.Scanner;

/**
 * Simple Java Program to check or find if a number is Armstrong number or not.
 * An Armstrong number of three digit is a number whose sum of cubes of its digit is equal
 * to its number. For example 153 is an Armstrong number of 3 digit because 1^3+5^3+3^3 or   1+125+27=153
 * @author Javin
 */

public class ArmstrongTest{

   
    public static void main(String args[]) {
   
        //input number to check if its Armstrong number
        System.out.println("Please enter a 3 digit number to find if its an Armstrong number:");
        int number = new Scanner(System.in).nextInt();
     
        //printing result
        if(isArmStrong(number)){
            System.out.println("Number : " + number + " is an Armstrong number");
        }else{
            System.out.println("Number : " + number + " is not an Armstrong number");
        }

   
    }

    /*
     * @return true if number is Armstrong number or return false
     */

    private static boolean isArmStrong(int number) {
        int result = 0;
        int orig = number;
        while(number != 0){
            int remainder = number%10;
            result = result + remainder*remainder*remainder;
            number = number/10;
        }
        //number is Armstrong return true
        if(orig == result){
            return true;
        }
     
        return false;
    }
 
}

Output:
Please enter a 3 digit number to find if its an Armstrong number:
153
Number : 153 is an Armstrong number
Please enter a 3 digit number to find if its an Armstrong number:
153
Number : 153 is an Armstrong number
Please enter a 3 digit number to find if its an Armstrong number:
371
Number : 371 is an Armstrong number

That's all on How to check if a number is Armstrong or not. It’s pretty simple Java program and if you look closely it just gets digit by digit by using remainder operator and reduce number by 1 digit after dividing it by 10. Let me know if you find any bug on this Java program for checking Armstrong number.

Other Java programming tutorial you may like :
Java program to reverse number in Java
Write a Java program to check if number is palindrome or not
Write a Java program to find GCD of two numbers in Java
Write a Java program to read text file in Java
How to find square root of a number in Java

6 comments:

  1. Good tutorial, How to find Armstrong number with more than 3 digits ?

    ReplyDelete
    Replies
    1. /*
      Simple Java Program to check or find if a number is Armstrong number or not.
      [ An Armstrong number of any number of digit is a number whose sum of each digits raised to the power the total no. of digits is equal to it (the original number). For example, 9474 is an Armstrong number of 4 digits because 9^4 + 4^4 + 7^4 + 4^4 = 6561 + 256 + 2401 = 9474 ]
      */

      import java.io.*;
      import java.util.*;

      public class ArmstrongNumber
      {
      public static void main(String args[])
      {

      // Input number to check if its Armstrong number
      System.out.println("Please enter a number to find if it is an Armstrong number: ");
      int number = new Scanner(System.in).nextInt();

      // Printing result
      if(isArmStrong(number))
      {
      System.out.println(number + " is an Armstrong number");
      }
      else
      {
      System.out.println(number + " is not an Armstrong number");
      }

      } // End of main()

      // Function returning true if number is Armstrong number or returning false

      private static boolean isArmStrong(int number)
      {
      int result = 0;
      int dc=0;
      int orig = number;
      while(orig != 0)
      {
      dc++;
      orig = orig/10;
      }
      orig = number;
      while (orig!=0)
      {
      result = result + (int)Math.pow(orig%10, dc);
      orig = orig/10;
      }
      if(number == result)
      return true;
      else
      return false;
      } // End of isArmStrong()
      }

      OUTPUT:
      Please enter a number to find if it is an Armstrong number:
      9474
      9474 is an Armstrong number

      Please enter a number to find if it is an Armstrong number:
      8208
      8208 is an Armstrong number

      Delete
  2. According to Abhirupa Mitra's code numbers from 1 to 9 would be displayed as Armstrong numbers which is not correct. What is the correct definition of Armstrong numbers? Within 1 to 10 only 1 is an armstrong number.

    ReplyDelete
  3. The definition of an Armstrong Number was not that clear to me. I looked up here and figured out what it is: http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap04/arms.html

    ReplyDelete
  4. I'd probably do it like this:

    public boolean isArmstrong(int i) {
    return (i >= 100) && (i <= 999) && (sumCubeDigits(i) == i);
    }

    private int sumCubeDigits(int digits) {
    if (digits == 0)
    return 0;
    else
    return cube(digits % 10) + sumCubeDigits(digits / 10);
    }

    private int cube(int i) {
    return i * i * i;
    }

    and if it turned out to be too slow, like this:

    public boolean isArmstrong(int i) {
    if ((i < 100) || (i > 999)) {
    return false;
    } else {
    int a = i % 10;
    int b = (i / 10) % 10;
    int c = i / 100;
    int sumOfCubes = (a*a*a) + (b*b*b) + (c*c*c);
    return (i == sumOfCubes);
    }
    }

    ReplyDelete
  5. I'd do it like this....

    public static void armstrong(int number){
    String numToString = String.valueOf(number);
    int number1;
    int multiplication = 0;
    for (int i=0; i<=numToString.length()-1; i++){
    number1 = Character.getNumericValue(numToString.charAt(i));
    multiplication = multiplication + number1 * number1 * number1;
    }
    if (number == multiplication) {
    System.out.println("Number : " + number + " is an Armstrong number");
    } else {
    System.out.println("Number : " + number + " is not an Armstrong number");
    }
    }

    ReplyDelete

Java67 Headline Animator