The following code shows how to display Fibonacci Series in C Language. A fibonacci series is the one whose next term equals the sum of previous two terms. The following code shows fibonacci series in two different ways. While, the first function shows the series of specified number of terms. Another function takes a value as a parameter. Then, it displays the series till the last term remains less than or equal to the specified value.
// Display Terms of the Fibonacci Series
#include <stdio.h>
void fib1(int n)
{
int x=-1, y=1, z, i;
printf("\nFibonacci Series of %d Terms...\n", n);
for(i=1;i<=n;i++)
{
z=x+y;
x=y;
y=z;
printf("%d ", z);
}
}
void fib2(int n)
{
int x=-1, y=1, z, i;
printf("\n\nFibonacci Series till %d...\n", n);
while(1)
{
z=x+y;
if(z>n)
break;
x=y;
y=z;
printf("%d ", z);
}
}
int main()
{
int number_of_terms, last_term;
printf("Enter the Number of Terms in Fibonacci Series: ");
scanf("%d", &number_of_terms);
fib1(number_of_terms);
printf("\nEnter the value of Last Term in Fibonacci Series: ");
scanf("%d", &last_term);
fib2(last_term);
return 0;
}
Output
Enter the Number of Terms in Fibonacci Series: 20
Fibonacci Series of 20 Terms…
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
Enter the value of Last Term in Fibonacci Series: 700
Fibonacci Series till 700…
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610
Further Reading
50+ C Programming Interview Questions and Answers
C Program to Find Factorial of a Number
- AI
- Android
- Angular
- ASP.NET
- Augmented Reality
- AWS
- Bioinformatics
- Biometrics
- Blockchain
- Bootstrap
- C
- C#
- C++
- Cloud Computing
- Competitions
- Courses
- CSS
- Cyber Security
- Data Science
- Data Structures and Algorithms
- Data Visualization
- Datafication
- Deep Learning
- DevOps
- Digital Forensic
- Digital Trust
- Digital Twins
- Django
- Docker
- Dot Net Framework
- Drones
- Elasticsearch
- ES6
- Extended Reality
- Flutter and Dart
- Full Stack Development
- Git
- Go
- HTML
- Image Processing
- IoT
- IT
- Java
- JavaScript
- Kotlin
- Latex
- Machine Learning
- MEAN Stack
- MERN Stack
- Microservices
- MongoDB
- NodeJS
- PHP
- Power Bi
- Projects
- Python
- Quantum Computing
- React
- Robotics
- Rust
- Scratch 3.0
- Shell Script
- Smart City
- Software
- Solidity
- SQL
- SQLite
- Tecgnology
- Tkinter
- TypeScript
- VB.NET
- Virtual Reality
- Web Designing
- WebAssembly
- XML