Fibonacci Roots

QUESTION:

The first two terms in the Fibonacci sequence are 0 and 1, respectively, and each subsequent term is the sum of the previous two. Using this definition to calculate the first several terms in the sequence, we get
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
Let us define the Fibonacci roots of a positive integer n to be the two smallest consecutive Fibonacci numbers whose sum is greater than or equal to n.

Input Format:
Input consists of a single integer which corresponds to n.
Assumption:
Assume that n is less than or equal to 2000.
Output Format:
Output consists of integers, separated by a space.

Sample Input 1 :
31
Sample Output 1:
13 21

Sample Input 2 :
89
Sample Output 2:
34 55

SOLUTION:

#include<stdio.h>
int main()
{
    int a=0,b=1,c=0,n;
    scanf("%d",&n);
    while(1)
    {
        c=a+b;
        if(c>=n)
        goto END;
        a=b;
        b=c;
    }
    END: printf("%d %d",a,b);
    return 0;
}

Comments

Popular posts from this blog

Implementing a Fixed size Queue of maximum size 5 using an array

Implementing a Dynamically sized stack using array

Functions - Null Matrix