Armstrong Number

Armstrong Number is a number that equals to the sum of its digits, each raised to a power. Armstrong number was discovered by mathematician Michael Frederick Armstrong (1941-2020) and is also known as narcissistic or Plus Perfect or pluperfect number. Armstrong numbers form the OEIS sequence A005188.

Example :

For one-digit :

1: 1^1 = 1
2: 2^1 = 2
3: 3^1 = 3

So,1,2 & 3 are one-digit Armstrong Number

1, 2, 3, 4, 5, 6, 7, 8, 9 are one-digit Armstrong Number

For three-digits :

153 : 1^3 + 5^3 + 3^3 = 1 + 125+ 27 = 153

So, 153 is a three-digit Armstrong Number

For four-digits :

1634: 1^4 + 6^4 + 3^4 + 4^4 = 1 + 1296 + 81 + 256 = 1643

So, 1634 is a four-digit Armstrong Number

There is no Armstrong Number for two-digit

Armstrong numbers between 0 to 999 are 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407.

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

1) Java Program to prints all the Armstrong numbers up to the specified limit ( i.e Armstrong Number upto 100,500,1000 & so on )

import java.util.Scanner;

public class ArmstrongNumber1 {

	static boolean isArmstrong(int n)   
	{   
	
		int temp, digits=0, last=0, sum=0;   
		temp=n;   
	
		while(temp>0)    
		{   
			temp = temp/10;   
			digits++;   
		}
		temp = n;   
		
		while(temp>0)   
		{   
			last = temp % 10;   
			sum +=  (Math.pow(last, digits));   
			temp = temp/10;   
		}  
		
	
		if(n==sum)   
		return true;      
	
		else return false;   
	}   
	
	public static void main(String args[])     
	{    
			
		int num;   
	
		Scanner sc= new Scanner(System.in);  
		System.out.print("Enter the limit: ");  
		num=sc.nextInt();  
	
		System.out.println("Armstrong Number up to "+ num + " are: ");  
	
		for(int i=0; i<=num; i++)  
	
		if(isArmstrong(i))  
		System.out.print(i+ ", ");  
    }   
	
}    

Output 1 :

Enter the limit: 100
Armstrong Number up to 100 are: 
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 

Output 2 :

Enter the limit: 1000
Armstrong Number up to 1000 are: 
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407,

2) Java program to check if given number is an Armstrong number or not.(Logic 1)

import java.util.Scanner;

public class ArmstrongNumber2 {
	
	
	static boolean isArmstrong(int n)   
	{   
	
		int temp, total_digits=0, last_num=0, total_sum=0;   
		temp=n;   
	
		while(temp>0)    
		{   
			temp = temp/10;   
			total_digits++;   
		}   
	
		temp = n;   
	
		while(temp>0)   
		{   
			last_num = temp % 10;   
			total_sum +=  (Math.pow(last_num, total_digits));   
			temp = temp/10;   
		}  
	
		if(n==total_sum)   
			return true;      
	
		else return false;   
	
	}  
	
	
	public static void  main(String args[])     
	{     
		
		int num_to_check;   
		
		Scanner sc= new Scanner(System.in);  
		System.out.print("Enter the number: ");  
	
		num_to_check=sc.nextInt();  
	
		if(isArmstrong(num_to_check))  
		{  
	
			System.out.print("Entered Number is a Armstrong ");  
		}
		
	
		else   
		{  
			System.out.print("Entered Number is Not a Armstrong ");  
	
		}  
	}   
}  

Output 1:

Enter the number: 5
Entered Number is a Armstrong 

Output 2:

Enter the number: 169
Entered Number is Not a Armstrong 

Output 3:

Enter the number: 8208
Entered Number is a Armstrong

3) Java program to check if given number is an Armstrong number (Logic 2)

import java.util.Scanner;

public class ArmstrongNumber3 {
	int power_to_number(int x, long y)
	{
		if (y == 0)
			return 1;
		if (y % 2 == 0)
			return power_to_number(x, y / 2) * power_to_number(x, y / 2);
		return x * power_to_number(x, y / 2) * power_to_number(x, y / 2);
	}

	int order(int x)
	{
		int n = 0;
		while (x != 0) {
			n++;
			x = x / 10;
		}
		return n;
	}

	boolean isArmstrong(int x)
	{
		int n = order(x);
		int temp = x, total = 0;
		while (temp != 0) {
			int r = temp % 10;
			total = total + power_to_number(r, n);
			temp = temp / 10;
		}

		return (total == x);
	}

	public static void main(String[] args)
	{
		int num_to_check;   
		
		Scanner sc= new Scanner(System.in);  
		System.out.print("Enter the number: ");  
		
		num_to_check=sc.nextInt();  
		ArmstrongNumber3 ob = new ArmstrongNumber3();
		
		if(ob.isArmstrong(num_to_check)) {
			System.out.println("Entered Number is " + num_to_check  + " And is Armstrong");
		}else {
			System.out.println("Entered Number is " + num_to_check  + " And is not a Armstrong");
		}
	}
}


Output 1 :

Enter the number: 36
Entered Number is 36 And is not a Armstrong

Output 2 :

Enter the number: 153
Entered Number is 153 And is Armstrong

4) Java program to check if given number is an Armstrong number (Logic 3)

import java.util.Scanner;

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

        int num_to_check, originalNum, remainder, result = 0, n = 0;
        		
		Scanner sc= new Scanner(System.in);  
		System.out.print("Enter the number: ");  
		
		num_to_check=sc.nextInt();  

		originalNum = num_to_check;

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

        originalNum = num_to_check;

        for (;originalNum != 0; originalNum /= 10)
        {
            remainder = originalNum % 10;
            result += Math.pow(remainder, n);
        }

        if(result == num_to_check)
			System.out.println("Entered Number is " + num_to_check  + " And is Armstrong");
        else
			System.out.println("Entered Number is " + num_to_check  + " And is not Armstrong");
    }
}


Output 1 :

Enter the number: 45
Entered Number is 45 And is not Armstrong

Output 2 :

Enter the number: 370
Entered Number is 370 And is Armstrong
Scroll to Top