Q:

Optimize way to find an nth Fibonacci number using c programming

belongs to collection: C Programming on Numbers

0

Optimize way to find an nth Fibonacci number using c programming

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

simply we are using the concept Fn = Fn-1 + Fn-2 .

#include<stdio.h>
int Fibonacci(int n)
{
    int f0 = 0, f1 = 1, f =0, i=0;
    if( n == 0)
        return f0;
    for (i = 2; i <= n; i++)
    {
        f = f0 + f1;
        f0 = f1;
        f1 = f;
    }
    return f1;
}
int main ()
{
    int n = 0;
    int fn = 0;
    printf("\n Enter Number to find nth Fibonacci Number =  ");
    scanf("%d", &n);
    if(n < 0)
    {
        printf("Please Enter Positive number\n\n");
        return -1;
    }
    fn =  Fibonacci(n);
    printf("\n %d Fibonacci Number = %d\n\n", n, fn);
    return 0;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Programming on Numbers

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C program to find a neon number... >>
<< How to find whether a given number is prime number...