Tutorial Playlist
132 Lessons1. Introduction to C Tutorial
2. Addition of Two Numbers in C
3. Anagram Program in C
4. Armstrong Number in C
5. Array in C
6. Array of Pointers in C
7. Array of Structure in C
8. C Program to Find ASCII Value of a Character
9. Assignment Operator in C
10. Binary Search in C
11. Binary to Decimal in C
12. Bitwise Operators in C
13. Boolean in C
14. C Compiler for Mac
15. C Compiler for Windows
16. C Function Call Stack
17. C Language Download
18. Operators in C
19. C/C++ Preprocessors
20. C Program for Bubble Sort
21. C Program for Factorial
22. C Program for Prime Numbers
23. C Program for String Palindrome
24. C Program to Reverse a Number
25. Reverse a String in C
26. C string declaration
27. String Input Output Functions in C
28. Calculator Program in C
29. Call by Value and Call by Reference in C
30. Ceil Function in C
31. Coding Vs. Programming
32. Command Line Arguments in C/C++
33. Comments in C
34. Compilation process in C
35. Conditional Statements in C
36. Conditional operator in the C
37. Constant Pointer in C
38. Constants in C
39. Dangling Pointer in C
40. Data Structures in C
41. Data Types in C
42. Debugging C Program
43. Convert Decimal to Binary in C
44. Define And include in C
45. Difference Between Arguments And Parameters
46. Difference Between Compiler and Interpreter
47. Difference Between If Else and Switch
48. Do While Loop In C
49. Double In C
50. Dynamic Array in C
51. Dynamic Memory Allocation in C
52. Enumeration (or enum) in C
53. Evaluation of Arithmetic Expression
54. Factorial of A Number in C
55. Features of C Language
56. Fibonacci Series Program in C Using Recursion
Now Reading
57. File Handling in C
58. For Loop in C
59. Format Specifiers in C
60. Functions in C
61. Function Pointer in C
62. goto statement in C
63. C Hello World Program
64. Header Files in C
65. Heap Sort in C Program
66. Hello World Program in C
67. History of C Language
68. How to compile a C program in Linux
69. How to Find a Leap Year Using C Programming
70. Identifiers in C
71. If Else Statement in C
72. If Statement in C
73. Implementation of Queue Using Linked List
74. Increment and decrement operators in c
75. Input and Output Functions in C
76. How To Install C Language In Mac
77. Jump Statements in C
78. Lcm of Two Numbers in C
79. Length of an Array in C
80. Library Function in C
81. Linked list in C
82. Logical Operators in C
83. Macros in C
84. Matrix multiplication in C
85. Nested if else statement in C
86. Nested Loop in C
87. One Dimensional Array in C
88. Operator Precedence and Associativity in C
89. Overflow And Underflow in C
90. Palindrome Program in C
91. Pattern Programs in C
92. Pointer to Pointer in C
93. Pointers in C: A Comprehensive Tutorial
94. Pre-increment And Post-increment
95. Prime Number Program in C
96. Program for Linear Search in C
97. Pseudo-Code In C
98. Random Access Files in C
99. Random Number Generator in C
100. Recursion in C
101. Relational Operators in C
102. Simple interest program in C
103. Square Root in C
104. Stack in C
105. Stack Using Linked List in C
106. Static function in C
107. Stdio.h in C
108. Storage Classes in C
109. strcat() in C
110. Strcmp in C
111. Strcpy in C
112. String Comparison in C
113. String Functions in C
114. String Length in C
115. String Pointer in C
116. strlen() in C
117. Structures in C
118. Structure of C Program
119. Switch Case in C
120. C Ternary Operator
121. Tokens in C
122. Toupper Function in C
123. Transpose of a Matrix in C
124. Two Dimensional Array in C
125. Type Casting in C
126. Types of Error in C
127. Unary Operator in C
128. Use of C Language
129. User Defined Functions in C
130. What is Variables in C
131. Is C language case sensitive
132. Fibonacci Series in C
The Fibonacci sequence is a cornerstone concept in mathematics and computer science. It comprises a sequence of numbers where each number is a sum of its two immediate predecessors, usually starting from 0 and 1. This article examines the construction of a Fibonacci Series program in C using recursion and other alternative methods.
If you want to download this tutorial in PDF format for further reading: Download Tutorial PDF
In the realm of the C programming language, the Fibonacci series is a numerical sequence achieved through specific programming constructs. The series, conventionally, commences from 0 and 1, with subsequent numbers calculated as the sum of the prior two.
For instance, a Fibonacci series beginning with 0 and 1 appears as follows:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
There are several methodologies for constructing a Fibonacci series in C, each offering its unique programming constructs. These methods include:
To comprehend the mechanics of how a program calculates the Fibonacci series, it is first important to understand the fundamental principle behind the Fibonacci sequence. The Fibonacci sequence refers to a number series where every number is the sum of the two preceding numbers, starting from 0 and 1. Formally, the sequence can be defined by the relation:
F(n) = F(n-1) + F(n-2)
Where F(n) is the nth term in the Fibonacci sequence.
Now, let's dissect how a program computes the Fibonacci function using both an iterative approach (for example, a 'for' loop) and a recursive approach.
Here's a step-by-step guide on how an iterative program calculates the Fibonacci series:
The recursive approach involves a function calling itself to calculate the Fibonacci sequence. Here's how it works:
The iterative method directly implements the definition of the Fibonacci series in a straightforward manner. On the other hand, the recursive method leverages the nature of the Fibonacci series and the property of functions to call themselves to simplify the program's structure.
C programming language provides diverse ways to compute the Fibonacci series. These include:
The Fibonacci series computation can be achieved without recursion—by employing a 'for' loop or while loop. This method, known as the iterative method, calculates the next number by summing up the previous two numbers.
Below is a basic Fibonacci series program in C without recursion, followed by an in-depth explanation:
#include <stdio.h> |
This code begins by initialising two integer variables t1 and t2, to 0 and 1, respectively, representing the first two terms of the Fibonacci series. The user is then prompted to input the number of terms they wish to print.
The for loop runs n times, where n is the number of terms entered by the user. During each iteration, the code prints the current term, computes the next term by adding t1 and t2, and updates t1 and t2 to be the last two terms for the next round of calculation.
Recursion in programming describes a process where a function calls itself within its code. The Fibonacci series in C can be computed using recursion, as shown below:
#include <stdio.h> |
The function fibonacci(int n) is defined to compute the nth term of the Fibonacci sequence using recursion. If n equals 0 or 1, the function directly returns n. If n is greater than 1, the function calls itself with arguments n-1 and n-2 and returns the sum of these function calls, per the Fibonacci sequence's rule.
The main function asks the user for the number of terms they want in the series. Then, a for loop runs n times to print the first n terms of the Fibonacci series. During each iteration, the fibonacci() function is called with the current term number to compute the corresponding Fibonacci number.
We can use recursion, a fundamental concept in various programming languages, to print the Fibonacci series. Recursion refers to the technique of making a function call itself directly or indirectly, making the solution to a problem easier to express.
A recursive function to display the Fibonacci series can be written as follows:
#include <stdio.h> |
In this code, we use recursion to print the Fibonacci series up to n numbers. We first define a function printFibonacci(int n), where n is the number of terms in the series.
In the function, we define three static variables, n1, n2, and n3, to hold the current number and the two preceding numbers in the series. n1 and n2 are initialised to the first two numbers in the series, 0 and 1.
If n is greater than 0, the function calculates n3 as the sum of n1 and n2, updates n1 and n2 for the next iteration, prints n3, and calls itself with n-1.
The main function asks users to input the number of terms they want in the series. It then prints the first two numbers and calls printFibonacci(n-2) to print the remaining numbers.
We can also print the Fibonacci series using an iterative method, such as a 'for' loop. Here's how to do it:
#include <stdio.h> |
In this program, we define a function printFibonacci(int n), where n is the number of terms in the series. We initialise n1 and n2 to the first two numbers in the series and print them.
The function then enters a 'for' loop that runs n-2 times. In each iteration, it calculates n3 as the sum of n1 and n2, prints n3, and updates n1 and n2 for the next iteration.
The main function works the same as in the recursive version, asking the user to input the number of terms and then calling printFibonacci(n) to print the series.
The Fibonacci series, a fascinating concept in both mathematics and computer science, can be calculated using several methodologies in the C programming language. While recursion offers an elegant and intuitive solution, it may not be the most efficient method for larger series due to the function calls overhead. Conversely, an iterative method like a 'for' loop can execute more quickly, even though it may seem less straightforward.
Ultimately, the choice between recursion and iteration will depend on the specific requirements of your project, such as code readability, execution speed, and memory usage. Both methods, however, provide invaluable insight into how algorithms work and highlight the power of the C programming language.
To get a deeper understanding of these programming concepts and much more, consider enrolling in upGrad’s Master of Science in Computer Science course. Leveraging in-demand skills, programming efficiencies and more, this course is designed to equip you with the knowledge and skills necessary for the ever-evolving tech world.
1. What is a Fibonacci series?
The Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding ones. It usually starts with 0 and 1.
2. How is the Fibonacci series calculated in C?
The Fibonacci series can be calculated in C using various methods, such as using recursion or an iterative method like a 'for' loop.
3. What is recursion in C?
Recursion in C is the process of a function calling itself from within its own code.
4. Why use recursion for the Fibonacci series in C?
Recursion provides a simple and clear way to calculate the Fibonacci series. However, for large inputs, the recursive method can be inefficient due to the overhead of function calls.
PAVAN VADAPALLI
Director of Engineering
Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working …Read More
Popular
Talk to our experts. We’re available 24/7.
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enrolling. upGrad does not make any representations regarding the recognition or equivalence of the credits or credentials awarded, unless otherwise expressly stated. Success depends on individual qualifications, experience, and efforts in seeking employment.
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...