xxxxxxxxxx
#include <stdio.h>
// declaring function
int fib(int num)
{
if (num == 0)
{
return 0;
}
else if (num == 1)
{
return 1;
}
else
{
return fib(num - 1) + fib(num - 2);
}
}
int main()
{
int a;
printf("enter the number of terms you want to print\n");
scanf("%d", &a);
printf("fibonacci series upto %d is : ", a);
for (int c = 0; c <a; c++)
{
printf(" %d ", fib(c));
}
return 0;
}
xxxxxxxxxx
//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;
}
xxxxxxxxxx
// Sum of fibonacci series
#include <stdio.h>
int main()
{
int fib[1000];
int num, sum = 1, i;
scanf(" %d", &num);
fib[0] = 0;
fib[1] = 1;
printf(" %d + %d", fib[0], fib[1]);
for(i = 2; i < num; i++){
fib[i] = fib[i-1] + fib[i-2];
printf(" + %d", fib[i]);
sum += fib[i];
}
printf("\n = %d", sum);
return 0;
}
xxxxxxxxxx
#include <stdio.h>
void fib(int a, int b, int max){ // max is used to stop the recursion when desired number is reached
if(a >= max){
return;
}
printf("%d,", a);
fib(b, b + a, max); // a = b, b = a + b;
}
int main(){
fib(0,1,10);
return 0;
}
xxxxxxxxxx
//Program Name: Print fibonacci series using for loop upto n term
// for e.g 1, 1, 2, 3, 5, 8
#include <stdio.h>
void main()
{
int s1=0,s2=1; //initializing first two numbers
int nextNum=0,SumUpto=0;
printf("\n\n\tPlease enter number up to which print Fibonacci series is required \t");
scanf("%d",&SumUpto);
//printing first two numbers
printf("\n\tfibbonacci Series up to %d is ",SumUpto);
printf("\n\n\t%d %d",s1,s2);
for(nextNum=2;nextNum<=SumUpto;)
{
s1=s2;
s2=nextNum;
printf(" %d",nextNum);
nextNum=s1+s2;
}
}
xxxxxxxxxx
#include <stdio.h>
void main()
{
int s1=0,s2=1; //initializing first two numbers
int nextNum=0,SumUpto=0;
printf("\n\n\tPlease enter number up to which print Fibonacci series is required \t");
scanf("%d",&SumUpto);
printf("\n\tfibbonacci Series up to %d is ",SumUpto);
printf("\n\n\t%d %d",s1,s2);
for(nextNum=2;nextNum<=SumUpto;)
{
s1=s2;
s2=nextNum;
printf(" %d",nextNum);
nextNum=s1+s2;
}
}
xxxxxxxxxx
int fibs(int a)
{
if (a==0) return 0;
else if(a==1)
return 1;
else
return (fibs(a-1)+fibs(a-2));
}