C Program to show Fibonacci Series




c programming
c programming

Write a C program , that prints all the Fibonacci numbers , which are smaller than or equal to a number k(k≥2) ,which was entered by the user.

  • The Fibonacci numbers are a sequence of numbers,where then-th number of Fibonacci is defined as:
  • Fib(0)=0,
  • Fib(1)=1,
  • Fib(n)=Fib(n-1)+Fib(n-2) Usage :> Input of k:21 > Fibonacci numbers<=21:01123581321

Solution 1 –

The solution below will answer following questions :
1. Write a program to generate the Fibonacci series in C.
2. Fibonacci series in C using for loop.
3. How to print Fibonacci series in C.
4. Write a program to print Fibonacci series inC.
5. How to find Fibonacci series in C programming.
6. Basic C programs Fibonacci series.

/**
Fibonacci series in C using for loop by Codebind.com
*/
#include <stdio.h>
#include <stdlib.h>


int main () {
  int r=0,a=0,n,b=1,i,c;
  char redo;
  do {
    a=0;
    b=1;
    printf("enter the the value of n:");
    scanf("%d", &n);
    if(n>100) {
      printf("enter some lessor value\n");
    }
    else {
      for (i=0;i<=n;i++) {
        c=a+b;
        a=b;
        b=c;
        printf("serial no.%d => ", i+1);
        printf("%d\n",a);
      }
      printf("enter y or Y to continue:");
      scanf("%s", &redo);
      printf("\n");
    }
  }
  while(redo=='y'||redo=='Y');

  return 0;
}



/*
OUTPUT
enter the the value of n:10
serial no.1 => 1
serial no.2 => 1
serial no.3 => 2
serial no.4 => 3
serial no.5 => 5
serial no.6 => 8
serial no.7 => 13
serial no.8 => 21
serial no.9 => 34
serial no.10 => 55
serial no.11 => 89
enter y or Y to continue:
*/

Solution 2 –

If we want to Write a program to generate the Fibonacci series in C using recursion, the follow the solution below :

/**
Fibonacci series in C using recursion by codebind.com
*/

#include <stdio.h>
#include <stdlib.h>

int Fibonacci(int x) {
  if (x < 2){
    return x;
  }
  return (Fibonacci (x - 1) + Fibonacci (x - 2));
}

int main(void) {
  int number;
  printf( "Please enter a positive integer: ");
  scanf("%d", &number);
  if (number < 0)
    printf( "That is not a positive integer.\n");
  else
    printf("%d Fibonacci is: %d\n", number, Fibonacci(number));
  return 0;
}


/*
OUTPUT
Please enter a positive integer: 10
10 Fibonacci is: 55

*/

Solution 3 –

The solution below will answer following questions :
1. Fibonacci series using array in C
2. Fibonacci series program in C language
3. Source code of Fibonacci series inC
4.Write a program to print Fibonacci series in C

/**
Fibonacci series in C using array by codebind.com
*/

#include <stdio.h>
#include <stdlib.h>

int main(void) {
  int i,number;
  long int arr[40];

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

  arr[0]=0;
  arr[1]=1;

  for(i = 2; i< number ; i++){
    arr[i] = arr[i-1] + arr[i-2];
  }

  printf("Fibonacci series is: \n");
  for(i=0; i< number; i++)
    printf("%d\n", arr[i]);
  return 0;
}


/*
OUTPUT
Enter the number :
10
Fibonacci series is:
0
1
1
2
3
5
8
13
21
34
*/

 


Partner Sites

VideoToGifs.com

EasyOnlineConverter.com

SqliteTutorials.com





Be the first to comment

Leave a Reply

Your email address will not be published.


*