C Program

Write a program to print Fibonacci Series

program to find the largest of n numbers and its location in an array
The Fibonacci series is a sequence where each term is the sum of the two preceding ones, starting from 1 and 1. The sequence looks like this:
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
In this program, we aim to generate and print the Fibonacci series up to a specified number of terms, entered by the user. Let’s break down the logic and the full code implementation:
#include <stdio.h> // Include the Standard Input-Output header for printf and scanf functions

// Function prototype for Fibonacci function
int Fibonacci(int x);

void main() {
int term, i;

// Prompt the user to enter the number of terms
printf("Enter the number of terms of Fibonacci Series which is going to be printed: ");
scanf("%d", &term); // Read the number of terms from the user

// Loop to print the Fibonacci series up to the given number of terms
for(i = 0; i < term; i++) {
printf("%d ", Fibonacci(i)); // Print each Fibonacci number using the Fibonacci function
}

// Add a newline after printing the series
printf("\n");
}

// Fibonacci function to calculate the Fibonacci number at position x
int Fibonacci(int x) {
if(x == 0 || x == 1) // Base case: If the position is 0 or 1, return 1
return 1;
else
// Recursive case: Fibonacci number is the sum of the two previous Fibonacci numbers
return (Fibonacci(x - 1) + Fibonacci(x - 2));
}

Explanation of the Code:

  1. Header File:

    • #include <stdio.h>: This includes the Standard Input/Output library, which is necessary for using printf() and scanf() functions.
  2. Main Function:

    • Variables:

      • term: This variable stores the number of terms that the user wants to print in the Fibonacci series.
      • i: This is used as a loop counter.
    • User Input:

      • The program prompts the user to input the number of terms for the Fibonacci sequence using printf().
      • It then reads the input using scanf() and stores it in the variable term.
    • Printing the Fibonacci Series:

      • The for loop runs from i = 0 to i < term, and in each iteration, it calls the Fibonacci(i) function to get the Fibonacci number at position i.
      • Each Fibonacci number is printed using printf(), followed by a space.
  3. Fibonacci Function:

    • The function Fibonacci(int x) is a recursive function that calculates the Fibonacci number at position x.
    • Base Case: If x is 0 or 1, the function returns 1 because the first two numbers in the Fibonacci sequence are both 1.
    • Recursive Case: For other values of x, the function recursively calls itself to compute the sum of the two preceding Fibonacci numbers (Fibonacci(x-1) + Fibonacci(x-2)).
  4. Output Formatting:

    • After printing the Fibonacci numbers, a newline (\n) is printed to move the cursor to the next line for clean output.

Sample Output:

If the user enters 6 as the number of terms, the program will output

Enter the number of terms of Fibonacci Series which is going to be printed: 6 1 1 2 3 5 8

This is the Fibonacci series for the first 6 terms: 1, 1, 2, 3, 5, 8.

How the Fibonacci Function Works:

The recursive function Fibonacci(x) computes the Fibonacci number for position x by adding the results of the two previous positions. Let’s look at an example for Fibonacci(4):

Fibonacci(4) = Fibonacci(3) + Fibonacci(2) = (Fibonacci(2) + Fibonacci(1)) + (Fibonacci(1) + Fibonacci(0)) = ((Fibonacci(1) + Fibonacci(0)) + 1) + (1 + 1) = ((1 + 1) + 1) + (1 + 1) = 2 + 2 = 3

This is how the program computes each Fibonacci number recursively.

Team Educate

About Author

Leave a comment

Your email address will not be published. Required fields are marked *

You may also like

C program to test whether a given identifier is valid or not
C Program

C Program to Test Whether a Given Identifier is Valid or Not

Rules for Valid Identifiers in C: The identifier must begin with a letter or an underscore (_). It cannot start
C program to test whether a given identifier is valid or not
C Program Compiler Design

C program to implement simple code generator

ALGORITHM:1. Start2. Get address code sequence.3. Determine current location of 3 using address (for 1st operand).4. If current location not