# Fibonacci Series in C using Function

## Fibonacci Series in C using Function Program Description:

Write a Program to generate the Fibonacci Series in C using Function. The program should accept an integer from the user and print the Fibonacci series (Fibonacci numbers) up to the given number using a function.

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

### Example Input and Output:

Input:

Enter a Number: 1000

Output:

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987

## Fibonacci Series in C using Function Program Algorithm:

1. Take the input from the user and store it in a variable named num
2. If the num is a negative number, display an error message and allow the user to try again.
3. Once we got the valid num. Call the generateFibonacci function by passing the num to generate the Fibonacci number. The program control jumps to generateFibonaccifunction.
4. The generateFibonaccifunction accepts a number as input ( num) and generates the Fibonacci numbers up to the number num.
• To generate the Fibonacci series, We use the F{n}&nbsp;=&nbsp;F{n-1}&nbsp;+&nbsp;F{n-2} formula.
• Take three variables a, b, and c. Initialize the first two Fibonacci numbers with and 1. i.e a = 0 and b = 1
• Then generate the third Fibonacci number by adding the previous two numbers in the Fibonacci series. Here we used the variable c to track the present Fibonacci series.
• Repeat the above step using a Loop until we reach the user-provided number num. i.e while(c <= num)
• At Each Iteration:
• Print the present Fibonacci number  c
• Then move forward by assigning the  b value  a ( a = b), c value to b ( b = c)
• Finally, generate the next number in the Fibonacci series by adding a and b ( c = a + b)
• This loop will continue until the Loop condition c <= num becomes False
5. Once we reached the num, The loop will stop and the function generateFibonacci will return. ( here we are not returning anything, So the return type of the generateFibonacci function is void). The program control comes back to main() function.

## Fibonacci Series in C using Function Program:

### Program Explanation:

In the above Fibonacci series program, We have used a function named generateFibonacci , The generateFibonacci series function expects an integer number ( num) as input and generates the Fibonacci number up to num

The generateFibonacci function doesn’t return anything, So we have specified the return type as the void.

We called the generateFibonacci from the main() function.

As the generateFibonacci function is defined before it is called, So there is no need to specify the function declaration, That is why we haven’t added the function declaration at the top of the program for the generateFibonacci function.

### Program Output:

Compile the program using the C compiler, We are using the GCC compiler to compile the program under the ubuntu operating system.

gcc fib-function.c

The above command generates the executable file a.out. Run the program.

Test-Case 1: Enter a positive number and check the results.

Program generating the Fibonacci series correctly, Let’s try few more examples.

Test Case 2: Enter a Negative number:

As you can see from the above output, The program is generating an error message ( Error: Invalid Input, Please enter positive number) when the user enters a negative number. And Program is also asking for the user input again. We are using the Goto statement to take the program control back to the INPUT label.

## Related C Language Programs:

1. C Program to find power of number without Loops
2. C Program to Find all Factors of a Number
3. C Program to find all Prime Factors of number
4. C Program to Calculate the GCD or HCF of Two Number
5. C Program to Calculate the LCM of two Numbers
6. C Program to Check Palindrome Number
7. C Program to Check 3-Digit and N-Digit Armstrong Number
8. C Program to Generate Armstrong Numbers upto N ( User-Provided Number)
9. C Program to Generate Armstrong Numbers between two Intervals

Venkatesh

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

### 3 Responses

1. […] Fibonacci Series using function […]

2. […] Fibonacci Series using function […]

3. […] Fibonacci Series using function […]