Program to Calculate Fibonacci
Fibonacci of Given Number
We will write a C program to display the fibonacci series of the given number and the number will be entered by the user. This will help to understand the basic structure of programming. In this program , we will display the fibonacci series of the numbers easily by using proper syntax and algorithms.
Working of Program :
In the program, we will require some numbers from the user to display the fibonacci series of the numbers.
Important Note for Fibonacci Series:
- In fibonacci series, the next number will always be the sum of previous two numbers.
For example: 0,1,1,2,3,5,8,13,21… - The first two numbers of fibonacci series will alays be 0 and 1.
Problem 1
Write a program to display the fibonacci series of 8 terms using while() loop.
- Firstly, we have to enter the term of number.
- Then print the fibonacci of the given term of number.
Code
#include<stdio.h> int main () { int a = 0, b = 1, c, i = 0; printf ("%d\n", a); printf ("%d\n", b); while (i < 6) { c = a + b; printf ("%d\n", c); a = b; b = c; i = i + 1; } return 0; }
Output
0 1 1 2 3 5 8 13
Note:
In the following we will display the fibonacci of the given terms of number using do-while() loop.
Syntax for do – while() loop:
do { // Statements // Increment / Decrement } while(condition)
Problem 2
Write a program to display the fibonacci series of 13 terms by using do – while().
- Firstly, we have to enter the term of number.
- Then print the fibonacci of the given term of number.
Code
#include<stdio.h> int main () { int a = 0, b = 1, c, i = 0; printf ("%d\n", a); printf ("%d\n", b); do { c = a + b; printf ("%d\n", c); a = b; b = c; i = i + 1; } while (i < 13); return 0; }
Output
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment