# How To Calculate Armstrong Number In Java Without Using Recursion

March 18, 2013
In my previous post here I have shown how to calculate Armstrong number using recursion technique.

In this post Devopriya also tried the same thing using only while condition. This is done not using recursion Technique.

An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371.

My objective is to find out all the Armstrong Number between 1-999

The main function is pretty similar

Let us look at the total class implementation:

The output??

In this post Devopriya also tried the same thing using only while condition. This is done not using recursion Technique.

An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371.

My objective is to find out all the Armstrong Number between 1-999

The main function is pretty similar

public static void main(String[] args) { // TODO Auto-generated method stub arms ab=new arms(); //I have given the class name as arms for(int i=0;i<999;i++ ) { if(isarmstrong(i,(ab.getNumber(i)))) //getNumber is the function which will get a number and calculate the value as //per Armstrong principal { System.out.println("The Armstrong number is" +i); } }The supporting functions are ...

- isaermstrong
- cube

public static boolean isarmstrong(int mainnumber,int calculatedval) // this method will take the main number and the calculated value obtained from //getNumber and will determine if they are same. { if(mainnumber==calculatedval) return true; else return false; }The cube method will look like-

public int cube(int a) //takes some integer value { return a*a*a; // returns the cube value for that number }Now lets look the getNumber function this is the heart of this method

public int getNumber(int a) { result=0; int b; while(a>0) { b=a%10; //get the remainder result=result +cube(b); //add the cube of the remainder to result a=a/10; //divide the number by 10 } return result; }

Let us look at the total class implementation:

public class arms { static int result=0; /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub arms ab=new arms(); for(int i=0;i<3000;i ++ ) { if(isarmstrong(i,(ab.getNumber(i)))) { System.out.println("The Armstrong number is"+ i); } } } public static boolean isarmstrong(int mainnumber,int calculatedval) { if(mainnumber==calculatedval) return true; else return false; } public int cube(int a) { return a*a*a; } public int getNumber(int a) { result=0; int b; while(a>0) { b=a%10; result=result +cube(b); a=a/10; } return result; } }Let me know if you like this concept.

The output??

The Armstrong number0 The Armstrong number1 The Armstrong number153 The Armstrong number370 The Armstrong number371 The Armstrong number407

**Author Description:**Devopriya Chatterjee is a Software Engineer. She has written series of Javacode to automate applications. and She is a regular guest blogger of AskQTP. |

## 0 comments