Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

An Armstrong number of three digits is an integer such that the sum of the cubes

ID: 3929674 • 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 number

Explanation / Answer

#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;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote