3.DS - LAB - C Program to generate n Fibonacci numbers using a recursive function(Part A)

C Program to generate n Fibonacci numbers using a recursive function(Part A)

ALGORITHM : 

Step 1: Start
Step 2: Declare a recursive function fibonacci_recursive(n).
Step 3: If n is less than or equal to 1, return n.
Step 4: Otherwise, return fibonacci_recursive(n - 1) + fibonacci_recursive(n - 2).
Step 5: Input the value of n (number of Fibonacci numbers to generate).
Step 6: Initialize an empty list to store the Fibonacci sequence (fibonacci_sequence).
Step 7: Iterate from 0 to n-1 and append fibonacci_recursive(i) to the fibonacci_sequence list.
Step 8: Display the fibonacci_sequence as the output.
Step 9: End


SOURCE CODE : 

#include <stdio.h>
int main()
{
  int n, first = 0, second = 1, next, c;

  printf("Enter the number\n");
  scanf("%d", &n);

  printf("First %d number of Fibonacci series are:\n", n);

  for (c = 0; c < n; c++)
  {
    if (c <= 1)
      next = c;
    else
    {
      next = first + second;
      first = second;
      second = next;
    }
    printf("%d\n", next);
  }

  return 0;
}

OUTPUT :

Enter the number of Fibonacci numbers to generate: 7
0 1 1 2 3 5 8

OUTPUT 1 :





No comments:

Post a Comment