Fibonacci Series in C
The Fibonacci series in C is a sequence of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1.
What is Fibonacci Series?
The Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1.
The Fibonacci sequence is named after the Italian mathematician Leonardo of Pisa, who was also known as Fibonacci. He introduced the series to the Western mathematics in his book Liber Abaci, which he wrote in 1202.
The Fibonacci series is an example of a recursive sequence, which means that the next number in the series is calculated by adding the previous two numbers. The first few numbers in the series are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, and so on. It is also closely related to the Golden Ratio and occurs in many natural phenomena, such as the branching of trees and the arrangement of leaves on a stem.
Approach to print Fibonacci Series in C Language
Here is one approach to print the Fibonacci series in C:
- Include the
stdio.h
header file to use theprintf
andscanf
functions. - In the main function, declare three variables:
i
,n
,t1
,t2
, andnextTerm
. - Prompt the user to enter the number of terms they want in the series and store it in the variable
n
. - Initialize
t1
andt2
to 0 and 1 respectively. These will be the first two terms of the series. - Use a for loop to iterate from 1 to
n
. In each iteration, print the value oft1
, which is the current term in the series. - Calculate the next term in the series by adding
t1
andt2
and store it in the variablenextTerm
. - Update the values of
t1
andt2
for the next iteration by settingt1
to the value oft2
andt2
to the value ofnextTerm
. - Repeat steps 5 to 7 for all the iterations of the for loop.
- Finally, return 0 from the main function to indicate successful execution of the program.
This is just one of the possible approaches to print the Fibonacci series in C. There are many other ways to accomplish this task, each with its own advantages and disadvantages.
Pseudo Code to Print Fibonacci Series in C Language
// Pseudo code for printing Fibonacci series in C
// Step 1: Include the stdio.h header file
#include <stdio.h>
// Step 2: Define the main function
int main()
{
// Step 3: Declare variables for iteration, number of terms, and the current and next terms in the series
int i, n, t1, t2, nextTerm;
// Step 4: Prompt the user to enter the number of terms they want in the series
printf("Enter the number of terms: ");
scanf("%d", &n);
// Step 5: Initialize the first two terms in the series
t1 = 0;
t2 = 1;
// Step 6: Use a for loop to iterate from 1 to the number of terms
for(i = 1; i <= n; i++)
{
// Step 7: Print the current term in the series
printf("%d ", t1);
// Step 8: Calculate the next term in the series
nextTerm = t1 + t2;
// Step 9: Update the current and next terms for the next iteration
t1 = t2;
t2 = nextTerm;
}
// Step 10: Return 0 from the main function to indicate successful execution
return 0;
}
C Program to print Fibonacci series
#include <stdio.h>
int main()
{
int i, n, t1 = 0, t2 = 1, nextTerm;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (i = 1; i <= n; ++i)
{
printf("%d, ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
return 0;
}
This C program will prompt the user to enter the number of terms they want in the Fibonacci series, and then print the series using a for loop. The initial values of t1 and t2 are set to 0 and 1 respectively, the same as the Fibonacci series. And with each iteration of the loop, the next term in the series is calculated and printed, and the values of t1 and t2 are updated for the next iteration.
The output will look something like this:
Enter the number of terms: 8
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13,
Please Remember that the exact output will depend on the value entered by the user for the number of terms.
Check Out Other Programs :