C program Fibonacci series and Fibonacci number
This C program prints Fibonacci number series to a given number x. The Fibonacci series starts with two number 0 and 1, and then successive numbers are find by its previous two number in the series.
Fibonacci series
start F.series = {0,1} next number in the series F.series = {0,1,0+1} next number in the series F.series = {0,1,1,1+1} next number in the series F.series = {0,1,1,2,1+2} process continue till next number < x
Fibonacci series - C program
The C program read a integer value x from user and then prints fibonacci series numbers less than integer value x.
#include <stdio.h>
int main() {
int x=0;
int cF=0,pF=1;
printf("\n print Fibonacci series");
printf("\nEnter value x :");
scanf("%d",&x);
printf("\n Fibonacci Series :");
do {
printf( "%d ",cF);
cF= cF + pF;
pF=cF-pF;
} while (cF < x );
return 0;
}
Output
print Fibonacci series Enter value x : 15 Fibonacci Series : 0 1 1 2 3 5 8 13
It is a C program which finds nth Fibonacci number using recursive function call. The program answers to the question what is value of 7th fibonacci number.
Fibonacci number
int series(f) { if( f<=1) return f; return ( series(f-1) + series(f-2) ) }
Fibonacci number recursive function - C programming code
The C program read a integer value n from user and then finds nth Fibonacci number.
#include <stdio.h>
int series(int f) {
if (f ==0)
return 0;
else if (f==1)
return 1;
else
return (series(f-1) + series(f-2));
}
int main() {
int n=0;
printf("\n Find nth Fibonacci number ");
printf("\nEnter value for n :");
scanf("%d",&n);
printf ( " nth fibonacci number is: %d " ,series(n-1));
return 0;
}
Fibonacci number - c program output
Find nth Fibonacci number Enter value for n 10 nth fibonacci number is : 34
Comments
Post a Comment