C Online Compiler
Example: Fibonacci Series - Iterative Method in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Fibonacci Series - Iterative Method #include <stdio.h> int main() { // Step 1: Declare variables for the first two terms, the next term, and the number of terms. int t1 = 0, t2 = 1, nextTerm; int n, i; // Step 2: Prompt the user to enter the number of terms. printf("Enter the number of terms: "); scanf("%d", &n); // Step 3: Print a message indicating the series. printf("Fibonacci Series: "); // Step 4: Loop to generate and print the series. for (i = 1; i <= n; ++i) { // Step 4.1: Print the first term if it's the first iteration, otherwise print the current t1. printf("%d, ", t1); // Step 4.2: Calculate the next term. nextTerm = t1 + t2; // Step 4.3: Update t1 and t2 for the next iteration. t1 = t2; t2 = nextTerm; } printf("\n"); // Newline for better formatting return 0; }
Output
Clear
ADVERTISEMENTS