0

What is wrong with this C program?

I wrote the following C program to find whether a no. is perfect no. but when i run it the compiler crashes and gives msg as test.exe has stopped working. test.c is my file name btw. why? my code:- void perfect(int x); int main() { int x; scanf("%d",&x); perfect(x); return 0; } void perfect(int x) { int i,s=0; for(i=0;i<x;i++) { int r=x%i; if(r==0) s+=i; } if(s==x) printf("Number is perfect"); else printf("Number not perfect"); }

15th Sep 2017, 4:37 AM
Shantanu Shinde
Shantanu Shinde - avatar
4 Antworten
+ 2
I found it ! You did r % i when i == 0, which is a runtime error
15th Sep 2017, 4:55 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 1
000.damn. thanks
15th Sep 2017, 4:58 AM
Shantanu Shinde
Shantanu Shinde - avatar
0
i have typed that in compiler. here i just posted the main code.
15th Sep 2017, 4:54 AM
Shantanu Shinde
Shantanu Shinde - avatar
- 1
There should be a #include <stdio.h> but without it, it shouldn't even compile so I guess you just forget to copy past it :/
15th Sep 2017, 4:42 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar