Program to generate Fibonacci series in C | C program to generate Fibonacci Series up to a given number
Program Description :
Write a Program to generate the Fibonacci series in C language. The program will accept a number from the user and generates the Fibonacci series up to the provided number. We are going to use the C Language loops to generate the Fibonacci series.
Pre-Requisites:
It is recommended to know the C Loops. Please go through the following articles to understand the C loops.
- While Loop in C Language
- For Loop in C Language with Example Programs
- do while loop in Language with example programs
What is the Fibonacci Number?
The Fibonacci series is a series of whole numbers in which each number is the sum of the two preceding numbers. Beginning with and 1, the sequence of Fibonacci numbers would be 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, etc.
The formula to calculate Fibonacci Series is n = (n-1) + (n-2).
F{n} = F{n-1} + F{n-2} so F{0} = 0 and F{1} = 1, etc.
where (n-1) means “the last number before n in the series” and (n-2) refers to “the second last one before n in the series.”
Here are the first few Fibonacci numbers
Fibonacci Series Table:
The first two Fibonacci numbers are and 1. (base Fibonacci numbers)
n-2 | n-1 | n |
---|---|---|
– | – | 0 |
– | – | 1 |
0 | 1 | 1 ( 0 + 1 = 1) |
1 | 1 | 2 ( 1 + 1 = 2) |
1 | 2 | 3 ( 1 + 2 = 3) |
2 | 3 | 5 ( 2 + 3 = 5) |
3 | 5 | 8 ( 3 + 5 = 8) |
5 | 8 | 13 ( 5 + 8 = 13) |
8 | 13 | 21 ( 8 + 13 = 21) |
13 | 21 | 34 ( 13 + 21 = 34) |
Program to generate Fibonacci Series in C Algorithm:
- Start the program by taking input from the user. And store the input to a variable num.
- Check if the given number is positive or negative. If num is negative, Display the error message. As we don’t have the Fibonacci series for the Negative numbers.
- If the user enters a positive number, We proceed further and generate the Fibonacci series up to the given number.
- Initialize the two variable i with and j with 1, These are our base Fibonacci numbers.
- Now add these numbers to form the next Fibonacci number in the series. We are using a variable k as the next Fibonacci number in the series. So add i and j to create k ( i.e k = i + j)
- Let’s start the loop, The loop will continue until the Present Fibonacci number ( k ) becomes equal to or greater than the user-provided number ( num) – Here is our loop condition, k <= num
- At Each Iteration:
- Print the present Fibonacci number k
- Then move forward by assigning the j value i ( i = j), k value to j ( j = k)
- Finally, create the next number in the Fibonacci series by adding i and j ( k = i + j)
- This loop will continue until the Loop condition k <= num becomes False
- Once the loop is terminated, That means the present Fibonacci number is reached the user-provided number (num). So we got the Fibonacci series up to the given number num.
- Stop the Program.
Fibonacci Series Program in C using while loop:
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
/* Program : Fibonacci series upto user given number */ #include<stdio.h> int main() { // Take the input from the user. int i,j,k,num; printf("Enter any positive Number : "); scanf("%d",&num); /* * because No Fibonacci series for Negative * Numbers,. please enter more than 0. ex: 1,50,100. */ // Check if the number is positive if(num > 0) { printf("Fibonacci series is :"); // initialize first two fibonacci numbers i.e 0, 1 i=0; j=1; printf(" %d %d ",i,j); // n is equal to (n-1) + (n-2) k=i+j; // check if the 'num' is grether than 'k' while(k <= num) { printf("%d ",k); i=j; j=k; k=i+j; } } else { // 'num' is Negative number. Display error. printf("No fibonacci series for Negative Numbers"); } printf("\n"); } |
As you can see from the above program, We only allow the Positive number from the user and display an error message on the Negative number.
Program Output:
Let’s Compile and Run the program, We are going to use the GCC compiler to compile the program under the Linux operating system.
Compile the program
$ gcc fibonacci-series-1.c
This generates the a.out executable file. Run the executable using ./a.out command.
1 2 3 4 |
$ ./a.out Enter any positive Number : 50 Fibonacci series is : 0 1 1 2 3 5 8 13 21 34 $ |
We have entered the 50, So the program generated the Fibonacci series 0 1 1 2 3 5 8 13 21 34, It contains all Fibonacci numbers up to the provided number 50. So the program is working as excepted.
Let’s run a few more times.
1 2 3 4 5 6 7 8 9 10 |
$ ./a.out Enter any positive Number : 200 Fibonacci series is : 0 1 1 2 3 5 8 13 21 34 55 89 144 $ ./a.out Enter any positive Number : 10 Fibonacci series is : 0 1 1 2 3 5 8 $ ./a.out Enter any positive Number : 1000 Fibonacci series is : 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 $ |
What if the user enters the Negative value
1 2 3 4 |
$ ./a.out Enter any positive Number : -34 No fibonacci series for Negative Numbers $ |
As you can see from the above output, The program displayed an error message.
Fibonacci Series in C Language using the for loop:
Let’s write the Fibonacci series program using the for loop. The Fibonacci series logic is going to be the same, but we are going to add some additional functionality like Prompting the user again for the Input ( If the user enters Invalid Input).
To Prompt the user for Input again, We are going to use the Goto Statement in C language. The Goto statement is a Jump Statement used to jump from one statement in a function from another statement. If the user enters Invalid input, We are going to take the program control to start of the program by using the Goto Statement.
Here is the Fibonacci series program using for loop.
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
/* Program : Fibonacci series upto user given number */ #include<stdio.h> int main() { // goto statement INPUT Label INPUT: // Take the input from the user. int i,j,k,num; printf("Enter any positive Number : "); scanf("%d",&num); /* * because No Fibonacci series for Negative * Numbers,. please enter more than 0. ex: 1,50,100. */ // Check if the number is positive if(num > 0) { printf("Fibonacci series is :"); // initialize first two fibonacci numbers i.e 0, 1 i=0; j=1; printf(" %d %d ",i,j); // n is equal to (n-1) + (n-2) k=i+j; // check if the 'num' is grether than 'k' // We are going to do all updates as part of the // for loop 'update' option. for (; k <= num; i=j, j=k, k = i+j) { printf("%d ", k); } } else { // 'num' is Negative number. Display error. printf("No fibonacci series for Negative Numbers \n"); goto INPUT; } printf("\n"); } |
A couple of things to Note here, We added the goto INPUT and INPUT label to handle the invalid input case.
And We moved all our update operations ( step 7 in the above algorithm) i.e i=j, j=k, k = i+j are moved to for loop update step. We can have them inside the loop as well.
for (; k <= num; i=j, j=k, k = i+j)
We can also further modify it to include the Intialization step as well like below.
for (k=i+j; k <= num; i=j, j=k, k = i+j)
Program Output:
Here is the output of the program
1 2 3 4 5 6 7 8 9 10 11 |
$ gcc fibonacci-series-1.c $ ./a.out Enter any positive Number : 20 Fibonacci series is : 0 1 1 2 3 5 8 13 $ ./a.out Enter any positive Number : 100 Fibonacci series is : 0 1 1 2 3 5 8 13 21 34 55 89 $ ./a.out Enter any positive Number : 1 Fibonacci series is : 0 1 1 $ |
The program behavior on Invalid Inputs
1 2 3 4 5 6 7 8 |
$ ./a.out Enter any positive Number : -245 No fibonacci series for Negative Numbers Enter any positive Number : -21 No fibonacci series for Negative Numbers Enter any positive Number : 2 Fibonacci series is : 0 1 1 2 $ |
As you can see from the above output, The program asked for the user input again. It will prompt the user until the user provides a valid number.
Exercises:
- Rewrite the above program using the do while loop and goto statement.
6 Responses
[…] In our previous article, we looked at the C program to generate Fibonacci Series up to a given number […]
[…] C Program to generate Fibonacci Series up to a given number […]
[…] C Program to generate Fibonacci Series up to a given number […]
[…] C Program to generate Fibonacci Series up to a given number […]
[…] C Program to generate Fibonacci Series up to a given number […]
[…] C Program to generate Fibonacci Series up to a given number […]