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
- Include the Standard Input-Output Library: Use
#include <stdio.h>to include the standard input-output library, which is necessary for usingprintfandscanffunctions. - Write the Main Function: Define the
mainfunction, which is the entry point of every C program. - Declare Variables: Declare variables to store the number of terms, and the first two Fibonacci numbers.
- Input the Number of Terms: Use
scanfto take input from the user for the number of terms. - 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, andnextare declared.nwill store the number of terms, whilefirstandsecondstore the first two numbers in the Fibonacci series, initialized to 0 and 1, respectively.nextis 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. Thescanffunction reads the input and stores it in the variablen.
Step 3: Check for Valid Input
- The program checks if the input number
nis positive. Ifnis less than or equal to 0, it prompts the user to enter a positive integer.
Step 4: Generate and Print the Fibonacci Series
- If
nis valid, the program uses aforloop 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
firstandsecondnumbers. Thefirstis then updated to the value ofsecond, andsecondis 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.