GoogleTag

Google Search

[Programming] Question 9: Generate Fibonacci series in C?

Learn how to write a C program to generate the Fibonacci series. This tutorial explains how to calculate and print Fibonacci numbers using iterative approach. It includes clear examples and code snippets to help you understand the logic behind the Fibonacci sequence and implement it effectively in C.

#include <stdio.h>

int main() {
	int n, first = 0, second = 1, next, i;

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

	printf("Fibonacci Series: \n");

	for (i = 0; i < n; i++) {
		printf("%d, ", first);
		next = first + second;
		first = second;
		second = next;
	}

	return 0;
} 

 

Inside the main() function:

- An integer variable n is declared to store the number of terms in the Fibonacci series.

- Integer variables first and second are initialized to 0 and 1, respectively, as the first two terms of the series.

- An integer variable next is declared to calculate the next term in the series.

- The user is prompted to enter the number of terms.

- The Fibonacci series is generated using a for loop.

- Within the loop, each term is printed, and the values of first, second, and next are updated to generate the next term.

 

 

Featured Posts

SQL Interview Questions Topics

 SQL Topics to prepare for interviews,   SQL Basics: Introduction to SQL SQL Data Types DDL (Data Definition Language): C...

Popular Posts