C Program to Calculate Sum of First N Fibonacci Numbers
This guide explains how to write a C program that calculates the sum of the first n Fibonacci numbers. The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, starting from 0 and 1. We'll provide a complete C program with code examples, explain the formula used, and show how to interpret the results.
Introduction
The Fibonacci sequence is one of the most famous integer sequences in mathematics. It appears in various areas of mathematics, computer science, and even in nature. Calculating the sum of the first n Fibonacci numbers is a common programming exercise that helps understand loops, recursion, and basic arithmetic operations in C.
In this guide, we'll create a C program that:
- Takes an integer input n from the user
- Calculates the sum of the first n Fibonacci numbers
- Displays the result
Fibonacci Sequence
The Fibonacci sequence is defined by the recurrence relation:
Fibonacci Sequence Definition
F0 = 0
F1 = 1
Fn = Fn-1 + Fn-2 for n ≥ 2
The sequence begins as follows: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
The sum of the first n Fibonacci numbers is often denoted as Sn. For example:
- S1 = 0
- S2 = 0 + 1 = 1
- S3 = 0 + 1 + 1 = 2
- S4 = 0 + 1 + 1 + 2 = 4
C Program to Calculate Sum
Here's a complete C program that calculates the sum of the first n Fibonacci numbers:
int main() {
int n, i;
unsigned long long sum = 0, a = 0, b = 1, next;
// Get input from user
printf("Enter the number of terms: ");
scanf("%d", &n);
// Handle edge cases
if (n <= 0) {
printf("Please enter a positive integer.\n");
return 1;
}
// Calculate sum of first n Fibonacci numbers
for (i = 1; i <= n; i++) {
if (i == 1) {
sum += a;
} else if (i == 2) {
sum += b;
} else {
next = a + b;
sum += next;
a = b;
b = next;
}
}
// Display the result
printf("Sum of first %d Fibonacci numbers is: %llu\n", n, sum);
return 0;
}
The program uses a loop to generate Fibonacci numbers and accumulates their sum. It handles edge cases where n might be 0 or negative.
Formula Used
The sum of the first n Fibonacci numbers can be calculated using the following approach:
Sum of First n Fibonacci Numbers
Sn = F1 + F2 + ... + Fn
Where Fk is the k-th Fibonacci number
There's no simple closed-form formula for the sum of Fibonacci numbers, so we must compute it iteratively by generating each Fibonacci number and adding it to the sum.
Worked Example
Let's calculate the sum of the first 5 Fibonacci numbers:
| Term | Fibonacci Number | Running Sum |
|---|---|---|
| 1 | 0 | 0 |
| 2 | 1 | 0 + 1 = 1 |
| 3 | 1 | 1 + 1 = 2 |
| 4 | 2 | 2 + 2 = 4 |
| 5 | 3 | 4 + 3 = 7 |
The sum of the first 5 Fibonacci numbers is 7.
FAQ
- What is the Fibonacci sequence?
- The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, starting from 0 and 1.
- How do I calculate the sum of Fibonacci numbers?
- You can calculate the sum by generating each Fibonacci number in sequence and adding them together.
- What is the sum of the first 10 Fibonacci numbers?
- The sum of the first 10 Fibonacci numbers is 88.
- Can I use recursion to calculate the sum?
- While recursion can be used to generate Fibonacci numbers, it's generally less efficient for calculating the sum compared to an iterative approach.
- What happens if I enter a negative number?
- The program will display an error message and exit since the Fibonacci sequence is only defined for non-negative integers.