In this tutorial you will learn about the C Program to Find Nth Fibonacci Number and its application with practical example.
C Program to Find Nth Fibonacci Number
In this tutorial, we will learn to create a C program that will Find Nth Fibonacci Number using C programming.
Prerequisites
Before starting with this tutorial, we assume that you are the best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- While loop in C Programming.
What is Fibonacci Series?
The Fibonacci series is a series that is strongly related to Binet’s formula (golden ratio). In the Fibonacci series, the series starts from 0, 1 and then the third value is generated by adding those numbers.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
STEP 1: START STEP 2: INITIALIZE n STEP 3: Sending the message to inter the value of n from user STEP 4: Grabbing the n number element from the user STEP 5: Using that number in for loop and calculations STEP 6: Generating the Fibonacci series STEP 7: Printing the series. STEP 8: Return 0. STEP 10: END. |
Programs to Find Nth Fibonacci Number:-
In this program, we take the number of elements for the series from the user. Then we will generate the Fibonacci series using the while loop. At last, we will print the nth term using the print function. The code for the program is given below.
Program:-
Program to Find Nth Fibonacci Number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <stdio.h>//including the header files. int main() //body of the main function. { //Declaring the required variables for the program int t1 = 0, t2 = 1, nterm = 0, n; //Taking the input from the user. printf("Enter a positive number: "); scanf("%d", &n); // displays the first two terms which is always 0 and 1 printf("Fibonacci Series nth term : "); nterm = t1 + t2; //Generating the fibonacci series. while (nterm <= n) { //Printing the series from the first position to n terms t1 = t2; t2 = nterm; nterm = t1 + t2; } //Printing the nth term of the series. printf("%d. ", nterm); return 0; } |
Output:-
In the above program, we have first initialized the required variable
- t1 = it will hold the integer value.
- t2 = it will hold the integer value.
- nterm = it will hold the integer value.
- n = it will hold the integer value.
- i = it will hold the integer value to control the array.
Taking input the nth term from the user.
Generating the nth term of the Fibonacci series using the loops.
Program Code for Printing the series.