# Find nearest Fibonacci number from given number

Here given code implementation process.

``````// C Program for
// Find nearest Fibonacci number from given number
#include <stdio.h>

int minValue(int a, int b)
{
if (a < b)
{
return a;
}
return b;
}
// nearest fibonacci number to a given number
void nearestFibonacciNo(int n)
{
int a = 0;
int b = 1;
int c = a + b;
while (c <= n)
{
a = b;
b = c;
c = a + c;
}
// Get min difference
int min = minValue(n - a, n - b);
if (min == n - a)
{
// min difference of a
min = a;
}
else
{
// min difference of b
min = b;
}
printf("\n Given n : %d", n);
printf("\n Result  : %d", min);
}
int main(int argc, char
const *argv[])
{
// Test A
// num = 22
// output = 21
nearestFibonacciNo(22);
// Test B
// num = 10
// output = 8
nearestFibonacciNo(10);
return 0;
}``````

#### Output

`````` Given n : 22
Result  : 21
Given n : 10
Result  : 8``````
``````// Java program for
// Find nearest Fibonacci number from given number
public class FibonacciNo
{
public int minValue(int a, int b)
{
if (a < b)
{
return a;
}
return b;
}
public void nearestFibonacciNo(int n)
{
int a = 0;
int b = 1;
int c = a + b;
while (c <= n)
{
a = b;
b = c;
c = a + c;
}
// Get min difference
int min = minValue(n - a, n - b);
if (min == n - a)
{
// min difference of a
min = a;
}
else
{
// min difference of b
min = b;
}
System.out.print("\n Given n : " + n);
System.out.print("\n Result : " + min);
}
public static void main(String[] args)
{
FibonacciNo task = new FibonacciNo();
// Test A
// num = 22
// output = 21
// Test B
// num = 10
// output = 8
}
}``````

#### Output

`````` Given n : 22
Result : 21
Given n : 10
Result : 8``````
``````package main
import "fmt"
// Go program for
// Find nearest Fibonacci number from given number
type FibonacciNo struct {}
func getFibonacciNo() * FibonacciNo {
var me *FibonacciNo = &FibonacciNo {}
return me
}
func(this FibonacciNo) minValue(a, b int) int {
if a < b {
return a
}
return b
}
func(this FibonacciNo) nearestFibonacciNo(n int) {
var a int = 0
var b int = 1
var c int = a + b
for (c <= n) {
a = b
b = c
c = a + c
}
// Get min difference
var min int = this.minValue(n - a, n - b)
if min == n - a {
// min difference of a
min = a
} else {
// min difference of b
min = b
}
fmt.Print("\n Given n : ", n)
fmt.Print("\n Result : ", min)
}
func main() {
var task * FibonacciNo = getFibonacciNo()
// Test A
// num = 22
// output = 21
// Test B
// num = 10
// output = 8
}``````

#### Output

`````` Given n : 22
Result : 21
Given n : 10
Result : 8``````
``````// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Find nearest Fibonacci number from given number
class FibonacciNo
{
public: int minValue(int a, int b)
{
if (a < b)
{
return a;
}
return b;
}
void nearestFibonacciNo(int n)
{
int a = 0;
int b = 1;
int c = a + b;
while (c <= n)
{
a = b;
b = c;
c = a + c;
}
// Get min difference
int min = this->minValue(n - a, n - b);
if (min == n - a)
{
// min difference of a
min = a;
}
else
{
// min difference of b
min = b;
}
cout << "\n Given n : " << n;
cout << "\n Result : " << min;
}
};
int main()
{
FibonacciNo *task = new FibonacciNo();
// Test A
// num = 22
// output = 21
// Test B
// num = 10
// output = 8
return 0;
}``````

#### Output

`````` Given n : 22
Result : 21
Given n : 10
Result : 8``````
``````// Include namespace system
using System;
// Csharp program for
// Find nearest Fibonacci number from given number
public class FibonacciNo
{
public int minValue(int a, int b)
{
if (a < b)
{
return a;
}
return b;
}
public void nearestFibonacciNo(int n)
{
int a = 0;
int b = 1;
int c = a + b;
while (c <= n)
{
a = b;
b = c;
c = a + c;
}
// Get min difference
int min = this.minValue(n - a, n - b);
if (min == n - a)
{
// min difference of a
min = a;
}
else
{
// min difference of b
min = b;
}
Console.Write("\n Given n : " + n);
Console.Write("\n Result : " + min);
}
public static void Main(String[] args)
{
FibonacciNo task = new FibonacciNo();
// Test A
// num = 22
// output = 21
// Test B
// num = 10
// output = 8
}
}``````

#### Output

`````` Given n : 22
Result : 21
Given n : 10
Result : 8``````
``````<?php
// Php program for
// Find nearest Fibonacci number from given number
class FibonacciNo
{
public	function minValue(\$a, \$b)
{
if (\$a < \$b)
{
return \$a;
}
return \$b;
}
public	function nearestFibonacciNo(\$n)
{
\$a = 0;
\$b = 1;
\$c = \$a + \$b;
while (\$c <= \$n)
{
\$a = \$b;
\$b = \$c;
\$c = \$a + \$c;
}
// Get min difference
\$min = \$this->minValue(\$n - \$a, \$n - \$b);
if (\$min == \$n - \$a)
{
// min difference of a
\$min = \$a;
}
else
{
// min difference of b
\$min = \$b;
}
echo("\n Given n : ".\$n);
echo("\n Result : ".\$min);
}
}

function main()
{
\$task = new FibonacciNo();
// Test A
// num = 22
// output = 21
// Test B
// num = 10
// output = 8
}
main();``````

#### Output

`````` Given n : 22
Result : 21
Given n : 10
Result : 8``````
``````// Node JS program for
// Find nearest Fibonacci number from given number
class FibonacciNo
{
minValue(a, b)
{
if (a < b)
{
return a;
}
return b;
}
nearestFibonacciNo(n)
{
var a = 0;
var b = 1;
var c = a + b;
while (c <= n)
{
a = b;
b = c;
c = a + c;
}
// Get min difference
var min = this.minValue(n - a, n - b);
if (min == n - a)
{
// min difference of a
min = a;
}
else
{
// min difference of b
min = b;
}
process.stdout.write("\n Given n : " + n);
process.stdout.write("\n Result : " + min);
}
}

function main()
{
var task = new FibonacciNo();
// Test A
// num = 22
// output = 21
// Test B
// num = 10
// output = 8
}
main();``````

#### Output

`````` Given n : 22
Result : 21
Given n : 10
Result : 8``````
``````#  Python 3 program for
#  Find nearest Fibonacci number from given number
class FibonacciNo :
def minValue(self, a, b) :
if (a < b) :
return a

return b

def nearestFibonacciNo(self, n) :
a = 0
b = 1
c = a + b
while (c <= n) :
a = b
b = c
c = a + c

#  Get min difference
min = self.minValue(n - a, n - b)
if (min == n - a) :
#  min difference of a
min = a
else :
#  min difference of b
min = b

print("\n Given n : ", n, end = "")
print("\n Result : ", min, end = "")

def main() :
#  Test A
#  num = 22
#  output = 21
#  Test B
#  num = 10
#  output = 8

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

#### Output

`````` Given n :  22
Result :  21
Given n :  10
Result :  8``````
``````#  Ruby program for
#  Find nearest Fibonacci number from given number
class FibonacciNo
def minValue(a, b)
if (a < b)
return a
end

return b
end

def nearestFibonacciNo(n)
a = 0
b = 1
c = a + b
while (c <= n)
a = b
b = c
c = a + c
end

#  Get min difference
min = self.minValue(n - a, n - b)
if (min == n - a)
#  min difference of a
min = a
else

#  min difference of b
min = b
end

print("\n Given n : ", n)
print("\n Result : ", min)
end

end

def main()
#  Test A
#  num = 22
#  output = 21
#  Test B
#  num = 10
#  output = 8
end

main()``````

#### Output

`````` Given n : 22
Result : 21
Given n : 10
Result : 8``````
``````// Scala program for
// Find nearest Fibonacci number from given number
class FibonacciNo()
{
def minValue(a: Int, b: Int): Int = {
if (a < b)
{
return a;
}
return b;
}
def nearestFibonacciNo(n: Int): Unit = {
var a: Int = 0;
var b: Int = 1;
var c: Int = a + b;
while (c <= n)
{
a = b;
b = c;
c = a + c;
}
// Get min difference
var min: Int = minValue(n - a, n - b);
if (min == n - a)
{
// min difference of a
min = a;
}
else
{
// min difference of b
min = b;
}
print("\n Given n : " + n);
print("\n Result : " + min);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: FibonacciNo = new FibonacciNo();
// Test A
// num = 22
// output = 21
// Test B
// num = 10
// output = 8
}
}``````

#### Output

`````` Given n : 22
Result : 21
Given n : 10
Result : 8``````
``````// Swift 4 program for
// Find nearest Fibonacci number from given number
class FibonacciNo
{
func minValue(_ a: Int, _ b: Int) -> Int
{
if (a < b)
{
return a;
}
return b;
}
func nearestFibonacciNo(_ n: Int)
{
var a: Int = 0;
var b: Int = 1;
var c: Int = a + b;
while (c <= n)
{
a = b;
b = c;
c = a + c;
}
// Get min difference
var min: Int = self.minValue(n - a, n - b);
if (min == n - a)
{
// min difference of a
min = a;
}
else
{
// min difference of b
min = b;
}
print("\n Given n : ", n, terminator: "");
print("\n Result : ", min, terminator: "");
}
}
func main()
{
let task: FibonacciNo = FibonacciNo();
// Test A
// num = 22
// output = 21
// Test B
// num = 10
// output = 8
}
main();``````

#### Output

`````` Given n :  22
Result :  21
Given n :  10
Result :  8``````
``````// Kotlin program for
// Find nearest Fibonacci number from given number
class FibonacciNo
{
fun minValue(a: Int, b: Int): Int
{
if (a < b)
{
return a;
}
return b;
}
fun nearestFibonacciNo(n: Int): Unit
{
var a: Int = 0;
var b: Int = 1;
var c: Int = a + b;
while (c <= n)
{
a = b;
b = c;
c = a + c;
}
// Get min difference
var min: Int = this.minValue(n - a, n - b);
if (min == n - a)
{
// min difference of a
min = a;
}
else
{
// min difference of b
min = b;
}
print("\n Given n : " + n);
print("\n Result : " + min);
}
}
fun main(args: Array < String > ): Unit
{
val task: FibonacciNo = FibonacciNo();
// Test A
// num = 22
// output = 21
// Test B
// num = 10
// output = 8
}``````

#### Output

`````` Given n : 22
Result : 21
Given n : 10
Result : 8``````

## 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.