The Fibonacci Series is one of the most famous sequences in mathematics and computer science. In this series, every number is the sum of the two numbers before it. Starting from 0 and 1:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ...
The Fibonacci sequence appears throughout nature โ in the spiral arrangement of a sunflower's seeds, the pattern of a pine cone, the branching of trees, and the spiral shell of the nautilus. This is sometimes called the 'fingerprint of nature'.
#include <stdio.h>
int main() {
int n, a = 0, b = 1, c;
printf("Enter number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (int i = 1; i <= n; i++) {
printf("%d ", a);
c = a + b; // next term = sum of previous two
a = b; // update a
b = c; // update b
}
return 0;
}
Output for n=8: 0 1 1 2 3 5 8 13
#include <stdio.h>
int fibonacci(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
return fibonacci(n-1) + fibonacci(n-2);
}
int main() {
int n;
printf("Enter terms: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("%d ", fibonacci(i));
}
return 0;
}
Note: The iterative method is much more efficient. The recursive method has O(2โฟ) time complexity (exponential โ very slow for large n), while the iterative method is O(n) (linear โ much faster).
Starting from the 1st term as 0: 0, 1, 1, 2, 3, 5, 8, 13, 21, **34**. The 10th term is **34**.
What is India's First Supercomputer?
Learn about PARAM 8000, India's very first supercomputer. Discover the history of C-DAC, Vijay Bhatkar, and how India built its own supercomputer in 1991.
Difference Between Internet and Intranet
Learn the difference between the Internet (public, global network) and an Intranet (private, secure organizational network).
Inter-Process Communication (IPC) in Operating Systems
Understand what Inter-Process Communication (IPC) is in Operating Systems. Learn about Shared Memory and Message Passing mechanisms.
What does 'Invalid Credentials' mean?
Have you seen the error 'Invalid Credentials'? Learn exactly what it means, why it happens, and how to fix this common login issue.
What is the Full Form of IP in Computers?
Learn the full form of IP in computer networks. Understand what an Internet Protocol (IP) address is, and the difference between IPv4 and IPv6.
Turn this guide into revision flashcards, a practice exam, or an AI-generated podcast โ free, no signup required.