What is the output of the following code? [code] #include<stdio.h> int recursive_sum(int n) { if(n == 0) return 0;

Việt Quân

New member
What is the output of the following code?
Code:
#include<stdio.h>
int recursive_sum(int n)
{
      if(n == 0)
        return 0;
      return n + recursive_sum(n - 1);
}
int main()
{
     int n = -4;
     int ans = recursive_sum(n);
     printf("%d",ans);
     return 0;
}
A. 0
B. -10
C. 1
D. runtime error
 
Answer: D
Explanation: The above code doesn't handle the case of negative numbers and so the function recursive_sum() will be called again and again till the stack overflows and the program produces a runtime error.
 

Tìm kiếm

Back
Top