An Armstrong number of three digits is an integer such that the sum of the cubes
ID: 3928540 • Letter: A
Question
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 numberExplanation / Answer
#include <stdio.h>
int armstrong(int n)
{
int temp=n, rem, res = 0;
while (temp != 0)
{
rem = temp%10;
res += rem*rem*rem;
temp /= 10;
}
if(res == n)
return 1;
else
return 0;
}
int main(void) {
int n,res=0;
printf("Enter three digit integer: ");
scanf("%d", &n);
if(n<=99 && n>=1000){
printf("Enter three digit integer: ");
scanf("%d", &n);
}
res=armstrong(n);
printf("%d",res);
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.