Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

2. Easy Code of fibonacci series in c program using for loop.

C
O
D
E

IN C PROGRAM

CODE:

#include <stdio.h>

int main()

{
int n, first = 0, second = 1, next;

printf(“Enter the number of terms: “);
scanf(“%d”, &n);

printf(“Fibonacci Series: “);

for (int i = 1; i <= n; i++) {
if (i == 1) {
printf(“%d, “, first);
continue;
}
if (i == 2) {
printf(“%d, “, second);
continue;
}
next = first + second;
first = second;
second = next;
printf(“%d, “, next);
}

printf(“\n”);

return 0;
}

Table of Contents

P
R
O
G
R
A
M
I
N
G

Fibonacci series in c program
c program

E X E C U T E

Execution of fibonacci series in c program

Execution of fibonacci series in c
c program

 

E X P L A N A T I O N

IN C PROGRAM

Overview

The provided C code is a program that generates and prints a Fibonacci series based on user input. It uses a for loop to calculate and display the Fibonacci sequence. Let’s break down the code into its components and explain each part.

Code Structure

The code consists of several key parts:

  1. Header File Inclusion

Overview

The provided C code is a program that generates and prints a Fibonacci series based on user input. It uses a for loop to calculate and display the Fibonacci sequence. Let’s break down the code into its components and explain each part.

Code Structure

The code consists of several key parts:

  1. Header File Inclusion#include <stdio.h>

  2. Main Function
    int main() {
    // Code
    return 0;
    }

    The main function is the entry point of the program, where the program execution begins. It returns an integer value (0 in this case) to indicate the success of the program.

  3. Variable Declarations
    int n, first = 0, second = 1, next;

    Here, three integer variables are declared: n, first, second, and next. These variables are used to store user input and intermediate values during the Fibonacci series generation.
  4. User Input
    printf(“Enter the number of terms: “);
    scanf(“%d”, &n);

    The program prompts the user to enter the number of terms they want in the Fibonacci series. The input is read using scanf and stored in the variable n.

    • Print Statement
      printf(“Fibonacci Series: “);

      This line is a print statement that informs the user that the following numbers will be the Fibonacci series.

    • For Loop for Fibonacci Calculation and Printing
      for (int i = 0; i < n; i++)
      {
      // Code
      }

      This is the heart of the program. It uses a for loop to generate and print the Fibonacci series. The loop iterates from i = 0 to i < n, where n is the user-provided number of terms.

  5. Fibonacci Calculation

    Now, let’s examine the logic inside the for loop to understand how the Fibonacci series is generated and printed.

    if (i <= 1)
    next = i;
    else
    {
    next = first + second;
    first = second;
    second = next;
    }
    printf(“%d “, next);
     

    The code within the for loop calculates and prints the next term in the Fibonacci series. It does this using an if-else statement to handle the initial cases (i.e., the first two terms) and the subsequent terms differently.

    • Initial Cases (i <= 1):
      • When i is 0 or 1 (the first two terms of the Fibonacci sequence), next is set to the value of i. In this case, first and second remain unchanged.
    • Subsequent Terms (i > 1):
      • For terms beyond the first two, next is calculated as the sum of first and second, which represent the previous two terms. After the calculation, the values of first and second are updated. first takes the value of second, and second takes the value of next.

    Finally, the value of next is printed, followed by a space to separate it from the next term. This process repeats for each term in the series until the loop terminates.

    Output

    The generated Fibonacci terms are printed on the screen as they are calculated. The loop iterates n times, so the user gets the requested number of terms in the Fibonacci series.

    Program Execution

    Here’s a step-by-step breakdown of how the program works:

    1. The program starts by including the necessary header file and defining the main function.
    2. It declares four integer variables: n (for user input) and first, second, and next for Fibonacci series calculations.
    3. The user is prompted to enter the number of terms they want in the Fibonacci series.
    4. The user’s input is read and stored in the variable n.
    5. The program prints a message indicating that it’s about to display the Fibonacci series.
    6. The for loop starts to generate and print the series.a. If i is 0 or 1, the value of next is set to i.b. For all other values of i, next is calculated as the sum of first and second. The variables first and second are updated accordingly.c. The value of next is printed, followed by a space.
    7. The loop continues to run until i reaches n - 1, ensuring the requested number of terms is generated.
    8. The program finishes execution, and the final Fibonacci series is displayed on the screen.

    Fibonacci Series Generation

    The Fibonacci series is generated as follows:

    • The first two terms are fixed at 0 and 1.
    • Subsequent terms are calculated by adding the previous two terms.
    • For each iteration, the program prints the next term, which is the sum of the first and second terms.
    • first and second are updated accordingly for the next iteration.

    Example Output

    Let’s say the user enters 10 as the number of terms. The program will generate and print the first 10 terms of the Fibonacci series, which will look like this:

    mathematical
    Enter the number of terms: 10

    Fibonacci Series: 0 1 1 2 3 5 8 13 21 34

    Each term is separated by a space, and the series ends after displaying the 10th term.

    Conclusion

    This C program demonstrates how to generate and print a Fibonacci series using a for loop. It takes user input to determine the number of terms to generate and employs a straightforward logic for calculating each term in the series. The output is presented in a user-friendly format, making it easy to visualize the Fibonacci sequence based on the user’s input.

 

MORE PROGRAM :

BEST C PROGRAMMING BOOK :

 

 

Leave a Comment