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 33 + 73 + 13 = 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:

I have done the main code but I need to use int armstrong(int *n); as a function

please modify my code so that it use int armstrong(int *n); as a function

// Armstrong number
#include<stdio.h>
// main function
int main (){
//declare vribales
int x, sum=0, a, remainder;
printf(\"Enter a positive integer:\ \");
scanf(\"%d\",&x);
a=x;
while(a!=0){remainder=a%10;sum=sum+remainder*remainder*remainder;a=a/10;}
//display results
if(sum==x){printf(\"%d is an Armstrong number\ \",x);}
else{printf(\"%d not is an Armstrong number\ \",x);}return 0;
}

Solution

#Program Code:

// Armstrong number
#include<stdio.h>
// main function

int armstrong(int *n);    //function declaration

int main (){
//declare vribales
int x,r,a;
printf(\"Enter a positive integer:\ \");
scanf(\"%d\",&x);                               //reading input number
a=x;
r=armstrong(&x);                           //passing input number to arm strong function as a pointer (as per your requirment

if(r==0){printf(\"%d is an Armstrong number\ \",a);}           //Function return value is 0 it is armstrong number
else{printf(\"%d not is an Armstrong number\ \",a);}return 0;       //Function return value is it is not armstrong number
}
// Function Defination of armstrong //
int armstrong(int *a)
{

int b,sum=0,remainder,r;
b=*a;

   while((*a)!=0){remainder=(*a)%10;sum=sum+remainder*remainder*remainder;*a=(*a)/10;}

   // return values


   if(sum==b)
   {
       return 0;
   }
   else
   {
       return 1;
   }

}

Input :

Enter a positive integer:                                                                                                                                                

371                      

Output :

371 is an Armstrong number

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 n
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 n

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site