Armstrong Number  

Difficulty Level Medium
Frequently asked in Oracle VMware
Math

Problem Statement  

Armstrong NumberPin
Armstrong Number

Let a number x containing n digits. If sum of each digit raised to power n is equal to the number itself, then the number is said to be armstrong number i.e.

abc… = pow(a, n) + pow(b, n) + pow(c, n) + ……

Write a program to check Armstrong Number  

Example

153
Yes

Explanation: 13 + 53 + 33

= 1 + 125 + 27

Please click Like if you loved this article?

=153  Therefore, 153 is an armstrong number.

1634
Yes
123
No

Algorithm

1. Initialize an integer variable n.
2. Create a function to check the armstrong number which accepts an integer variable as it's parameter.
3. Find the length of the number.
4. Iterate while the number is greater than 0, mod number with 10. Find the digit's power n and add to sum. Divide the number by 10.
5. Check if sum and original number are equal or not.
6. If yes, Print "Yes".
7. Else print "No".

Complexity Analysis

Time Complexity:

O(log(n)*log(log(n)) where n is the number of digits in the given integer variable because we are finding exponentiation for all the digits of numbers.

Space Complexity:

O(1) because we used constant space.

C++ program

#include<bits/stdc++.h>
using namespace std;

int expo(int a, int b){
    int res = 1;
    while(b>0){
        if(b&1)
            res = res*a;
        a = a*a;
        b = b>>1;
    }
    return res;
}

bool checkArmstrong(int x){
    int l = 0, n = x;
    while(n){
        l++;
        n = n/10;
    }
    int sum=0;
    n = x;
    while(n){
        int digit = n%10;
        sum += expo(digit, l);
        n = n/10;
    }
    return(sum == x);
}

int main(){
    int n = 153;
    if(checkArmstrong(n))
        cout<<"Yes";
    else
        cout<<"No";
    return 0;
}
Yes

Java program

import java.lang.Math;
class Armstrong{ 
    
    boolean checkArmstrong(int x){ 
        int l = 0, n = x; 
        while(n!=0){ 
            l++; 
            n = n/10; 
        }
        int sum=0;
        int num = x;
        while(num!=0){ 
            int digit = num%10; 
            sum += Math.pow(digit, l); 
            num = num/10; 
        }
        return(sum == x); 
    } 
  
    public static void main(String[] args){ 
        Armstrong a = new Armstrong(); 
        int n = 153; 
        if(a.checkArmstrong(n))
            System.out.println("Yes");
        else
            System.out.println("No");
    } 
}
Yes

Write a program to find n’th armstrong number.  

Example:

11
370

Explanation: 11th Armstrong number is 370.

15
8208

Algorithm

1. Initialise an integer variable n.

See also
Palindrome Number

2. Create a function to check the armstrong number which accepts an integer variable as it’s parameter.

3. Iterate over an integer stating from 1. Mod number with 10 to get the last digit. Find the digit’s power n and add to sum.

4. Divide the number by 10.

5. Check if sum is equal to current iterating number, increment the count.

6. If count is equal to n, return current iterating number.

Time Complexity:

O(log(n)*log(log(n)) where n is the number of digits in the given integer variable.

Please click Like if you loved this article?

Space Complexity:

O(1) because we used constant extra space.

C++ program

#include<bits/stdc++.h>
using namespace std;

int expo(int a, int b){
    int res = 1;
    while(b>0){
        if(b&1)
            res = res*a;
        a = a*a;
        b = b>>1;
    }
    return res;
}

int nthArmstrong(int n){
    int count=0;

    for(int i=1; i<=INT_MAX; i++){
        int num=i, rem, digit=0, sum=0;

        digit =(int)log10(num)+1;

        while(num>0){
            rem = num % 10;
            sum = sum + expo(rem,digit);
            num = num / 10;
        }
        if(i == sum)
            count++;
        if(count==n)
            return i;
    }
}

int main(){
    int n = 15;
    cout<<nthArmstrong(n);
    return 0;
}
8208

Java program

import java.lang.Math; 
  
class Armstrong{ 
      
    static int nthArmstrong(int n){ 
        int count = 0; 
          
        for(int i=1; i<=Integer.MAX_VALUE; i++){ 
            int num = i, rem, digit = 0, sum = 0; 
              
            digit = (int) Math.log10(num) + 1; 
              
            while(num > 0){  
                rem = num % 10; 
                sum = sum + (int)Math.pow(rem, digit); 
                num = num / 10; 
            } 
              
            if(i == sum) 
                count++; 
            if(count == n) 
                return i; 
        } 
        return n; 
    } 
      
    public static void main(String[] args){ 
        int n = 15; 
        System.out.println(nthArmstrong(n)); 
    } 
}
8208

 

Please click Like if you loved this article?

2