Skip to main content

Check Armstrong

You are given an integer 'n'.

Return 'true' if 'n' is an Armstrong number, and 'false' otherwise.


An Armstrong number is a number (with 'k' digits) such that the sum of its digits raised to 'kth' power is equal to the number itself. 
For example, 371 is an Armstrong number because 3^3 + 7^3 + 1^3 = 371.

bool checkArmstrong(int n){
    //Write your code here
    int num=0,temp;
    int count=0;
    for(int i=n;i>0;i=i/10)
     count++;
    for(int i=n;i>0;i=i/10)
    {
        temp=i%10;
        num=num+pow(temp,count);
    }
   if(num==n)
    return true;
   else
    return false;
}

Comments

Popular posts from this blog

Sum of All Divisors from 1 to N

Introduction In this blog, we will discuss the problem of finding the sum of all divisors from 1 to N. This problem is an observation-based problem with a tint of simple mathematics. We will explore different approaches and algorithms to solve this problem efficiently. Understanding the Problem The problem statement is self-explanatory. Given a number N, we need to find the sum of all its divisors from 1 to N. Brute Force Approach Initially, we can think of solving this problem using a brute force approach. We can iterate from 1 to N and check if each number is a divisor of N. If it is a divisor, we add it to the sum. However, this approach is not optimal for large values of N. Optimized Approach To optimize the solution, we can use a contribution technique. Instead of finding all the divisors individually, we can find the contribution of each number in the sum. Let's understand this technique with an example: For N = 8: 1 can contribute to all numbers from 1 to 8 2 can contribut...