C PROGRAM TO FIND FIBONACCI SERIES

WHAT IS FIBONACCI SERIES

Fibonacci series : Fibonacci series is one in which the nth term is sum of (n-1)th term and (n-2)th term. The first two numbers of series are 0 and 1.
For example: 0 1 1 2 3 5 8 13 21 and so on

Now we can write a c program to find fibonacci series in the following ways :

PROGRAM TO FIND FIBONACCI SERIES USING LOOPS

//C PROGRAM TO PRINT FIBONACCI SERIES USING LOOPS
 
 #include<stdio.h>
 
 int main()
 {
  /* Declaring variable for n=number, f=first, s=second, 
  t=third number, i=to iterate the loop */
  int n,f,s,t,i;
  
  // Inputing number till where fibonacci series is to be printed
  printf("Enter the number till which you want to print fibonacci series:");
  scanf("%d",&n);
  
  // Determining and printing fibonacii series
  printf("Fibonacci Series: ");
  
  f=0;
  s=1;
  printf("%d %d ",f,s);
  for(i=3;i<=n;i++)
  {
   t=f+s;
   printf("%d ",t);
   f=s;
   s=t;
  }
  
  return 0;
 }

C PROGRAM TO FIND FIBONACCI SERIES USING FUNCTION

//C PROGRAM TO FIND FIBONACCI SERIES USING FUNCTION
#include<stdio.h>

void fibonacci(int n);
int main()
{
  int n;

  // Inputing number till where fibonacci series is to be printed
  printf("Enter the number till which you want to print fibonacci series:");
  scanf("%d",&n);
  
  fibonacci(n);
  return 0;
}

void fibonacci(int n)
{
  /* Declaring variable for f=first, s=second, 
  t=third number, i=to iterate the loop */
  int f,s,t,i;
  
  // Determining and printing fibonacii series
  printf("Fibonacci Series: ");
  
  f=0;
  s=1;
  printf("%d %d ",f,s);
  for(i=3;i<=n;i++)
  {
   t=f+s;
   printf("%d ",t);
   f=s;
   s=t;
  }
}

C PROGRAM TO FIND FIBONACCI SERIES USING RECURSION

//WRITE A C PROGRAM TO FIND FIBONACCI SERIES USING RECURSION
#include<stdio.h>

int fibonacci(int n);

  /* Declaring variable for f=first, s=second, 
  t=third number, i=to iterate the loop */
  int f,s,t,i;
  f=0;
  s=1;

int main()
{
   int n;
  // Inputing number till where fibonacci series is to be printed
  printf("Enter the number till which you want to print fibonacci series:");
  scanf("%d",&n);
  
  printf("Fibonacci Series: ");
  printf("%d %d ",f,s);
  
  fibonacci(n);
  return 0;
}

int fibonacci(int n)
{
 if(n==2)
 {
    return 0;
 }
 
 else
 {
    t=f+s;
    printf("%d ",t);
    f=s;
    s=t;
    fibonacci(n-1);
 }
}
Note : You can write much better code than these. So view these example and try to program by yourself. Use your own logic. This would improve your programming skills.

Share this

Related Posts

Previous
Next Post »