CodersZone logo

Armstrong Number

An Armstrong number is a positive m-digit number that is equal to the sum of the m th powers of their digits. It is also known as pluperfect, or Plus Perfect, or Narcissistic number.
We need two parameters to implement armstrong numbers. One is the number of digits in the number, and second the sum of the digits raised to the power of a number of digits.
Let’s understand it through an example:

1: 11 = 1
2: 21 = 2
3: 31 = 3
153: 13 + 53 + 33 = 1 + 125+ 27 = 153
125: 13 + 23 + 53 = 1 + 8 + 125 = 134 (Not an Armstrong Number)
1634: 14 + 64 + 34 + 44 = 1 + 1296 + 81 + 256 = 1643

Similarly, we can check other number also.
The first few Armstrong numbers between 0 to 999 are 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407. Some other Armstrong numbers are 1634, 8208, 9474, 54748, 92727, 93084, 548834, 1741725, 4210818, 9800817, 9926315, 24678050, 24678051, 88593477, 146511208, 472335975, 534494836, 912985153, 4679307774, 32164049650, 32164049651.

Algorithm

  1. The number of digits in num is found out
  2. The individual digits are obtained by performing num mod 10, where the mod is the remainder operation.
  3. The digit is raised to the power of the number of digits and stored
  4. Then the number is divided by 10 to obtain the second digit.
  5. Steps 2, 3 and 4 are repeated until the value of num is greater than 0
  6. Once num is less than 0, end the while loop
  7. Check if the sum obtained is same as the original number
  8. If yes, then the number is an armstrong number

Code

    import java.util.Scanner;  
    import java.lang.Math;  
	public class ArmstsrongNumberExample  
	{  
	//function to check if the number is Armstrong or not  
	static boolean isArmstrong(int n)   
	{   
	int temp, digits=0, last=0, sum=0;   
	//assigning n into a temp variable  
	temp=n;   
	//loop execute until the condition becomes false  
	while(temp>0)    
	{   
	temp = temp/10;   
	digits++;   
	}   
	temp = n;   
	while(temp>0)   
	{   
	//determines the last digit from the number      
	last = temp % 10;   
	//calculates the power of a number up to digit times and add the resultant to the sum variable  
	sum +=  (Math.pow(last, digits));   
	//removes the last digit   
	temp = temp/10;   
	}  
	//compares the sum with n  
	if(n==sum)   
	//returns if sum and n are equal  
	return true;      
	//returns false if sum and n are not equal  
	else return false;   
	}   
	//driver code  
	public static void main(String args[])     
	{     
	int num;   
	Scanner sc= new Scanner(System.in);  
	System.out.print("Enter the limit: ");  
	//reads the limit from the user  
	num=sc.nextInt();  
	System.out.println("Armstrong Number up to "+ num + " are: ");  
	for(int i=0; i<=num; i++)  
	//function calling  
	if(isArmstrong(i))  
    //prints the armstrong numbers  
	System.out.print(i+ ", ");  
	}   
	}  

            

Output

Enter a number: 153
153 is an Armstrong number
 
See the same program in C See the same program in Python