C program to generate the Fibonacci series in c

Learn how to generate and print the Fibonacci series efficiently in C. Explore a step-by-step guide with a code example to implement the Fibonacci series algorithm using loops.



C program to generate the Fibonacci series in c first declares four integer variables – num, i, n1, n2 and n3.

It prompts the user to enter the number of terms for the Fibonacci series using the printf and scanf functions.

The first two numbers of the Fibonacci series are initialized to n1=0 and n2=1.



Using a for loop, the program generates the remaining terms of the Fibonacci series by adding the previous two terms.

The printf function is used to output the Fibonacci series to the user.

C program to generate the Fibonacci series
C program to generate the Fibonacci series
C program to generate the Fibonacci series
output



#include <stdio.h>

int main() {
    int num, i, n1=0, n2=1, n3;

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

    printf("Fibonacci Series: %d, %d, ", n1, n2);

    for(i=2; i<num; i++) {
        n3 = n1 + n2;
        printf("%d, ", n3);
        n1 = n2;
        n2 = n3;
    }

    return 0;
}

Output:

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

A program to find Fibonacci series in c

This program generates the Fibonacci series up to the given number of terms using a for loop.

  • It starts by initializing n1 and n2 as the first two terms of the series, which are 0 and 1, respectively.
  • The user is prompted to enter the number of terms they want to generate. Then,
  • The program prints the initial terms (n1 and n2) as a part of the series.
  • The for loop iterates from the third term (i = 2) up to the specified number of terms (num).
  • In each iteration, it calculates the next term n3 by adding the previous two terms (n1 and n2).
  • The value of n3 is then printed as a part of the Fibonacci series.
  • After printing n3, the program updates n1 and n2 to prepare for the next iteration.
  • It assigns the value of n2 to n1 and assigns the value of n3 to n2, effectively shifting the values for the next calculation.
  • The loop continues until it reaches the desired number of terms. Once the loop completes, the program returns 0 to indicate successful execution.



Running this program and entering a specific number of terms will display the Fibonacci series up to that number.

It showcases the concept of using loops to generate series and how each term is calculated by adding the previous two terms.

This program generates the Fibonacci series up to the given number of terms.

It uses a for loop to calculate and print the series by adding the previous two terms.

The initial values n1 and n2 are set as 0 and 1 respectively. The loop iterates from the third term to the given number of terms.

At each iteration, n3 is calculated as the sum of n1 and n2. The value of n3 is printed, and then the values of n1 and n2 are updated accordingly.

The loop continues until the desired number of terms is reached. Finally, the program returns 0 to indicate successful execution.

admin https://study-from-here.com

Digital Marketing Consultants and Social Media Marketing Expert with over 3 years of rich experience in various Branding, Promotions, business directories, On pages and off page optimization, Link building Advertising, Research, paid advertisement, content writing and marketing

You May Also Like

More From Author

1 Comment

Add yours

+ Leave a Comment