Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Find the next fibonacci number

  • Difficulty Level : Expert
  • Last Updated : 10 Mar, 2021

Given a fibonacci number N, the task is to find the next fibonacci number.
Examples: 
 

Input: N = 5 
Output:
8 is the next fibonacci number after 5
Input: N = 3 
Output:
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

 

Approach: The ratio of two adjacent numbers in the Fibonacci series rapidly approaches ((1 + sqrt(5)) / 2). So if N is multiplied by ((1 + sqrt(5)) / 2) and round it, the resultant number will be the next fibonacci number.
Below is the implementation of the above approach: 
 

C++




// C++ implementation of the approach
#include<bits/stdc++.h>
using namespace std;
 
// Function to return the next
// fibonacci number
int nextFibonacci(int n)
{
    double a = n * (1 + sqrt(5)) / 2.0;
    return round(a);
}
 
// Driver code
int main()
{
    int n = 5;
    cout << nextFibonacci(n);
}
 
// This code is contributed by mohit kumar 29

Java




// Java implementation of the approach
class GFG
{
     
    // Function to return the next
    // fibonacci number
    static long nextFibonacci(int n)
    {
        double a = n * (1 + Math.sqrt(5)) / 2.0;
        return Math.round(a);
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int n = 5;
        System.out.println(nextFibonacci(n));
    }
}
 
// This code is contributed by AnkitRai01

Python3




# Python3 implementation of the approach
from math import *
 
# Function to return the next
# fibonacci number
def nextFibonacci(n):
    a = n*(1 + sqrt(5))/2.0
    return round(a)
 
# Driver code
n = 5
print(nextFibonacci(n))

C#




// C# implementation of the approach
using System;
 
class GFG
{
     
    // Function to return the next
    // fibonacci number
    static long nextFibonacci(int n)
    {
        double a = n * (1 + Math.Sqrt(5)) / 2.0;
        return (long)Math.Round(a);
    }
     
    // Driver code
    public static void Main(String[] args)
    {
        int n = 5;
        Console.WriteLine(nextFibonacci(n));
    }
}
 
// This code is contributed by 29AjayKumar

Javascript




<script>
 
// Javascript implementation of the approach
 
// Function to return the next
// fibonacci number
function nextFibonacci(n)
{
    let a = n * (1 + Math.sqrt(5)) / 2.0;
    return Math.round(a);
}
 
// Driver code
  
    let n = 5;
    document.write(nextFibonacci(n));
 
// This code is contributed by Mayank Tyagi
 
</script>
Output: 
8

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!