Find the next fibonacci number

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

`````` [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``````
``````// 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

`````` [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``````
``````//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

`````` [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``````
``````//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

`````` [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``````
``````<?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

`````` [0] is next fibonacci are : 1
[13] is next fibonacci are : 13
[55] is next fibonacci are : 55
[3] is next fibonacci are : 3
[1] is next fibonacci are : 1
[233] 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

`````` [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``````
``````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

`````` [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``````
``````// 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

`````` [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``````

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.