# How To Calculate Armstrong Number In Java Using Recursion

0
138
• 1
•
•
•
•
•
•
•
•
•
1
Share

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 method goes like—

```public static void main(String[] args)
{
// TODO Auto-generated method stub
ArmstrongNumber a=new ArmstrongNumber();
for(int i=0;i<999;i++)
{
if(isarmstrong(i,(a.getNumber(i,0))))
{
//getNumber method will take the number and pass the initial result as 0
//to the called function
System.out.println("The Armstrong number" +i);
//printing the armstrong number
}
}
}
```

This method will check if the number is really Armstrong Number

```public static boolean isarmstrong(int mainnumber,int calculatedval)
{
// this method will check if the input number and calculated value according
//to armstrong number are same.
if(mainnumber==calculatedval)
return true;
// return true if both are same
else
return false;
//return false if both are unequal
}
```

This method will return the cube of the a given number

```public int cube(int a)
{
// taking a number and returning the cube of it
return a*a*a;
}
```

This is method which will do the main job for us. This methods will be called over and over to get the corresponding digits and their cube

```public int getNumber(int a,int result1)
//this method will take a number and some result as parameter
{
int result = result1;
int b;
b=a/10;
//diving the number by 10
int c=a%10;
//getting the remainder
if(b>0)
// checking if the remaining part is greater than 0
{
result=result+ cube(c);
//getting the cube of the remainder
if(b>=10)
// checking if the remaining part is greater than 10
{
result=getNumber(b,result);
//calling the same function with the remaining part of
//the number after division by 10 and result
}
else
{
//if the result after divided by 10 is less than 10 then making cube of the
//last digit
result= result +cube(b);
}
}
else
{
result=result+ cube(c);
//getting the cube of the remainder
}
return result;
// returning the result
}
```

The entire code goes like-

```public class ArmstrongNumber {
int result=0;
/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
ArmstrongNumber a=new ArmstrongNumber();

for(int i=0;i<999;i++)
{
if(isarmstrong(i,(a.getNumber(i,0))))
{
//getNumber method will take the number and pass the
//initial result as 0 to the called function
System.out.println("The Armstrong number" +i);
//printing the armstrong number
}
}
}
public static boolean isarmstrong(int mainnumber,int calculatedval)
{
// this method will check if the input number and calculated
//value according to armstrong number are same.
if(mainnumber==calculatedval)
return true;
// return true if both are same
else
return false;
//return false if both are unequal
}
public int cube(int a)
{
// taking a number and returning the cube of it
return a*a*a;
}
public int getNumber(int a,int result1)
//this method will take a number and some result as parameter
{
int result = result1;
int b;
b=a/10;
//diving the number by 10
int c=a%10;
//getting the remainder
if(b>0)
// checking if the remaining part is greater than 0
{
result=result cube(c);
//getting the cube of the remainder
if(b>=10)
// checking if the remaining part is greater than 10
{
result=getNumber(b,result);
//calling the same function with the remaining part of the number
//after division by 10 and result
}
else
{
//if the result after divided by 10 is less than 10 then making
// cube of the last digit
result= result +cube(b);
}
}
else
{
result=result +cube(c);
//getting the cube of the remainder
}
return result;
// returning the result
}
}

```

We are done…

The output??

```The Armstrong number0
The Armstrong number1
The Armstrong number153
The Armstrong number370
The Armstrong number371
The Armstrong number407
```
Don't miss out!

Receive top technical news, lesson ideas, travel tips and more!

Give it a try. You can unsubscribe at any time.