Saturday, 28 July 2018

C Fibonacci Series


C Fibonacci series


Hello Friends, in this post we are going to learn how to create c Fibonacci series. We will create the Fibonacci series using the c program. But before that we will see what Fibonacci series is-

What is Fibonacci series?


Fibonacci series is nothing but the series of numbers in which the sum of previous two numbers will determines the next number of series.

For example

 0, 1, 1, 2, 3, 5, 8, 13, 21 etc.

Note: Fibonacci series starts from the 0 and 1.

C Fibonacci series program


We can write a c Fibonacci series program in two different ways- that’s the program using the recursion and program without the recursion.

Program 1: c Fibonacci series program without recursion


/* C Fibonacci series program  */
#include <stdio.h>
#include<conio.h>
void main()
{
  int Num, firstNum = 0, secondNum = 1, next, count;
  printf("Enter the end number of Fibonacci series\n");
  scanf("%d", &Num);
  printf(" %d is the termination number of Fibonacci series are:\n", Num);
  for (count = 0; count < n; count++)
  {
    if (count <= 1)
      next = count;
    else
    {
      next = firstNum + secondNum;
      firstNum = secondNum;
      secondNum = next;
    }
    printf("%d\n", next);
  }

  getch();
}


Program 2. C Fibonacci series program using function recursion


#include<stdio.h>
#include<conio.h>

int f(int);

void main()
{
  int num, i = 0, count;

  scanf("%d", &num);

  printf("Fibonacci series termination number:\n");

  for (count = 1; count <= num; count++)
  {
    printf("%d\n", f(i));
    i++;
  }

  getch();
}

int f(int num)
{
  if (num == 0 || num == 1)
    return num;
  else
    return (f(num-1) + f(num-2));
}


Share This
Previous Post
Next Post

Admin and author of BestBCANotes. Complted master degree in Computer Science.

0 comments: