Fibonacci Number - C Program

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:

 fibonacci output

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.