Program to generate Fibonacci series in C | C program to generate Fibonacci Series up to a given number

fibonacci-series-in-c-program-upto-a-number-using-loops

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.

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-2n-1n
0
1
011 ( 0 + 1 = 1)
112 ( 1 + 1 = 2)
123 ( 1 + 2 = 3)
235 ( 2 + 3 = 5)
358 ( 3 + 5 = 8)
5813 ( 5 + 8 = 13)
81321 ( 8 + 13 = 21)
132134 ( 13 + 21 = 34)

Program to generate Fibonacci Series in C Algorithm:

  1. Start the program by taking input from the user. And store the input to a variable num.
  2. 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.
  3. If the user enters a positive number, We proceed further and generate the Fibonacci series up to the given number.
  4. Initialize the two variable i with and j with 1, These are our base Fibonacci numbers.
  5. 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)
  6. 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
  7. 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
  8. 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.
  9. Stop the Program.

Fibonacci Series Program in C using while loop:

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.

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.

What if the user enters the Negative value

As you can see from the above output, The program displayed an error message.

Program-to-generate-fibonacci-series-in-c-output

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.

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

The program behavior on Invalid Inputs

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.

More Programs on Fibonacci Series :

  1. C Program to generate first n Fibonacci Numbers.
  2. C Program to generate nth Fibonacci Number.

Learn More about Fibonacci Numbers:

C Tutorial Index:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

6 Responses

  1. […] In our previous article, we looked at the C program to generate Fibonacci Series up to a given number […]

  2. […] C Program to generate Fibonacci Series up to a given number […]

  3. […] C Program to generate Fibonacci Series up to a given number […]

  4. […] C Program to generate Fibonacci Series up to a given number […]

  5. […] C Program to generate Fibonacci Series up to a given number […]

  6. […] C Program to generate Fibonacci Series up to a given number […]

Leave a Reply