Fibonacci Sequence
Source: Wikipedia |
Fibonacci sequence is a series of numbers arranged such that the number in nth place is the sum of (n-1)th and (n-2)th element in the sequence, starting with 0,1. In simple words, current place number is the sum of 2 numbers behind it in the series given the first two numbers be 0 and 1.
Fibonacci Sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, . . . .
Mathematical Rule for Fibonacci Sequence:
Concepts in C to know to make the program to compute Fibonacci Sequence:
- Loops
- Operators
- Loop break and continue statements
Algorithm for Fibonacci Sequence
- Start
- Declare necessary variables
- Set first number's value to 0 and second to 1
- Take input for nth term
- Start loop from 1 to n(inclusive)
- Print first number
- Sum up first and second number and store it in a variable
- Set first number's value to current second number's
- Set current second number's value to the stored sum
- End
Flowchart for Fibonacci Sequence
C program for Fibonacci Sequence:
#include <stdio.h>int main() { int n, nTerm, fTerm = 0, sTerm = 1; printf("Enter the number of terms: "); scanf("%d", &n); printf("Required Fibonacci Series is: "); for (i = 1; i <= n; ++i) { printf("%d, ", fTerm); nextTerm = fTerm + sTerm; fTerm = sTerm; sTerm = nTerm; }}
Output:
Enter the number of terms: 7
Required Fibonacci Series is: 0,1,1,2,3,5,8
If you got any problems or queries, then please comment below. We will try to clear your doubts as soon as possible.
0 Comments