An Armstrong number of three digits is an integer such that
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. For example, 371 is an Armstrong number since 3^3 + 7^3 + 1^3 = 371. Write a program to prompt the user for a three digit positive integer n and output whether the number is an Armstrong number or not. Write a function called armstrong which returns 0 if the number is an armstrong number and 1 if it is not an armstrong number. The following function prototype is required: int armstrong(int *n); See the sample output as follows: Enter a positive integer: 371 371 is an Armstrong number
Solution
#include <stdio.h>
int armstrong(int *n)
{
int m = *n,t;
int ans=0;
while(m>0)
{
t = m%10;
ans += t*t*t;
m = m/10;
}
if(ans==*n){return 1;}
return 0;
}
int main()
{
int n=372;
int a = armstrong(&n);
printf(\"%d\ \",a);
return 0;
}
