Uva 12895 - Armstrong Number

/*

One kind of very easy Number theory problem i have ever seen !!!
To find Armstrong Numbers just do summation among digits where all digits of a number is exponent by numbers of digits.
That means,  Summation = 1st_number ^ total_digit + 2nd_number ^ total_digit + 3rd_number ^ total_digit + ...... + nth_numbers ^ total_digit.
If the Summation is equal to the  main Number then, the number is called Armstrong Number.
Let consider Number = 153, then the total_digit = 3;
Summation = 1^3 + 5^3 + 3^3 = 153; so, Summation=Number, Hence the Number is an Armstrong Number.
*/

// Verdict : Accepted
// Time :: 0.000

#include<bits/stdc++.h>

#define sf scanf
#define pf printf
using namespace std;

typedef long long LL;

bool isArmstrong(LL n)
{
    int digit=0;
    LL tmp = n, to_check=n;

    while(n)
    {
        n/=10;
        digit++;
    }

    LL sum=0,mul=1;
    int mdo;

    while(tmp)
    {
        mdo = tmp%10;
        mul=1;

        for(int i=0;i<digit;i++)
        {
            mul*=mdo;
        }

        sum+=mul;
        tmp/=10;
    }

    if(sum == to_check)
    {
        return true;
    }

    else
    {
        return false;
    }
}

int main()
{
    int test;
    sf("%d",&test);

    while(test--)
    {
        LL num;
        sf("%lld",&num);

        if(isArmstrong(num))
        {
            pf("Armstrong\n");
        }

        else
        {
            pf("Not Armstrong\n");
        }
    }

    return 0;
}

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LightOJ 1009 - Back to Underworld

LeetCode Palindrome Number