﻿ C Program: Display the first n terms of fibonacci series - w3resource

# C Exercises: Display the first n terms of Fibonacci series

## C For Loop: Exercise-35 with Solution

Write a program in C to display the first n terms of the Fibonacci series.
The series is as follows:
Fibonacci series 0 1 2 3 5 8 13 .....

Visual Presentation: Sample Solution:

C Code:

``````#include <stdio.h> // Include the standard input/output header file.

int main() {
int prv = 0, pre = 1, trm, i, n; // Declare variables for previous, current, and next terms, as well as loop counters.

// Prompt the user to input the number of terms.
printf("Input number of terms to display: ");
scanf("%d", &n); // Read the input from the user.

// Print a message indicating the number of terms.
printf("Here is the Fibonacci series up to %d terms:\n", n);

printf("%5d %5d", prv, pre); // Print the first two terms.

for (i = 3; i <= n; i++) { // Loop to generate the Fibonacci series starting from the third term.
trm = prv + pre; // Calculate the next term.
printf("%5d", trm); // Print the next term.
prv = pre; // Update the previous term.
pre = trm; // Update the current term.
}

printf("\n"); // Move to the next line after printing the series.

return 0; // Return 0 to indicate successful execution.
}

```
```

Sample Output:

```Input number of terms to  display : 10
Here is the Fibonacci series upto  to 10 terms :
0     1    1    2    3    5    8   13   21   34
```

Flowchart: C Programming Code Editor:

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿