0

Why this code return "0" as result?

#include <stdio.h> #include <float.h> int a(int b){ int c; while (b > 0){ return b + a(b - 1); } } int main() { printf("%i",a(10)); return 0; }

15th Aug 2020, 1:42 PM
Phyo Htet Aung
Phyo Htet Aung - avatar
3 Answers
+ 2
It returns 0 as an exit status meaning no errors have occured. (plus its "int main" you need to return an integer) If there is a problem, it will exit with anything but 0, usually 1.
15th Aug 2020, 1:43 PM
Slick
Slick - avatar
0
I write a recursive function with while statement instead of if ...else statement.But the result is not I expect.Why is this?
15th Aug 2020, 1:46 PM
Phyo Htet Aung
Phyo Htet Aung - avatar
0
The result of the code is 55 which is summation of values 10+9+8+...+1.
15th Aug 2020, 1:48 PM
Avinesh
Avinesh - avatar