Armstrong Numbers | Practice | GeeksforGeeks

For a given 3 digit number, find whether it is armstrong number or not. An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. Return "Yes" if it is a armstrong number else return "No".
NOTE: 371 is an Armstrong number since 33 + 73 + 13 = 371

Example 1:
Input: N = 153
Output: "Yes"
Explanation: 153 is an Armstrong number
since 13 + 53 + 33 = 153.
Hence answer is "Yes".

Example 2:
Input: N = 370
Output: "Yes"
Explanation: 370 is an Armstrong number
since 33 + 73 + 03 = 370.
Hence answer is "Yes".

Your Task:  
You dont need to read input or print anything. Complete the function armstrongNumber() which takes n as input parameter and returns "Yes" if it is a armstrong number else returns "No"..

Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)

Constraints:
100 ≤ n <1000
0
aishnamdev13in 4 hours
//Java Solution
class Solution {   static String armstrongNumber(int n){       int temp = n;    int arm = 0;    while(n!=0) {        int rem = n%10;        arm += Math.pow(rem,3);         n = n/10;    }    if(arm == temp) {        return "Yes";    }    else {        return "No";    }   }}
0
mayank180919992 days ago
  string armstrongNumber(int n){
        // code here
        int digit;
        int sum=0;
        int val=n;
        while(n!=0){
            digit=n%10;
            sum+=pow(digit,3);
            n=n/10;
        }
        if(sum==val){
            return "Yes"; 
        }
     return "No"; 
    }
0
gfgcode10104 days ago
class Solution {   static String armstrongNumber(int n){       // code here        int temp = n;       int r,sum = 0;              while(n>0){                      r = n%10;           n = n/10;           sum = sum + (r*r*r);       }              if(temp==sum){           return "Yes";       }       else{           return "No";       }   }}
0
selibrshaman5 days ago
java passed cases: 33/33if (Math.pow((n%1000-n%100)/100, 3)+Math.pow((n%100-n%10)/10, 3)+Math.pow(n%10, 3)==n) return "Yes";       else return "No";
0
navhayer991 week ago
class Solution {   static String armstrongNumber(int n){       // code here       int rem, result=0;       int temp=n;
         while(temp!=0){           rem=temp%10;           result+=Math.pow(rem,n);                      temp/=10;       }       if(result==n)           return "Yes";           else               return "No";                  }       }
0
rsbly7300951 week ago
class Solution{
    
    armstrongNumber(n){
        let sum = 0;
        for(let item of n.toString()){
            sum += item*item*item;
           
        } 
        
        if(sum == n){
            return "Yes";
        }
        else return "No";
    }
}
0
abhisri19971 week ago
armstrongNumber(n){
        const unitArr = n.toString().match(/[0-9]/g);
        let sum = 0;
        unitArr.forEach(val => {
            sum += Number(val) ** 3;
        })
        
        return sum === n ? "Yes" : "No";
    }
Easiest way to achieve this without any mathematical converting of hundred, thousands, unit places.
0
convertedengineer2 weeks ago
int sum=0;
        int originaln=n;
        while(n>0){
            int lastDigit=n%10;
            sum+=pow(lastDigit,3);
            n=n/10;
        }
        if(sum==originaln){
            return "Yes";
        }
        else{
            return "No";
        }
+1
nallalasumanthreddy3 weeks ago
UPVOTE PYTHON SOLUTION
class Solution:
    def armstrongNumber (ob, n):
        a=0
        for i in str(n):
            a+=int(i)**3
        if a==n:
            return ("Yes")
        else:
            return ("No")
0
nyapparel17723 weeks ago
static boolean checkIfArmStrong(int n) { int res=0; // get how many digits  int inputToCount = n; int len=0; int inputNum = n; int currentDigit=0; while(inputToCount > 0) {  inputToCount /=10;  len++; } for(int i=1; i<=len; i++) {  currentDigit = inputNum%10;  res += Math.pow(currentDigit, 3);  inputNum = inputNum/10; } return (res == n) ? true : false;}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.