Armstrong number is nothing but the sum of its digits each raised to the power of the number of digits. Before Michael F. Armstrong named it Armstrong number, it was known as Narcissistic number. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474,… this is a series of the Armstrong numbers. Now let’s find out whether a given number is an Armstrong number or not in this Armstrong Number in java article.
Table of Contents
How to identify an Armstrong number
If the sum of the digits each of them raised to the power of the number of digits is equal to the original number then it is called an Armstrong number. Let’s understand the logic behind this with an example given below.
Logic in Armstrong number
Suppose,
PQSR is a integer then,
PQRS = Pn+ Qn+Rn+Sn
Where n is the digits count.
If PQRS is equal to the Pn+ Qn+Rn+Sn then, it is an Armstrong number.
Example 1
Input number = 146
1³ + 4³ + 6³ = 1*1*1 + 4*4*4 + 6*6*6 = 281
Hence, it is not an armstrong number
Example 2
Input number = 1634
14 + 64 + 34 + 44 = 1*1*1 + 6*6*6 + 3*3*3 + 4*4*4 = 1634
Hence, it is an Armstrong number
Armstrong number program in java
Here we have given four programs for finding Armstrong number like using for loop, recursive function, while loop, and in between the range.
Armstrong number in java using while loop
Example
import java.util.Scanner;
public class armstrong_number {
public static void main(String[] args) {
int number =0;
int result = 0, rem; //variables for calculation
int x;
Scanner object = new Scanner(System.in); //using scanner class for accepting input from user
System.out.println("Input a positive integer");
number = object.nextInt();
x = number;
while (x!= 0) // loop with exit condition n is not equal to 0
{
rem = x% 10;
result = result + rem*rem*rem; // logic for finding armstrong number
x = x/10;
}
if(number == result) //check if original number and addition is same or not
System.out.print("This is an Armstrong number\n");
else
System.out.print("This is not an Armstrong number\n");
}
}
Output
Enter a number:370
This is an Armstrong number
Enter a number:20
This is an Armstrong number
Armstrong number in java using recursive function
Example
import java.util.Scanner;
public class armstrong_number {
int find(int num, int total) //static function for identifying armstrong number
{
int n;
while(num!=0)
{
n=num%10;
total=total+(n*n*n); //logic for armstrong number
num/=10 ;
}
return total;
}
public static void main(String[] args) {
int n1, n2;
int result = 0;
Scanner sc = new Scanner(System.in); // using scanner class for accepting number from user
System.out.print("Enter starting of range:");
n1 = sc.nextInt();
System.out.print("Enter ending of range:");
n2 = sc.nextInt();
armstrong_number arm = new armstrong_number();
for(int i=n1;i<n2;i++) // loop for travalling from starting point to ending point
{
result = arm.find(i,0);
if(result == i)
System.out.println(i);
}
}
}
Output
Enter starting of range:100
Enter ending of range:1000
153
370
371
407
Print Armstrong number in between n to m range
Example
import java.util.Scanner;
public class armstrong_number {
static int arm(int num) //static function for identifying armstrong number
{
int n,total=0;
while(num!=0)
{
n=num%10;
total=total+(n*n*n); //logic for armstrong number
num/=10 ;
}
return total;
}
public static void main(String[] args) {
int n1, n2;
int result = 0;
Scanner sc = new Scanner(System.in); // using scanner class for accepting number from user
System.out.print("Enter starting of range:");
n1 = sc.nextInt();
System.out.print("Enter ending of range:");
n2 = sc.nextInt();
while(n1<n2) // loop for finding armstrong numbers in igiven range
{
result = arm(n1); //call to static function
if(result == n1)
System.out.println(n1); //prints the armstrong number
n1++;
}
}
}
Output
Enter starting of range:100
Enter ending of range:1000
153
370
371
407
Armstrong number in java using for loop
Example
import java.util.Scanner;
public class armstrong_number {
public static void main(String[] args) {
int num=0;
int n, remainder, result = 0, i = 0;
Scanner sc = new Scanner(System.in); // using scanner class for accepting number from user
System.out.print("Enter a number : ");
num = sc.nextInt();
n = num;
for (;n != 0; n /= 10)
{
i++;
}
n = num;
for (;n != 0; n /= 10)
{
remainder = n % 10; //logic for armstrong number
result += Math.pow(remainder, i);
}
if(result == num) // check if the sum of cubes of each digits is equal to the number
Systemout.println("Given number is an Armstrong number");.
else
System.out.println("Given number is not an Armstrong number");
}
}
Output
Enter a number:370
Given number is an Armstrong number
Enter a number:134
Given number is not an Armstrong number