# Armstrong number in java – Java Program to check Armstrong number

Armstrong number in java: In the previous article, we have discussed Java Program to Check Tech Number

In this article we will see different ways to check whether a number is Armstrong number or not. But before moving to thee actual concept, let’s first know what is this Armstrong number then we will move to the next part.

## Program to check Armstrong number

Concept:

‘3’ digit number in which cube sum of all it’s digit is equal to the number it self is called as Armstrong number.

For example :

407 is an Armstrong number since (4*4*4)+(0*0*0)+(7*7*7)=407

We will see different ways to check Armstrong number.

So, let’s start one by one.

### Method 1: Checking  Armstrong number using while loop

By using while loop we can check Armstrong number.

Approach:

• Take a number .
• Using while loop Calculate its cube sum of digits .
• Compare with the original number and print according to it .

Program:

import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
// CREATING OBJECT
Scanner sc = new Scanner(System.in);
// TAKING STRING FORM USER
System.out.print("Enter the number : ");
int n= sc.nextInt();
// storing original value in temp variable
int k=n;
int temp=0;
// calculating the cube sum of digit of that number
while(n>0)
{
int a=n%10;
temp=temp+(a*a*a);
n=n/10;
}
// comparing with the original value and printing  it.
if (k==temp)
System.out.print(k + " Is an Armstrong");
else
System.out.print(k+ "Is an Not Armstrong");
}
}


Output:

Enter the number : 407
407 Is an Armstrong

### Method 2: Checking  Armstrong number using for loop

By using for loop we can check Armstrong number.

Approach:

• Take a number .
• Using for loop Calculate its cube sum of digits .
• Compare with the original number and print according to it .

Program:

import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
// CREATING OBJECT
Scanner sc = new Scanner(System.in);
// TAKING STRING FORM USER
System.out.print("Enter the number : ");
int n= sc.nextInt();
// storing original value in temp variable
int k=n;
int temp=0;
// calculating the cube sum of digit of that number
for( ;n!=0;n/=10 )
{
int a=n%10;
temp=temp+(a*a*a);
}
// comparing with the orginal value and printing  it.
if (k==temp)
System.out.print(k + " Is an Amstrong");
else
System.out.print(k+ " Is Not an Amstrong");
}
}

Output:

Enter the number : 407
407 Is an Armstrong

### Method 3: Checking  Armstrong number using bufferedReader

Above two methods we saw using for loop and while loop. But in this we will take user input by using bufferedReader class.

Approach:

• Take a number .
• Using for loop Calculate its cube sum of digits .
• Compare with the original number and print according to it .

Program:

import java.io.*;
public class Main
{
public static void main(String[] args) throws IOException
{
// CREATING OBJECT
// TAKING STRING FORM USER
System.out.print("Enter the number : ");
// storing original value in temp variable
int k=n;
int temp=0;
// calculating the cube sum of digit of that number
for( ;n!=0;n/=10 )
{
int a=n%10;
temp=temp+(a*a*a);
}
// comparing with the orginal value and printing  it.
if (k==temp)
System.out.print(k + " Is an Amstrong");
else
System.out.print(k+ " Is Not an Amstrong");

}
}

Output:

Enter the number : 407
407 Is an Armstrong

### Method 4: Checking  Armstrong number using recursion

By using recursion we can check Armstrong number.

Approach:

• Create a function that will calculate and return the sum of cube of digit .
• Take a number .
• Parse the entered value to that function and store it to a variable .
• Compare with the original number and print according to it .

Program:

import java.util.Scanner;
public class Main
{
int fams(int n,int a)
{   //calculating cube sum of digits
if(n!=0)
{
int x=n%10;
a=a+(x*x*x);
n/=10 ;
return fams(n,a);
}
return a;
}
public static void main(String[] args)
{
// CREATING OBJECT
Scanner sc = new Scanner(System.in);
// TAKING STRING FORM USER
System.out.print("Enter the number : ");
int n= sc.nextInt();
// storing original value in temp variable
int k=n;
// creating the object of main function
Main ams= new Main();
//parsing value to the function
int temp=ams.fams(n,0);
// comparing with the orginal value and printing  it.
if (k==temp)
System.out.print(k + " Is an Amstrong");
else
System.out.print(k+ " Is Not an Amstrong");
}
}

Output:

Enter the number : 407
407 Is an Armstrong

Understand the Programming Language Java and learn the language fastly by using our wide range of Java Programming Examples with Output and try to write programs on your own.

Related Java Decision Making and Loop Programs: