Today i am here to discuss about how to code the fibonacci series in c programming language. We will also see some expected limitations of our program that usually no one will tell you.
So here is the program itself and i hope before moving towards the program people already knew about Fibonacci series. If not please go and learn about it.
Copy the code from below :
#include <stdio.h>
int main()
{
int n, i, 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;
}
In this program, we first declare variables to store the number of terms (n), the loop counter (i), and the terms of the series (t1 and t2). We then prompt the user to enter the number of terms they want to print and store this value in the 'n' variable.
Next, we use a for loop to iterate through the desired number of terms. Inside the loop, we print the value of 't1' and then calculate the next term in the series by adding 't1' and 't2' and storing the result in the 'nextTerm' variable. Finally, we update the values of 't1' and 't2' to be the current 'nextTerm' and the previous 't1', respectively. This allows us to move on to the next term in the series for the next iteration of the loop.
This program will continue to print the Fibonacci series until it has reached the desired number of terms.
Let us see the example to understand how the program will work. Also people who do not know about fibonacci series will understand about it very well after watching this example.
This program will output the first 10 terms of the Fibonacci series, starting with 0 and 1 and then calculating and printing the next terms by adding the previous two.
Note that the program will print a comma and a space after each term except for the last one, which will be followed by a newline character. You can modify the program to change the formatting of the output as desired.
Now let us talk about the limitations of our program based on c programming language.
The program only works for positive integers: The program uses a for loop to iterate through the desired number of terms, and the loop counter (i) is an integer variable. This means that the program will only work for positive integer values of n. If a negative or decimal value is entered for n, the program will not produce the desired output.
The program may produce inaccurate results for large values of n: The Fibonacci series is an infinite series, meaning that it goes on indefinitely. However, the program uses variables of type 'int' to store the terms of the series, which can only hold a limited range of values. As a result, the program may produce inaccurate results for very large values of n due to integer overflow.
The program does not handle invalid input: If the user enters a value that is not a positive integer for the number of terms, the program will not handle the input properly. This could cause the program to crash or produce unexpected results.
To overcome these limitations, you could modify the program to use a different data type to store the terms of the series (such as 'long long' or 'double') or to handle invalid input more gracefully. You could also add additional functionality to the program, such as the ability to print the series in reverse or to print the sum of the terms in the series.
Let us see what will happen if we input a negative number.
let us now input a big number.
You can see that when we input 50 we get last few numbers of fibonacci series as negative. That is because of the range of int.
If you also want to learn coding then find your teacher according to your language. For me i am comfortable with hindi language and follow hindi teachers.
You can refer to my teacher below in case you are also comfortable with hindi.
I will soon call myself a trader and a small programmer together.
Happy trading and keep learning what you love.