0% found this document useful (0 votes)
6 views1 page

Recursive Functions: Factorial & Fibonacci

This document contains sample code for two recursive functions: one to compute the sum of the first n natural numbers using recursion, and another to calculate the nth Fibonacci number using recursion. The code includes functions to add numbers recursively and find the Fibonacci number at a given index recursively.

Uploaded by

kssmanikanta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views1 page

Recursive Functions: Factorial & Fibonacci

This document contains sample code for two recursive functions: one to compute the sum of the first n natural numbers using recursion, and another to calculate the nth Fibonacci number using recursion. The code includes functions to add numbers recursively and find the Fibonacci number at a given index recursively.

Uploaded by

kssmanikanta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

/* Sample codes for recursive funtions

1. To compute factorial of a given number


2. To find fibonacci number of the given index
*/
include <stdio.h>
void main()
{
int k=10;
printf("sum of 1st %d numbers : %d",k, add(k));
}
int add(int n)
{
if(n<1)
return 0;
else
return(n+add((n-1)));
}

#include <stdio.h>
void main()
{
int x,y;
printf("Enter the index to find fibonacci value");
scanf("%d",&x);
y=fib(x);
printf("The fibonacci value is %d\n",y);
}
int fib(z)
{
if(z==0 || z==1)
return(1);
else
{
return(fib(z-1)+fib(z-2));
}
}

You might also like