Please show how the following C program output is 15 include

Please show how the following C program output is 15

#include <stdio.h> int recFunc (int n) {
int result;

if ( n == 0 )
result = 0;
else
result = n + recFunc(n - 1); return result;

}

int main (void)
{ printf(\"%i\",recFunc(5));

return 0; }

Solution

Answer: 15

This program will calculate the sum of numbers staring fron 0 up to 5.

we are calling function recFunc() with value 5.

This is a recursive function and it will call itself until n ==0.

This statement will execute until n value is 0.

result = n + recFunc(n - 1); return result;

It will calculate like this

5 + 4 + 3 + 2 + 1 = 15

Please show how the following C program output is 15 #include <stdio.h> int recFunc (int n) { int result; if ( n == 0 ) result = 0; else result = n + recF

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site