this is code of armstrong no. by take something return something method but errors are coming please suggest ways to correct it | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

this is code of armstrong no. by take something return something method but errors are coming please suggest ways to correct it

#include <stdio.h> int arm(int); int main(){ int n,A; printf("enter no"); scanf("%d",&n); A=arm(n); if(A==n) printf ("armstrong no."); else printf ("not armstrong no."); getchar();} int arm (int y) { int temp,r,sum=0; temp=y; while(y!=0){ r=y%10; sum=sum+r*r*r; y=y/10; } return(y);}

14th Jul 2020, 9:11 PM
unnati priya
unnati priya - avatar
2 Answers
+ 1
Return <sum> instead of <y> But I think `sum = sum + r * r " r` is only good when input given is a 3-digit number. If the number has different number of digit, you need to count the digit of number <y> (let's say <digits>), then multiply each digit <r> by <digits>.
14th Jul 2020, 9:46 PM
Ipang
0
unnati priya Nothing wrong in your logic Only thing you have to do return sum instead of y. And also why temp variable ,no need of it.
15th Jul 2020, 3:36 AM
uday kiran
uday kiran - avatar