C Program to Print the Fibonacci Series

Introduction

The Fibonacci series is a sequence where each number is the sum of the two preceding ones, starting from 0 and 1. This guide will show you how to write a C program to generate and print the Fibonacci series up to a specified number of terms provided by the user.

Problem Statement

Create a C program that:

  • Takes an integer input from the user representing the number of terms.
  • Prints the Fibonacci series up to the specified number of terms.

Example:

  • Input: 10 (first 10 Fibonacci numbers)
  • Output: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34

Solution Steps

  1. Include the Standard Input-Output Library: Use #include <stdio.h> to include the standard input-output library, which is necessary for using printf and scanf functions.
  2. Write the Main Function: Define the main function, which is the entry point of every C program.
  3. Declare Variables: Declare variables to store the number of terms, and the first two Fibonacci numbers.
  4. Input the Number of Terms: Use scanf to take input from the user for the number of terms.
  5. Generate and Print the Fibonacci Series: Use a loop to generate and print each Fibonacci number up to the specified number of terms.

C Program

#include <stdio.h>

/**
 * C Program to Print the Fibonacci Series
 * Author: https://www.javaguides.net/
 */
int main() {
    // Step 1: Declare variables to hold the number of terms and Fibonacci numbers
    int n, first = 0, second = 1, next;

    // Step 2: Prompt the user to enter the number of terms
    printf("Enter the number of Fibonacci terms: ");
    scanf("%d", &n);

    // Step 3: Check if the number of terms is valid
    if (n <= 0) {
        printf("Please enter a positive integer.\n");
    } else {
        // Step 4: Generate and print the Fibonacci series
        printf("Fibonacci Series: ");

        for (int i = 1; i <= n; i++) {
            if (i == 1) {
                printf("%d", first); // Print the first Fibonacci number
                continue;
            }
            if (i == 2) {
                printf(", %d", second); // Print the second Fibonacci number
                continue;
            }
            next = first + second; // Calculate the next Fibonacci number
            first = second; // Update first
            second = next; // Update second

            printf(", %d", next); // Print the next Fibonacci number
        }
        printf("\n"); // Newline for clean output
    }

    return 0;  // Step 5: Return 0 to indicate successful execution
}

Explanation

Step 1: Declare Variables

  • Variables n, first, second, and next are declared. n will store the number of terms, while first and second store the first two numbers in the Fibonacci series, initialized to 0 and 1, respectively. next is used to calculate the subsequent Fibonacci numbers.

Step 2: Input the Number of Terms

  • The program prompts the user to enter the number of Fibonacci terms using printf. The scanf function reads the input and stores it in the variable n.

Step 3: Check for Valid Input

  • The program checks if the input number n is positive. If n is less than or equal to 0, it prompts the user to enter a positive integer.

Step 4: Generate and Print the Fibonacci Series

  • If n is valid, the program uses a for loop to generate and print the Fibonacci series:
    • First and Second Terms: The first two terms, 0 and 1, are printed directly.
    • Subsequent Terms: For the subsequent terms, the next Fibonacci number is calculated by summing the first and second numbers. The first is then updated to the value of second, and second is updated to the newly calculated Fibonacci number. This continues until the loop completes.

Step 5: Return 0

  • The return 0; statement indicates that the program executed successfully.

Output Example

Example 1:

Enter the number of Fibonacci terms: 10
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34

Example 2:

Enter the number of Fibonacci terms: 5
Fibonacci Series: 0, 1, 1, 2, 3

Invalid Input Example:

Enter the number of Fibonacci terms: 0
Please enter a positive integer.

Conclusion

This C program demonstrates how to generate and print the Fibonacci series up to a specified number of terms. It covers basic concepts such as loops, conditional statements, and handling user input, making it a useful example for beginners learning C programming.

Leave a Comment

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

Scroll to Top