# Armstrong number in java: Algorithm and programs

#### What is Armstrong number in java?

A number is said to be Armstrong if its sum of cubes of its digits equals the number is called an Armstrong number in java.

for example:-

0, 1, 153, 370, 371,etc.

let’s take a number 153

-> (1*1*1) + (5*5*5) +(3*3*3)

-> 1 + 125 + 27

-> 153

so it is Armstrong number.

#### Algorithm

```START
Step 1 → Take an integer a=0,temp,sum
Step 2 → initilized temp with n
Step 3 → using while loop where n>0
Step 4 → take an integer r
Step 5 → initilized r with n%10;
Step 6 → initilized n with n/10
Step 7 → initilized a=a+(r*r*r)
Step 8 → if temp==a then it is an armstrong
Step 9 → else it is not an armstrong
STOP```

#### The  in java using a while loop

class Armstrong{

public static void main(String[] args)  {

int s=0,r,temp;

int n=370;//It is the number to check armstrong

temp=n;

while(n>0)

{

r=n%10;

n=n/10;

s=s+(a*a*a);

}

if(temp==s)

System.out.println(“it is an armstrong number”);

else

System.out.println(“it is not an armstrong number”);

}

}

#### Output

it is an Armstrong number

#### The Armstrong number in java using a for loop

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

int n = 370, on, r, s = 0, n = 0;

on = n;

for (;on != 0; on /= 10, ++n);

{

on = n;

}

for (;on != 0; on /= 10)
{
r = on % 10;
s += Math.pow(r, n);
}

if(s == n)
System.out.println(n + " is an Armstrong number.");
else
System.out.println(n + " is not an Armstrong number.");
}
}
```

#### Output

`370 is an Armstrong number`

#### The Armstrong number in java using a scanner

import java.util.Scanner;
public class JavaExample {

public static void main(String[] args) {

int n, r, temp, s = 0;
System.out.println(“Ënter Number”);
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
scanner.close();
r = n;

for( ;n!=0;n /= 10)
{
temp = n % 10;
s = s + (temp*temp*temp);
}

if(s == n)
System.out.println(n + ” is an Armstrong number”);
else
System.out.println(n + ” is not an Armstrong number”);
}
}

#### Output

Ënter Number
370
370 is an Armstrong number

#### Click here for more questions in java

String Palindrome in java: Algorithm and programs