Program to Generate First n Fibonacci Numbers in C

generate-first-n-fibonacci-numbers-in-c-language

Program Description:

Write a Program to Generate First n Fibonacci Numbers in C language. The program should accept a positive number ( n) from the user and generate first n Fibonacci Numbers. For example, If the user enters 3, The program should generate the first 3 Fibonacci numbers, which are 0, 1, 1. The program should display an error message on Invalid Input.

Here is the program expected Input and Output:

Example Input and Output:

Input:

How many fibonacci numbers do you want to print : 5

Output:

First 5 Fibonacci numbers are : 0 1 1 2 3

Also Read:

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

Pre-Requisites:

It is good to have an understanding of C Loops. Please go through the following articles to learn more about the C Loops

First n Fibonacci numbers in C Program Algorithm:

  1. Take the input number from the user and store it as a variable num
  2. Check if the num is positive or negative, If the num is Negative, Display the error message – Invalid Input, Please provide valid number
  3. If the num is a positive number, Proceed to the next step
  4. Initialize the first two Fibonacci numbers i = 0 and j = 1
  5. Check if the num is equal to 1, If so, print the first Fibonacci number and return. Otherwise, print the first two base Fibonacci numbers
  6. Then calculate the next Fibonacci number k using the i and j ( i.e k = i + j )
  7. We use cnt variable as the counter. As we already printed the first two Fibonacci numbers, The Counter cnt starts from 2. So update cnt = 2.
  8. Start the loop, The will continue until the num > cnt
  9. 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)
    • Also, Increment the cnt by 1.
    • This loop will continue until the Loop condition num > cnt becomes False
  10. Once we reach the n Fibonacci numbers, The loop will terminate and control comes out of the loop.

Program to Print first n Fibonacci numbers in C Language using while loop:

Program Output:

Compile and Run the program using your IDE. We are using the GCC Compiler on Linux OS.

Here is the output of the program

Test Case 1: When the user provided a positive number:

We are getting the expected output. When user entered num as 5, The program displayed the First 5 Fibonacci numbers which are 0 1 1 2 3

Similarly, Program gave the first 10 Fibonacci numbers when the user provided num as 100 1 1 2 3 5 8 13 21 34

Test 2: When the user provided a Negative Number:

As you can see from the above output, When the user provided a Negative number, The program is displaying the error message Invalid Input, Please provide valid number

First-n-fibonacci-numbers-in-c-program-output

📢 Note: This program generates Fibonacci numbers up to any number but You need to use sufficient Datatype for our i, j, and k variables. In the above program, we used an Integer datatype for i, j, and k variables so that it will generate up to 2+ Giga numbers. After that, it will give Negative numbers. Therefore, depending on your needs, use long int or long long int. To learn about the fundamental C datatypes and their sizes, please refer to the below article

First n Fibonacci numbers program in C using for loop:

Let’s look write the above program using the for loop as well.

Program Output:

We are getting the expected output.

Generate First n Fibonacci numbers Use the Goto statement on Invalid Input:

So far we are displaying the error message when the user enters Invalid input ( Invalid Input, Please provide valid number ).

Now, If the user enters invalid Input, Let’s show the error message and also allow the user to provide the new input. To do that, We use the Goto statement in C. goto statement helps us to take the program control back to the start of the program

📢 The Goto statement is used to jump from one statement to another within a function.

If you notice, We have added a INPUT: (at line 10) and goto INPUT; (at line 17). The INPUT: is the label. So whenever the user enters Invalid inputs like Negative numbers, Then the goto INPUT; Statement will be executed, which takes the program control back to the label INPUT:. So the printf statement will be printed with the message and we can take the input from the user again using the scanf.

Program Output:

As you can see from the output above, the program displayed an error notice and requested for the user’s input once more when the user entered a negative number. This continued until the user entered a valid input.

More Programs on Fibonacci Series :

  1. C Program to generate Fibonacci Numbers upto given Number.
  2. C Program to generate nth Fibonacci Number.

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

3 Responses

  1. […] C Program to Print First n Fibonacci numbers […]

  2. […] C Program to Print First n Fibonacci numbers […]

  3. […] C Program to Print First n Fibonacci numbers […]

Leave a Reply