How to write fibonacci Series program in C

Here is  Fibonacci Series program in C.

// Fibonacci Series using Recursion
#include <stdio.h>
int fib(int n)
{
if (n <= 1)
return n;
return fib(n – 1) + fib(n – 2);
}

int main()
{
int n = 9;
printf(“%d”, fib(n));
getchar();
return 0;
}

Asked on January 15, 2020 in program.

Formulla for fibonacci series is:

an = [ Phin – (phi)n ]/Sqrt[5]. where Phi=(1+Sqrt[5])/2 is the so-called golden mean, and phi=(1-Sqrt[5])/2 is an associated golden number, also equal to (-1/Phi).

This formula is attributed to Binet in 1843, though known by Euler before him.

on January 21, 2020.
Add Comment
3 Answer(s)

Fibonacci Series in C without recursion

 

#include<stdio.h>
int main()
{
int n1=0,n2=1,n3,i,number;
printf(“Enter the number of elements:”);
scanf(“%d”,&number);
printf(“\n%d %d”,n1,n2);//printing 0 and 1
for(i=2;i<number;++i)//loop starts from 2 because 0 and 1 are already printed
{
n3=n1+n2;
printf(” %d”,n3);
n1=n2;
n2=n3;
}
return 0;
}

 

Fibonacci Series using recursion in C

#include<stdio.h>
void printFibonacci(int n){
static int n1=0,n2=1,n3;
if(n>0){
n3 = n1 + n2;
n1 = n2;
n2 = n3;
printf(“%d “,n3);
printFibonacci(n-1);
}
}
int main(){
int n;
printf(“Enter the number of elements: “);
scanf(“%d”,&n);
printf(“Fibonacci Series: “);
printf(“%d %d “,0,1);
printFibonacci(n-2);//n-2 because 2 numbers are already printed
return 0;
}

 

Answered on January 15, 2020.
Add Comment
The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, … The next number is found by adding up the two numbers before it.

Fibonacci Sequence
  1. The 2 is found by adding the two numbers before it (1+1)
  2. The 3 is found by adding the two numbers before it (1+2),
  3. And the 5 is (2+3),
  4. and so on!

Fibonacci Series up to n terms

#include <stdio.h>

int main() {

int i, n, t1 = 0, t2 = 1, nextTerm;

printf("Enter the number of terms: ");

scanf("%d", &n);

printf("Fibonacci Series: ");

for (i = 1; i <= n; ++i) {

printf(“%d, “, t1);

nextTerm = t1 + t2;

t1 = t2;

t2 = nextTerm;

}

return 0;

}

Answered on January 21, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.