Topics Index

Find Nearest Fibonacci Number

If you need explanation Read this topic

If you need Answer Take test on this topic

Write a program to find the nearest Fibonacci number. The Fibonacci numbers are 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 etc.,. Assume that the input number will be always greater than 3. There is no upper limit to the input number.

Input (Integer) Output (Integer)
9 8 (9 is between 8 and 13 and is nearer to 8 than 13)
29 34 (29 is between 21 and 34 and is nearer to 29 than 34)
50 55 (50 is between 34 and 55 and is nearer to 55 than 34)

class FindNearestFibonacciNumber
    public static void main(String s[])
        int input = 245;
        int result = findNearestFibonacciNumber(input);
        System.out.println("The nearest Fibonacci number for " + input + " is " + result);


    public static int findNearestFibonacciNumber(int input)

Topic: for Loop In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

first find a fibonacci no's number which is greaterthan input,   and lessthan input number
ex: input 30
greater is 34
less number is 21
after that take difference of (greater number of(fibonacci),input) and
take difference of ( smaller number of(fibonacci with respect input value),input)

   in both  which difference  is less return that value
  that value is near to input
here difference 4 is small thats why  34 is nearer to 30
return 34;
int c=1,a=0,b=1;                                                                                                               // c is greater fibonacci and b small fibonacci w.r.t     input value


   return b;
    return c;
input: 27
here c=34 and b=21
here 21 is near to 27 thats why return 21(variable b);

Posted by Goutham Singarapu    2015-02-05 06:21:52

here we have to a fimd a fibomaci numbr wich is close to input numbr. i.e we will have two fib no. -one grtr than inpt. -secnd less than input.. our result wil be th num whose dfrnc wid input is lesser. ........firstly we wil find thw fib no.  lesser than inpt..take a=0,b=1,c=1 while (c<input) c=a (plus)b, a=b, b=c....... end loop... now wen cntrl comes out of the loop, it will have c as th fib no.  grtr than input....and b as fib no.  less than input.... now result=(c-input)<(input-b)?c:b)......

Posted by Asma Mujtaba Khan    2015-02-05 08:41:52

This dose is now closed and the winners are Asma Mujtaba Khan,  for 'First Correct Comment'. The 'lucky liker' is ?????????? ?????. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2015-02-06 07:56:49

© meritcampus 2019

All Rights Reserved.

Open In App