Write a C program to print Fibonacci Series

Write a C program to print Fibonacci Series
 
Here's a C program to print the Fibonacci series:

C Programming Tutorial
#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 = 0; i < n; i++) { if (i <= 1) next = i; else { next = first + second; first = second; second = next; } printf("%d ", next); } return 0; }

In this program, we use a 'for loop' to generate and print the Fibonacci series. The user is prompted to enter the number of terms they want to print. The variables 'first' and 'second' are used to keep track of the previous two numbers in the series, while the 'next' stores the current number. Inside the loop, we check if 'i' is less than or equal to 1 to handle the initial two terms (0 and 1), and then we calculate the next number by adding the previous two numbers. The values of first, second, and next are updated accordingly in each iteration. Finally, we print the next number as part of the Fibonacci series.

When you run the program, it will ask you to enter the number of terms you want to print. After providing the input, the program will generate and display the corresponding Fibonacci series.

Post a Comment

Previous Post Next Post