Uri/Beecrowd problem no - 1151 solution in C

#include <stdio.h>

int main() {
    int n, t1 = 0, t2 = 1, nextTerm = 0;

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

    printf("Fibonacci Series: ");

    for (int i = 1; i <= n; ++i) {
        if (i == 1) {
            printf("%d, ", t1);
            continue;
        }
        if (i == 2) {
            printf("%d, ", t2);
            continue;
        }
        nextTerm = t1 + t2;
        t1 = t2;
        t2 = nextTerm;

        printf("%d, ", nextTerm);
    }
    return 0;
}

This is a C program that generates the Fibonacci series. The Fibonacci series is a sequence of numbers where each number is the sum of the two preceding ones. In this program, the user is asked to enter the number of terms they want in the series.

Including the necessary header file

The program starts by including the <stdio.h> header file, which is required for input/output operations in C.

Declaring variables

Next, the program declares the necessary variables n, t1, t2, and nextTerm. - n is used to store the number of terms entered by the user. - t1 and t2 are used to store the first two terms of the series. - nextTerm is used to store the next term of the series.

Getting user input

The program prompts the user to enter the number of terms they want in the Fibonacci series using the printf function. Then, it uses the scanf function to read the user input and store it in the variable n.

Generating the Fibonacci series

The program uses a for loop to generate and print the Fibonacci series. The loop runs from i = 1 to i <= n, where i represents the current term being generated.

Inside the loop, there are three main sections: - The first if condition checks if i is equal to 1. If true, it means we are at the first term of the series, so the program prints the value of t1 using the printf function and continues to the next iteration of the loop. - The second if condition checks if i is equal to 2. If true, it means we are at the second term of the series, so the program prints the value of t2 using the printf function and continues to the next iteration of the loop. - If none of the above conditions are true, the program calculates the nextTerm by adding t1 and t2, and then updates the values of t1 and t2 for the next iteration. Finally, it prints the value of nextTerm using the printf function.

Returning the result

After the loop finishes executing, the program ends by returning 0, indicating successful execution.

This program generates the Fibonacci series based on the user's input and demonstrates the use of variables, loops, conditionals, and input/output operations in C.