Fibonacci Sequence:
Fibonacci Sequence is the series of numbers in which the next term is the sum of the first two terms.
Algorithm :
step 1: Start
step 2: accept num;
step 3: i=0;
step 4: prev=0, next=1, temp=0;
step 5: Is i<num then
DO
PRINT prev
temp=prev+next
prev=next
next=temp1.
i=i+1
DONE
step 6: Stop
Flowchart:
Observe the fig2. It will be easy for you to understand the concept of Fibonacci series.
Fig2. |
Program:
#include <stdio.h> int main() { int num,i,prev=0,next=1,temp=0; printf("Enter the number:"); scanf("%d",&num); i=0; printf("Fibonacci sequence till %d numbers is:\n ",num); while(i<num) { printf("%d ",prev); temp=prev+next; prev=next; next=temp; i++; } return 0; } Output: Enter the number:10 Fibonacci sequence till 10 numbers is: 0 1 1 2 3 5 8 13 21 34
Observe the above program and the fig2..
We have declared the five variables, num, i, prev, next, temp.
num is taken as input and, prev, next, and the temp variable is initialized as zero, i is initialized as zero.
The input given to num will decide the Fibonacci length of the Fibonacci series.
The 'i' variable is used as a subscript to iterate the while loop.
The while loop will iterate till i<num.
The algorithm inside the body of the while loop is explained in fig2.
Post a Comment
Post a Comment