Posted on by Kalkicode
Code Number

# Find the next fibonacci number

The Fibonacci sequence is a series of numbers in which each number is the sum of the two preceding ones, starting from 0 and 1. In this article, we will explore a simple C program that finds the next Fibonacci number given a valid Fibonacci number as input.

## Problem Statement

Given a valid Fibonacci number as input, the task is to find the next Fibonacci number in the sequence.

Explanation with Suitable Example: Let's take an example to understand the problem. Suppose we have the Fibonacci number 13. The next Fibonacci number after 13 in the sequence is 21. Similarly, if we have the Fibonacci number 55, the next Fibonacci number will be 89.

## Pseudocode

1. Start
2. Define a function named next_fibonacci that takes an integer 'number' as input.
3. If 'number' is less than 0, return from the function as negative Fibonacci numbers do not exist.
4. Initialize a variable 'result' to store the next Fibonacci number.
5. If 'number' is 0, set 'result' to 1.
6. Otherwise, calculate the next Fibonacci number using the formula: round(number * ((1 + sqrt(5)) / 2)).
7. Display the calculated result.
8. End

## Algorithm Explanation

1. Start the program.
2. Define the function next_fibonacci.
3. Check if the input number is negative, return from the function.
4. If the input number is 0, set 'result' to 1, as the next Fibonacci number after 0 is 1.
5. Otherwise, calculate the next Fibonacci number using the formula: round(number * ((1 + sqrt(5)) / 2)).
• The formula uses the golden ratio to approximate the next Fibonacci number. Note that this formula assumes the given number is a valid Fibonacci number.
6. Display the calculated result.
7. Repeat steps 3-6 for each test case in the main function.
8. End the program.

## Code Solution

Here given code implementation process.

``````// C program
// Find the next fibonacci number
#include <stdio.h>
#include <math.h>

//Find the next fibonacci of given number
void next_fibonacci(int number)
{
if (number < 0)
{
return;
}
int result = 0;
if (number == 0)
{
result = 1;
}
else
{
// Formula
// (number * (1 + sqrt(5)) / 2)
// Assuming that, given number is valid fibonacci number
result = round(number * ((1 + sqrt(5)) / 2));
}
// Display the calculated result
printf("\n [%d] is next fibonacci are : %d ", number, result);
}
int main()
{
//Test case
next_fibonacci(0);
next_fibonacci(13);
next_fibonacci(55);
next_fibonacci(3);
next_fibonacci(1);
next_fibonacci(233);
return 0;
}``````

#### Output

``````  is next fibonacci are : 1
 is next fibonacci are : 21
 is next fibonacci are : 89
 is next fibonacci are : 5
 is next fibonacci are : 2
 is next fibonacci are : 377``````
``````// Java program
// Find the next fibonacci number
class FibonacciNo
{
//Find the next fibonacci of given number
public void next_fibonacci(int number)
{
if (number < 0)
{
return;
}
long result = 0;
if (number == 0)
{
result = 1;
}
else
{
// Formula
// (number *(1 + sqrt(5)) / 2)
// Assuming that, given number is valid fibonacci number
result = Math.round(number * ((1 + Math.sqrt(5)) / 2));
}
// Display the calculated result
System.out.print("\n [" + number + "] is next fibonacci are : " + result );
}
public static void main(String[] args)
{
FibonacciNo obj = new FibonacciNo();
//Test case
obj.next_fibonacci(0);
obj.next_fibonacci(13);
obj.next_fibonacci(55);
obj.next_fibonacci(3);
obj.next_fibonacci(1);
obj.next_fibonacci(233);
}
}``````

#### Output

``````  is next fibonacci are : 1
 is next fibonacci are : 21
 is next fibonacci are : 89
 is next fibonacci are : 5
 is next fibonacci are : 2
 is next fibonacci are : 377``````
``````//Include header file
#include <iostream>
#include<math.h>
using namespace std;

// C++ program
// Find the next fibonacci number

class FibonacciNo
{
public:
//Find the next fibonacci of given number
void next_fibonacci(int number)
{
if (number < 0)
{
return;
}
long result = 0;
if (number == 0)
{
result = 1;
}
else
{
// Formula
// (number *(1 + sqrt(5)) / 2)
// Assuming that, given number is valid fibonacci number
result = round(number *((1 + sqrt(5)) / 2));
}
// Display the calculated result
cout << "\n [" << number << "] is next fibonacci are : " << result;
}
};
int main()
{
FibonacciNo obj = FibonacciNo();
//Test case
obj.next_fibonacci(0);
obj.next_fibonacci(13);
obj.next_fibonacci(55);
obj.next_fibonacci(3);
obj.next_fibonacci(1);
obj.next_fibonacci(233);
return 0;
}``````

#### Output

``````  is next fibonacci are : 1
 is next fibonacci are : 21
 is next fibonacci are : 89
 is next fibonacci are : 5
 is next fibonacci are : 2
 is next fibonacci are : 377``````
``````//Include namespace system
using System;
// C# program
// Find the next fibonacci number
class FibonacciNo
{
//Find the next fibonacci of given number
public void next_fibonacci(int number)
{
if (number < 0)
{
return;
}
long result = 0;
if (number == 0)
{
result = 1;
}
else
{
// Formula
// (number *(1 + sqrt(5)) / 2)
// Assuming that, given number is valid fibonacci number
result = (long)Math.Round(number * ((1 + Math.Sqrt(5)) / 2));
}
// Display the calculated result
Console.Write("\n [" + number + "] is next fibonacci are : " + result);
}
public static void Main(String[] args)
{
FibonacciNo obj = new FibonacciNo();
//Test case
obj.next_fibonacci(0);
obj.next_fibonacci(13);
obj.next_fibonacci(55);
obj.next_fibonacci(3);
obj.next_fibonacci(1);
obj.next_fibonacci(233);
}
}``````

#### Output

``````  is next fibonacci are : 1
 is next fibonacci are : 21
 is next fibonacci are : 89
 is next fibonacci are : 5
 is next fibonacci are : 2
 is next fibonacci are : 377``````
``````<?php
// Php program
// Find the next fibonacci number

class FibonacciNo
{
//Find the next fibonacci of given number
public	function next_fibonacci(\$number)
{
if (\$number < 0)
{
return;
}
\$result = 0;
if (\$number == 0)
{
\$result = 1;
}
else
{
// Formula
// (number *(1 + sqrt(5)) / 2)
// Assuming that, given number is valid fibonacci number
\$result = round(\$number * (intval((1 + sqrt(5)) / 2)));
}
// Display the calculated result
echo "\n [". \$number ."] is next fibonacci are : ". \$result;
}
}

function main()
{
\$obj = new FibonacciNo();
//Test case
\$obj->next_fibonacci(0);
\$obj->next_fibonacci(13);
\$obj->next_fibonacci(55);
\$obj->next_fibonacci(3);
\$obj->next_fibonacci(1);
\$obj->next_fibonacci(233);
}
main();``````

#### Output

``````  is next fibonacci are : 1
 is next fibonacci are : 13
 is next fibonacci are : 55
 is next fibonacci are : 3
 is next fibonacci are : 1
 is next fibonacci are : 233``````
``````// Node Js program
// Find the next fibonacci number
class FibonacciNo
{
//Find the next fibonacci of given number
next_fibonacci(number)
{
if (number < 0)
{
return;
}
var result = 0;
if (number == 0)
{
result = 1;
}
else
{
// Formula
// (number *(1 + sqrt(5)) / 2)
// Assuming that, given number is valid fibonacci number
result = Math.round(number * ((1 + Math.sqrt(5)) / 2));
}
// Display the calculated result
process.stdout.write("\n [" + number + "] is next fibonacci are : " + result);
}
}

function main()
{
var obj = new FibonacciNo();
//Test case
obj.next_fibonacci(0);
obj.next_fibonacci(13);
obj.next_fibonacci(55);
obj.next_fibonacci(3);
obj.next_fibonacci(1);
obj.next_fibonacci(233);
}
main();``````

#### Output

``````  is next fibonacci are : 1
 is next fibonacci are : 21
 is next fibonacci are : 89
 is next fibonacci are : 5
 is next fibonacci are : 2
 is next fibonacci are : 377``````
``````import math

#  Python 3 program
#  Find the next fibonacci number

class FibonacciNo :
# Find the next fibonacci of given number
def next_fibonacci(self, number) :
if (number < 0) :
return

result = 0
if (number == 0) :
result = 1
else :
#  Formula
#  (number *(1 + sqrt(5)) / 2)
#  Assuming that, given number is valid fibonacci number
result = round(number * (((1 + math.sqrt(5)) / 2)))

#  Display the calculated result
print("\n [", number ,"] is next fibonacci are : ", result, end = "")

def main() :
obj = FibonacciNo()
# Test case
obj.next_fibonacci(0)
obj.next_fibonacci(13)
obj.next_fibonacci(55)
obj.next_fibonacci(3)
obj.next_fibonacci(1)
obj.next_fibonacci(233)

if __name__ == "__main__": main()``````

#### Output

`````` [ 0 ] is next fibonacci are :  1
[ 13 ] is next fibonacci are :  21
[ 55 ] is next fibonacci are :  89
[ 3 ] is next fibonacci are :  5
[ 1 ] is next fibonacci are :  2
[ 233 ] is next fibonacci are :  377``````
``````#  Ruby program
#  Find the next fibonacci number
class FibonacciNo
# Find the next fibonacci of given number
def next_fibonacci(number)
if (number < 0)
return
end

result = 0
if (number == 0)
result = 1
else
#  Formula
#  (number *(1 + sqrt(5)) / 2)
#  Assuming that, given number is valid fibonacci number
result =(number * ((1 + Math.sqrt(5)) / 2)).round()
end

#  Display the calculated result
print("\n [", number ,"] is next fibonacci are : ", result)
end

end

def main()
obj = FibonacciNo.new()
# Test case
obj.next_fibonacci(0)
obj.next_fibonacci(13)
obj.next_fibonacci(55)
obj.next_fibonacci(3)
obj.next_fibonacci(1)
obj.next_fibonacci(233)
end

main()``````

#### Output

``````  is next fibonacci are : 1
 is next fibonacci are : 21
 is next fibonacci are : 89
 is next fibonacci are : 5
 is next fibonacci are : 2
 is next fibonacci are : 377``````
``````// Scala program
// Find the next fibonacci number
class FibonacciNo
{
//Find the next fibonacci of given number
def next_fibonacci(number: Int): Unit = {
if (number < 0)
{
return;
}
var result: Long = 0;
if (number == 0)
{
result = 1;
}
else
{
// Formula
// (number *(1 + sqrt(5)) / 2)
// Assuming that, given number is valid fibonacci number
result = Math.round(number * ((1 + Math.sqrt(5)) / 2));
}
// Display the calculated result
print("\n [" + number + "] is next fibonacci are : " + result);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: FibonacciNo = new FibonacciNo();
//Test case
obj.next_fibonacci(0);
obj.next_fibonacci(13);
obj.next_fibonacci(55);
obj.next_fibonacci(3);
obj.next_fibonacci(1);
obj.next_fibonacci(233);
}
}``````

#### Output

``````  is next fibonacci are : 1
 is next fibonacci are : 21
 is next fibonacci are : 89
 is next fibonacci are : 5
 is next fibonacci are : 2
 is next fibonacci are : 377``````

## Resultant Output Explanation

For each test case in the main function, the program will call the next_fibonacci function with the given Fibonacci number as input and calculate the next Fibonacci number using the formula. The calculated result will be printed on the screen.

### Output Explanation for Test Cases

1. For the input Fibonacci number 0, the next Fibonacci number is 1.
2. For the input Fibonacci number 13, the next Fibonacci number is 21.
3. For the input Fibonacci number 55, the next Fibonacci number is 89.
4. For the input Fibonacci number 3, the next Fibonacci number is 5.
5. For the input Fibonacci number 1, the next Fibonacci number is 2.
6. For the input Fibonacci number 233, the next Fibonacci number is 377.

## Time Complexity of the Code

The time complexity of the code is O(1) because the next Fibonacci number is calculated directly using a simple formula without any loops or recursion. The formula involves basic arithmetic operations, which are constant time operations, making the overall time complexity constant.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post