Sum of fibonacci series in c; Through this tutorial, we will learn how to find sum of fibonacci series using for loop, while loop and function in c programs.
Programs to Sum of Fibonacci Series in C
Let’s use the following programs to find sum of fibonacci series using while loop, for loop and function in c:
- C Program to Sum of Fibonacci Series using For Loop
- C Program to Sum of Fibonacci Series using While Loop
- C Program to Sum of Fibonacci Series using Function
C Program to Sum of Fibonacci Series using For Loop
#include <stdio.h> int main() { int Number, Next, Second = 1, sum = 0; printf("\n Please Enter the Range Number: "); scanf("%d",&Number); for(int First = 0; First <= Number;) { printf("%d ", First); sum = sum + First; Next = First + Second; First = Second; Second = Next; } printf("\nThe Sum = %d\n", sum); }
The output of the above c program; is as follows:
Please Enter the Range Number: 10 0 1 1 2 3 5 8 The Sum = 20
C Program to Sum of Fibonacci Series using While Loop
#include<stdio.h> int main() { int Number, First = 0, Second = 1, Next = 0, sum = 0; printf("Enter Maximum Number for Fibonacci Series = "); scanf("%d", &Number); printf("First %d Fibonacci Series Numbers:\n", Number); while( First <= Number) { printf("%d ", First); sum = sum + First; Next = First + Second; First = Second; Second = Next; } printf("\nThe Sum of Fibonacci Series Numbers = %d\n", sum); }
The output of the above c program; is as follows:
Enter Maximum Number for Fibonacci Series = 5 First 5 Fibonacci Series Numbers: 0 1 1 2 3 5 The Sum of Fibonacci Series Numbers = 12
C Program to Sum of Fibonacci Series using Function
#include<stdio.h> int fibonacciSeries(int Number) { if ( Number == 0 ) return 0; else if ( Number == 1 ) return 1; else return ( fibonacciSeries(Number - 1) + fibonacciSeries(Number - 2) ); } int main() { int Number, i = 0, j, sum = 0; printf("\nEnter Maximum Number = "); scanf("%d", &Number); printf("Fibonacci Series Numbers up to %d:\n", Number); for ( j = 0 ; j < Number; j++ ) { printf("%d ", fibonacciSeries(j)); sum = sum + fibonacciSeries(j); } printf("\nThe Sum = %d\n", sum); }
The output of the above c program; is as follows:
Enter Maximum Number = 5 Fibonacci Series Numbers up to 5: 0 1 1 2 3 The Sum = 7