fibonacci series in c

Today, We want to share with you fibonacci series in c.In this post we will show you What is the Fibonacci sequence? & fibonacci series program in c using recursion, hear for Fibonacci Series generates subsequent number by including two previous numbers. we will give you demo and example for implement.In this post, we will learn about GO Program To Display Fibonacci Sequence with an example.

Fibonacci Series Program In C

each number is the sum of the two previous numbers. The first two numbers in the Fibonacci series are 0 and 1. Fibonacci series satisfies the following conditions −

Fn = Fn-1 + Fn-2

The beginning of the sequence is thus:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ……..

Algorithm
Algorithm of this fibonacci series is very simply −

START
   Step 1 → Take integer variable A, B, C
   Step 2 → Set A = 0, B = 0
   Step 3 → DISPLAY A, B
   Step 4 → C = A + B
   Step 5 → DISPLAY C
   Step 6 → Set A = B, B = C
   Step 7 → REPEAT from 4 - 6, for n times
STOP

Fibonacci Series up to n series

Example 1:

#include 
int main() {
    int i, n, firstVal = 0, secondVal = 1, andThenValue;
    printf("Enter the number of series: ");
    scanf("%d", &n);
    printf("Fibonacci Series: ");

    for (i = 1; i <= n; ++i) {
        printf("%d, ", firstVal);
        andThenValue = firstVal + secondVal;
        firstVal = secondVal;
        secondVal = andThenValue;
    }

    return 0;
}

Output

Enter the number of series: 10
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 

Fibonacci Sequence Up to a Certain Number

Example 2:

#include 
int main() {
    int firstVal = 0, secondVal = 1, andThenValue = 0, n;
    printf("Enter a positive number: ");
    scanf("%d", &n);

    printf("Fibonacci Series: %d, %d, ", firstVal, secondVal);
    andThenValue = firstVal + secondVal;

    while (andThenValue <= n) {
        printf("%d, ", andThenValue);
        firstVal = secondVal;
        secondVal = andThenValue;
        andThenValue = firstVal + secondVal;
    }

    return 0;
}

Output

Enter a positive integer: 100
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 

C Fibonacci Series Program using For Loop

Example 3:


#include 
int main()
{
	int Number, Next, i, First_Value = 0, Second_Value = 1;
	
	printf("\n Please Enter the Range Number: ");
	scanf("%d",&Number);
	
	for(i = 0; i <= Number; i++) 
	{
		if(i <= 1)
		{
			Next = i;
	    }
		else
		{
			Next = First_Value + Second_Value;
			First_Value = Second_Value;
			Second_Value = Next;
		}
		printf("%d \t", Next);
	}
	return 0;
}

Fibonacci series in C using Functions

#include
void ExampleOfFiboDemo_series(int Number) ;
int main()
{
   int Number;
 
   printf("Enter the number of series\n");
   scanf("%d", &Number);
 
   printf("ExampleOfFiboDemo series First %d Numbers:\n", Number);
   ExampleOfFiboDemo_series(Number) ;
 
   return 0;
}
void ExampleOfFiboDemo_series(int Number) 
{
   int i, First_Value = 0, Second_Value = 1, Next;
   for(i = 0; i <= Number; i++) 
   {
	if(i <= 1)
	{
	   Next = i;
        }
	else
	{
	   Next = First_Value + Second_Value;
	   First_Value = Second_Value;
	   Second_Value = Next;
	}
	printf("%d\t", Next);
   }
}

Fibonacci series in C using Recursion

#include
 
int ExampleOfFiboDemo_Series(int);
 
int main()
{
   int Number, i = 0, j;
 
   printf("\n Please Enter Number upto which you want too: ");
   scanf("%d", &Number);
 
   printf("Fibonacci series\n");
 
   for ( j = 0 ; j <= Number ; j++ )
   {
      printf("%d\t", ExampleOfFiboDemo_Series(j));
   }
    return 0;
}
 
int ExampleOfFiboDemo_Series(int Number)
{
   if ( Number == 0 )
      return 0;
   else if ( Number == 1 )
      return 1;
   else
      return ( ExampleOfFiboDemo_Series(Number - 1) + ExampleOfFiboDemo_Series(Number - 2) );
}

I hope you get an idea about fibonacci series program in c using recursion.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment