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

Recursion Programs BCA

The document provides C programs that demonstrate recursion through two examples: calculating the Fibonacci series and finding the factorial of a number. The Fibonacci function recursively computes values based on the sum of the two preceding numbers, while the factorial function multiplies the number by the factorial of the preceding number. Both programs include user input for the number of terms or the number to compute the factorial for.

Uploaded by

ashritha jois
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)
5 views1 page

Recursion Programs BCA

The document provides C programs that demonstrate recursion through two examples: calculating the Fibonacci series and finding the factorial of a number. The Fibonacci function recursively computes values based on the sum of the two preceding numbers, while the factorial function multiplies the number by the factorial of the preceding number. Both programs include user input for the number of terms or the number to compute the factorial for.

Uploaded by

ashritha jois
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

C Programs using Recursion (BCA Notes)

1. Fibonacci Series using Recursion


Logic: F(0)=0, F(1)=1, F(n)=F(n-1)+F(n-2)

#include <stdio.h>

int fibonacci(int n)
{
if(n == 0)
return 0;
else if(n == 1)
return 1;
else
return fibonacci(n-1) + fibonacci(n-2);
}

int main()
{
int n, i;

printf("Enter number of terms: ");


scanf("%d", &n);

for(i = 0; i < n; i++)


{
printf("%d ", fibonacci(i));
}

return 0;
}

2. Factorial using Recursion


Logic: n! = n × (n-1)! , Base case: 0! = 1

#include <stdio.h>

int factorial(int n)
{
if(n == 0 || n == 1)
return 1;
else
return n * factorial(n-1);
}

int main()
{
int n;

printf("Enter a number: ");


scanf("%d", &n);

printf("Factorial of %d = %d", n, factorial(n));

return 0;
}

You might also like