Friday, 2 May 2014

Evaluating a Recurrence Relation

A recurrence relation T is defined on n >= 0 and is given as T(n) = T(n-1) + 2*n with the base case T(0) = 1. You will be given one integer k and you have to write a program to find out T(k). The program must implement T( ) recursively.

INPUT:
One integer k.

OUTPUT:
T(k)

CONSTRAINTS:
The input k will satisfy the constraints 0 <= k <= 1000. You do not have to check if the input given to you falls outside this range.

#include<stdio.h>
int t(int x);
int main()
{
int x;
scanf("%d",&x);
printf("%d",t(x));
return 0;
}
int t(int x)
{
if(x==0)
return 1;
else
return (t(x-1) + 2*x);
}

No comments: