Please use comments Write a C program and test a recursive f

Please use comments.

Write a C program and test a recursive function that returns the value of the followinf fecursive definition:

what set of numbers is generated by this definition?

Solution

#include<stdio.h>

int f(int x)
{
//If x lest than or equal to zero than return 0
if(x<=0)
return 0;
//else add 2 to the previous value
else
return f(x-1)+2;
}

int main()
{
int x = 10;
int i;
  
printf(\"Below are the set of numbers generated by f(x) : \ \");
for(i=0;i<=10;i++)
{
printf(\"f(%d) = %d\ \",i,f(i));
}
return 0;
}

OUTPUT:

Below are the set of numbers generated by f(x) :
f(0) = 0
f(1) = 2
f(2) = 4
f(3) = 6
f(4) = 8
f(5) = 10
f(6) = 12
f(7) = 14
f(8) = 16
f(9) = 18
f(10) = 20

By seeing the output it generates the set of all the multiples of 2.

Please use comments. Write a C program and test a recursive function that returns the value of the followinf fecursive definition: what set of numbers is genera

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site