×

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

pow() function in C

pow() function is one of the in-built functions present in math.h header file Power function is used to calculate the powers of the given number. The syntax of the power...

3 minutes read.

Derived Data Types in C

A variable in a program occupies some space in the computer's memory where some value is stored. Each variable in C has an associated data type. A value to be...

4 minutes read.

Typecast vs. typedef in C

Typecast vs. typedef in C Typecast In the C programming language, converting the data type from one form to another is known as type casting or the type conversion. It is a...

5 minutes read.

Results of Comparison Operations in C and C++

In this tutorial, we will explore comparison operators and how the system should compare an incoming value supplied as a context parameter to a given value or range of values. A...

2 minutes read.

Bitwise XOR Operator in C

What is Bitwise? As we know, bits are the smallest unit of a number and are mostly used in a computing system. So, in bitwise, we do operations on bits instead...

3 minutes read.

Malloc function in C

When the user or programmer does not know how much memory space is needed in the application, it a needs dynamic memory allocation during runtime. Dynamic memory allocation helps us to...

4 minutes read.

Dangling pointers in C

Dangling pointers in C: A pointer is a variable that stores the memory address of other variables. The pointers may also store the address of other’s memory items. They are...

4 minutes read.

Prototype in C

A prototype is nothing but a model, a model of initial creation of an intended product. Similarly, in the C programming language, all functions have a prototype. In general, all...

4 minutes read.

memcmp() in C

Introduction: In this article we are discuss about memcmp() function in C. This function permits the person to evaluate the bytes of the two characters, as mentioned above. Depending on...

4 minutes read.

Pure Virtual Function in C

Before knowing about the pure virtual function some of the important points about the virtual function in C++ are given below. In c++ the member function that is defined in the...

4 minutes read.

Int in C

The keyword int in C programming stands for integer and it is a data type which is used for variable declarations or declaration of functions of different types. Similar to...

4 minutes read.

Conditional Operator in C

In C programming language, the conditional operator (also known as the ternary operator) is a shorthand way of writing an if-else statement. The syntax of conditional operator in C is...

3 minutes read.

getc() function in C

Getc is one of the file handling technique in C. The Getc() is a C library function gets the next character or new characters  from the specific stream and supports...

4 minutes read.

Find out Power without using POW function in C

Introduction: In this discussion, we will discuss how we find out power without using the POW function in C. In this article, you'll understand how to compute integer powers in...

3 minutes read.

Assert() Function in C

Assert (): In C, the statements are executed with the exit statement. In C language declare, and it tests the condition parameters. If the statement executed will be false, it...

3 minutes read.

Isalnum() function in C

Introduction: The isalnum () is a function used in C programming language. This function checks the passing number or argument is an alphanumeric number or not. The alphanumeric number consists of the...

3 minutes read.

Bigint (BIG INTEGERS) in C with Example

The maximum number of digits that a long, long int can have in C/C++ is 20. The issue is how to store the 22-digit number, which is difficult to do...

9 minutes read.

Projects on C language in 2024

Introduction Most aspiring programmers learn the C language as their first high-level programming language. The most flexible language utilized in every industry is, without a doubt, C. Even after 50 years...

9 minutes read.

How to Find Square Free Numbers in C?

Introduction Square-free number program is a typical interview and academic question in C Programming. In this section, we will define square-free integers, construct algorithms and C program to print the nth...

11 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.