by CodeChum Admin
This one’s probably one of the most popular recursion problems in programming. Your task is to create a recursive function that prints out the the first up to the nth number in the Fibonacci sequence by going through the numbers using recursion.
In case you forgot, here is the definition of a Fibonacci sequence:
Instructions:
#include <stdio.h>
int displayFibonacci(int n)
{
if ((n == 1) || (n == 2))
return 1;
return displayFibonacci(n-1) + displayFibonacci(n-2);
}
int main()
{
int n;
printf("Enter n: ");
scanf("%d", &n);
printf("\nFibonacci sequence:\n");
for(int i=1; i<=n; i++)
printf("%d ", displayFibonacci(i));
}
Comments
Leave a comment