×

Fibonacci series in C

We have learned about the Fibonacci series in mathematics. For a quick recap, A Fibonacci series is the sequence of numbers following a certain pattern i.e., the next number should be the sum of the before two numbers.

Clear understanding on Fibonacci series:

The first two numbers are 0 and 1. Adding the previous two elements, it becomes:

0
1
1 (0+1)
2 (1+1)
3 (1+2)
5 (2+3)
8 (3+5)
13 (5+8)
21 (8+13)
34 (13+21)
55 (21+34)

To execute a program to print the Fibonacci series in C, we have 2 prominent programs:

  1. Using Recursion
  2. Non-recursive

Non-Recursive:

Upto the given number of terms:

#include<stdio.h>
void main()
{
	int num1 = 0, num2 = 1, sum, i, n;
	printf (“Enter the number of elements you need: “);
	scanf (“%d”,&n);
	printf (“\n%d %d”,num1,num2);
	for (i=2;i<n;i++)
	{
		sum = num1+num2;
		printf (“ %d”,sum);
		num1 = num2;
		num2 = sum;
	}
}

Output:

Enter the number of elements you need: 4
0 1 1 2 

Mechanism:

We initialized the first two numbers num1 and num2, to 0 and 1 and printed the 2 numbers. Now, inside the loop, the sum of the first two numbers is calculated and is printed as it’s the rule, and then num1 and num2 are updated with num2 and sum, and the loop goes on till it reaches n numbers, printing the sequence.

Fibonacci series upto the given number:

#include<stdio.h>
void main()
{
	int num1 = 0, num2 = 1;
	int sum = 0,n;
	printf(“Enter the number upto which the series is needed: “);
	scanf(“%d”,&n);
	printf(“Fibonacci series: %d %d”,num1,num2);
	sum = num1 + num2;
	while(sum<=n)
	{
		printf(“%d,  “, sum);
		num1 = num2;
		num2 = sum;
		sum = num1 + num2;
	}
}

Output:

Enter the number upto which the series is needed: 100
Fibonacci series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,

Mechanism:

First, we tool the input of upto which number the series need to be printed. We print the first two elements 0 and 1 and then we calculate the sum as 0 + 1. Now, we open a while loop with condition, sum should be less than n because, when sum becomes n, it reaches n, it needs to stop at that point. Inside the loop, we print the sum and keep on updating the values of num1 and num2 as num2 and sum.

Using Recursion:

Upto the given number of terms:

#include<stdio.h>
void Fib(int n)
{    
  	            static int num1=0,num2=1,sum;   
                  if(n>0)
   		{    
    			    sum = num1 + num2;    
      		    num1 = num2;    
        		    num2 = sum;    
       		    printf ("%d ",sum);    
        		    Fib (n-1);    
  		  }    
} 
void main()
{    
  		  int n;    
  printf("Enter the number of elements: ");    
  		  scanf ("%d",&n);    
    		  printf ("Fibonacci Series: ");    
  		  printf ("%d %d ",0,1);    
  		  Fib (n-2);
 }

Output:

Enter the number of elements: 4
Fibonacci Series: 0 1 1 2

Mechanism:

We use a function Fib () taking the parameter n->number of elements. We take the number of elements as the input, print the first two numbers as 0 and 1. Two numbers are printed already; we need more n-2 elements, so we give the n-2 parameters to the function. In the function body, we calculate the sum, print the sum, then update the values of num1, num2 as num2, sum. Now, we call the function will n-1. Again the whole process continues till the value of n becomes 0. When n becomes 0, there will be no executable part in the function hence terminating the execution.


Related Topics

Armstrong program in C using function

In this article we will learn how to find Armstrong of a number using function in C. An Armstrong number is a three-digit number that is the sum of its separate...

1 minute read.

For Loop in C Programming Examples

The Syntax of the For Loop for (initialization statement; termination condition; modifying (increment/ decrement) statement) {       /* main body of the For loop */     } In for loop, the initialization command...

6 minutes read.

Find occurrence of substring in C using function

Introduction: In this article, we discuss finding the occurrence of a substring in C using function. This software takes strings and substrings as input and counts the occurrences of substrings within...

3 minutes read.

C Data type

Data Types in C with Examples Data types are used to define the type of data that a variable can store to perform a specific operation.ANSI C provides three types of...

2 minutes read.

Keywords in C

A keyword is a word that has a predetermined meaning. The C compiler knows the purposes of the keywords. The objectives of these keywords cannot be modified. Keywords are the...

9 minutes read.

C/C++ Program to Find the Size of int, float, double and char

In this tutorial, we will learn how to use the sizeof operator to determine the size of each variable. Program to Determine Variable Size Write a C or C++ program to determine the...

2 minutes read.

Difference between If Else and Nested If Else in C

What is the If Else statement in C? In C programs, if else conditions are used to perform some operations based on specific conditions. The operation is only executed if the...

6 minutes read.

Loop Questions in C

Question 1: What is For loop syntax? Ans: The syntax that has been used for the ‘for’ loop in C programming language is: for (initialization statement /*for providing a value to variables*/; test...

19 minutes read.

clrscr in C

clrscr function in C clrscr stands for: clr = clear scr= screen When the clrscr() function is called in a program everything currently displayed in the console(output of previous programs, output of current program,...

3 minutes read.

If else Programs in C Programming

If else Programs in C programming The if-else statement in C is based on some particular conditions to perform the operations. If and only if the given condition is valid, the operations listed in...

4 minutes read.

C Program to Find Largest Number Using Dynamic Memory Allocation

This tutorial will teach us how to locate the highest number a user has ever input into a dynamically allocated memory. C Program: #include <stdio.h> #include <stdlib.h> int main ()  {   int no;   double *data_n;   printf...

1 minute read.

Find Union and Intersection of Two Arrays in C

Union You can find the union of the two sorted arrays using the join merge method on arr1[] and arr2[]. Use two index variables i and j with initial values i =...

4 minutes read.

C –Structure

C structure is a collection of different types of data that is grouped together. It is used to represent records.Structkeyword is used to create a structure. Each element of a...

1 minute read.

Binary Search in C with Best and Worst Time Complexity

What is Binary Search? Binary search is an algorithm that is used to find an element in a sorted array efficiently. It has a time complexity of O(log n). It means...

3 minutes read.

Nested if-else statement in C

If we use an if-else statement within another if statement in a C program, it is called a nested if-else statement in C. It helps to check the condition inside...

3 minutes read.

Purpose of a Function Prototype in C

A function prototype in C is a declaration of a function that specifies the function's name, return type and parameters. It has the following syntax: return_type function_name(parameter_list); For example, the prototype for...

4 minutes read.

Heap Sort in C

In this tutorial, we will learn about heap sorting in C language, but before going to Heap sort, we have to know the concept of Complete Binary Tree. What is Complete...

8 minutes read.

Pi() Function in C

Pi: Mathematic constants are not defined in the c or c++ programming languages. To use the Mathematical constants firstly, we have to define the _USE_MATH_DEFINES and then declare the cmath and...

3 minutes read.

How to create a binary file in C?

Creating a binary file in C language is very simple, requiring only some specific functions to be implemented. There are also other binary modes: read, and write, which will be...

7 minutes read.

Loop Statement in C

Loop statement is used to execute one or more statement repeatedly multiple times. There are three types of loops in C language. Why use loop? We can use loop because it executes a...

2 minutes read.