Problem no 4: Armstrong number.

 QUESTION:

 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


ANSWER:


string armstrongNumber(int n){
        int x=n;
        int result=0;
        while(n>0)
        {
           
            int rem =n%10;
            result=result+pow(rem,3);
            n=n/10;
        }
        if(x==result)
        {
            return "Yes";
        }
        else
        {
            return "No";
        }
    }

Comments

Popular posts from this blog

problem no 18: number of buildings that can see the sun . ( or facing the sun.)

problem 3: given two integers N and M. The problem is to find the number closest to N and divisible by M. If there are more than one such number, then output the one having maximum absolute value.

problem no 7:Given two numbers A and B, find Kth digit from right of AB.