Program to Calculate nth Fibonacci Number in C Language

program-to-generate-nth-fibonacci-number-in-c-language

Program Description:

Write a Program to Calculate the nth Fibonacci Number in C Programming Language. The program should accept a number from the user and calculate the nth Fibonacci Number in the Fibonacci Series.

Fibonacci Series:

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 01, 1, 2, 3, 5, 8, 13, 21, 34, etc.

The formula to calculate Fibonacci Series is n = (n-1) + (n-2). 

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.”

F{n} = F{n-1} + F{n-2} so F{0} = 0 and F{1} = 1, etc.

So the zeroth Fibonacci number is . And First Fibonacci Number is 1, etc.

Here is the table which shows the first 11 Fibonacci numbers in Fibonacci Series.

nnth Fibonacci Number
00
11
21
32
43
55
68
713
821
934
1055

So if you the user want the 9th Fibonacci number, Then Program should display the value as 34. And The program also should display the error message if the user enters Invalid input.

📢 Zeroth Fibonacci Number is Zero (0) and First Fibonacci Number is 1

Excepted Inputs and Outputs:

Input:

Enter which fibonacci number(nth) you want to print : 10

Output:

10(th) Fibonacci Number in Fibonacci series is : 55

Pre-Requisites:

Program to Calculate nth Fibonacci Number in C Algorithm:

  1. Take the Input from the User and store it in variable num
  2. Check if the num is valid Input, We won’t allow the Negative numbers. If the user enters a negative number, then display an error message ( Invalid Input, Please provide valid number ) and stop the program.
  3. Initialize the first two Fibonacci numbers i.e zero(0) and One(1). We are using the variables i and j respectively.
  4. We calculate the next Fibonacci number based on the previous two Fibonacci numbers as specified in the formula – F{n} = F{n-1} + F{n-2}
  5. So Calculate the next Fibonacci number k using the previous two numbers i and j
  6. Check if the num is equal zero (0) or One(1), if so, Then display the result based on num and stop the program.
  7. If num is greater than the 1, Then we need to find the nth ( numth) Fibonacci number. To do that we use the Loop.
  8. Start from the 1 ( we use cnt variable cnt = 1) and go up to the num-1. At Each iteration
    • Move forward by assigning the  j value to  i (  i = j),  k value to  j (  j = k)
    • Finally, create the next Fibonacci number in the Fibonacci series by adding the previous two numbers i and  j (  k = i + j)
    • Increment the counter i.e cnt++
    • This loop will continue until the Loop condition cnt < num-1 becomes False
  9. Once the above loop terminated, Then we got our Nth Fibonacci Number in Fibonacci Series. Print the results onto the console using the printf statement.

Let’s translate the above pseudo-code into the C code.

Calculate nth Fibonacci Number in C Language Program:

Program Output:

Compile and run the program using your favorite IDE. We are using the GCC Compiler.

Compile the program.

$ gcc nth-fib.c

The above command generates the executable named a.out. Run it.

As you can see the program is giving the expected output. Let’s try a few more tests

nth-fibonacci-number-program-in-c-language-output

What if the user enters a negative number

The program is displaying an error message.

📢 If you try to calculate the large (nth – above 46th – depending upon Integer size) Fibonacci number, Then we might face the Integer Overflow problem. So Make sure to large datatype for variables i, j, and k.

For example, if you try to generate the 50th Fibonacci Number, You will get invalid values (Invalid valid means negative values or wrong values ) you can overcome this problem by using long long Integer for i, j, k variables.

In the Above program, We used Long int, So its max size is around 2 Giga +. So up to the 46th Fibonacci number we get the correct values but when we try to calculate the
47th Fibonacci number, We will get Negative values. Actually, the 47th Fibonacci number is  2971215073.

nth Fibonacci Number program using while loop:

The program algorithm is not going to change. We have used the for loop in the above example, Now we are going to use the while loop instead.

We also add a goto statement to take the control back to the input step. So that the user can provide the input again. ( Presently we simply terminating the program.)

Here is the modified program

Program Output:

Compile and run the C Program.

As you can see from the above output, We are getting the desired output.

Let’s look at how the program behaves to negative numbers.

As expected, The program is showing the error message and asking for the input again.

C Lang Tutorials 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...

Leave a Reply