• # C Program to Check Fibonacci Series using Function

Welcome to yet another informative blog post! Today, we will dive into the fascinating world of Fibonacci series and learn how to check if a given number belongs to this sequence using C programming language. Fibonacci series has a rich history and finds its application in various fields such as mathematics, computer science, and even nature. So, without further ado, let’s get started!

## Introduction

Before we delve into the implementation details, let’s understand what a Fibonacci series is. The Fibonacci series is a sequence of numbers in which each number, except for the first two, is the sum of the two preceding ones. Mathematically, it is defined as:

``````F(0) = 0
F(1) = 1
F(n) = F(n-1) + F(n-2) for n > 1``````

This series starts with 0 and 1, and continues indefinitely: 0, 1, 1, 2, 3, 5, 8, 13, 21, and so on.

Our goal is to write a C program that checks whether a given number belongs to the Fibonacci series. To achieve this, we will define a function that performs the necessary calculations.

## Checking for Fibonacci Numbers

To begin, let’s create a C program that prompts the user to enter a number and checks if it belongs to the Fibonacci series. We will accomplish this by writing a function called `isFibonacci()`.

``````#include <stdio.h>

// Function to check if a number is a Fibonacci number
int isFibonacci(int num)
{
// Fibonacci series always starts with 0 and 1
int a = 0;
int b = 1;

// Loop until we find a Fibonacci number greater than or equal to the given number
while (b < num)
{
int temp = b;
b = a + b;
a = temp;
}

// If the given number matches the Fibonacci number, return true (1). Otherwise, return false (0).
return (b == num);
}

// Main function
int main()
{
int number;

// Prompt the user to enter a number
printf("Enter a number: ");
scanf("%d", &number);

// Check if the number is a Fibonacci number
if (isFibonacci(number))
{
printf("%d is a Fibonacci number.\n", number);
}
else
{
printf("%d is not a Fibonacci number.\n", number);
}

return 0;
}``````

Let’s break down the code to better understand what’s happening.

## Understanding the Code

### The `isFibonacci()` Function

Our `isFibonacci()` function takes an integer `num` as input and returns an integer. Inside the function, we initialize two variables `a` and `b` to store the first two Fibonacci numbers, which are 0 and 1, respectively.

We then enter a loop that continues until `b` becomes greater than or equal to the given number `num`. In each iteration, we update the values of `a` and `b` by swapping them, similar to how the Fibonacci series is generated. Finally, we compare the value of `b` with `num` to check if they are equal. If they are, we return 1; otherwise, we return 0.

### The `main()` Function

In the `main()` function, we prompt the user to enter a number using `printf()` and `scanf()`. We then call the `isFibonacci()` function, passing the user input as the argument. Based on the return value of `isFibonacci()`, we display an appropriate message to the user using `printf()`.

## Testing the Program

Now that we have implemented our C program, let’s put it to the test. Compile and run the program, and you will be prompted to enter a number. Let’s try a few test cases:

1. Entering the number 5 will yield the output: “5 is a Fibonacci number.” This is because 5 is indeed a part of the Fibonacci series.

2. If we enter the number 6, the program will output: “6 is not a Fibonacci number.” As expected, 6 does not belong to the Fibonacci sequence.

Feel free to experiment with different input values and observe the output. You will notice that the program accurately determines whether a given number is part of the Fibonacci series.

## Conclusion

In this blog post, we explored the concept of Fibonacci series and learned how to write a C program to check if a number belongs to this sequence. By implementing the `isFibonacci()` function and utilizing basic loop and conditional statements, we were able to accomplish this task successfully.

Computing Fibonacci numbers can be a useful skill in various scenarios, such as implementing efficient algorithms, solving mathematical problems, or even generating aesthetically pleasing patterns. By applying the knowledge you gained from this blog post, you can now leverage the power of C programming for checking Fibonacci numbers.

Likewise, We hope you found this blog post informative and that it shed some light on the intriguing world of Fibonacci series. If you have any further questions or suggestions, feel free to leave a comment below. Happy coding!