Fibonacci Number - C Program
Tagged:
Fibonacci sequence/series is a sequence of numbers in which each successive number is equal to the sum of the preceding numbers.
The nth fibonacci number is:
fib(n) = fib(n-1) + fib(n-2); where fib(1) = 1 and F(2) = 1,
The first five numbers are 1, 1, 2, 3, 5
Below is a c program that will return the nth fibonacci number.
#include<stdio.h>
int main(){
int arr[100];
arr[0] = 1;
arr[1] = 1;
int n;
printf("enter n: ");
scanf("%i", &n);
int x = 2;
while(x<n){
arr[x] = arr[x-1] + arr[x-2];
x++;
}
printf("fib(%i) = %i\n",n, arr[n-1]);
return 0;
}
output:
![]()

Post new comment