FIBONACCI METHOD IN C
NOTE:
The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, usually starting with 0 and 1. This program defines that generates and prints the Fibonacci series up to a specified number of terms. The main
function takes user input for the number of terms and then to display the series.
This method to do the calculation between the previous value and the current value to get the next value.
{ 0 1 1 2 3 5 8 13 21 . . . . . . . . . . n }
// FIBONACCI METHOD
___________________________________________________________________________________
#include<stdio.h>
void main()
{
int a=0,b=1,i,c,num;
printf("how many range of fibonacci series needed:");
scanf("%d",&num);
printf("%d\t %d\t",a,b);Prints the current value of first 2
, which represents the current Fibonacci number.
for(i=2;i<=num;i++) // The loop iterates num times, generating the specified number of terms in the Fibonacci series.
{
c=a+b; // add the previous values c=0+1 = 1;
printf("%d\t",c);
a=b; //previous value for first a=1;
//Updates the values of first
and second
to progress to the next Fibonacci number in the series.
b=c; // current value for second b=1
}
}
In this fibonacci method can be implement in several ways.like in Recursion,With range of limit,using array ...We will know about it in later posts.
__________________________________________________________________________________
OUTPUT
how many range of fibonacci series needed:10
0 1 1 2 3 5 8 13 21 34 55
for reference:
c=a+b;
a=0,1,1,2,3,5;
b=1,1,2,3,5,8;
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
c=0+1=1 ;
c=1+1=2;
c=1+2=3;
c=2+3=5;
c=3+5=8 ;
c=5+8=13;
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
output =1,2.3,5.8,13....
No comments:
Post a Comment