A Platform to LEARN | CODE | ACHIEVE
Connect :
Code
#include<stdio.h>
int main()
{
int num;
int rem=0,sum=0,n=0;
printf("ENTER THE NUMBER : ");
scanf("%d",#);
n=num;
while(n>0)
{
rem=n%10;
sum=sum+rem*rem*rem;
n=n/10;
}
if(sum==num)
{
printf("IT IS AN ARMSTRONG NUMBER \n");
}
else
{
printf("IT IS NOT AN ARMSTRONG NUMBER \n");
}
return 0;
}
Output
ENTER THE NUMBER : 153
IT IS AN ARMSTRONG NUMBER
What we did - Our Approach
In this problem we have to check whether a given number is an armstrong number or not.An Armstrong number is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3*3*3 + 7*7*7 + 1*1*1 = 371.
To do so we declare a variable 'sum',which will store the result value(don't forget to initialize it with zero).Then,we update our 'sum' value by adding cube of remainder value(rem*rem*rem) to it in each iteration(until 'n' becomes 0).
After the last iteration,we compare the value of given number and our sum ,if both values are equal then given number is armstrong else it isn't.
See this code in :
Also see :
Participate in our MONTHLY CHALLENGE,and get a chance to get listed on our site and win the TITLE of "MR.GEEK".
Join us :
Connect :
SHARE:
For any Queries,suggestions,feel free to Mail us at :
[email protected]
For reporting any kind of copyright voilation,mail us at : [email protected]
© copyright CODE OF GEEKS