What is going to be the output of the following program incl

What is going to be the output of the following program: #include int recFunc (int n){int result; if(n == 0) result = 0; else result = 2 * n + recFunc (n -1); return result;} int main (void) {printf(\"%i\", recFunc (6)); return 0;} Output:

Solution

#include<stdio.h>

int recFunc(int n)
{
   int result;

   if(n==0)
       result=0;
   else
   {
       result=2*n+recFunc(n-1);

   }
   return result;
}
int main()
{
   printf(\"%d\ \", recFunc(6));
   return 0;
}

===================================

Output:

42

Explanation:This function calculates the 2*n+2*(n-1)+2(n-3)+...........+2*1+2*0

If we put n=6,

2*6+2*5+2*4+2*3+2*2+2*1+2*0=42

 What is going to be the output of the following program: #include int recFunc (int n){int result; if(n == 0) result = 0; else result = 2 * n + recFunc (n -1);

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site