Posted on by Kalkicode
Code Mathematics

# Determine that if given number is Fibonacci Number

A Fibonacci number is a number in the sequence of Fibonacci numbers, which is defined as follows:

• The first two Fibonacci numbers are 0 and 1.
• Each subsequent Fibonacci number is the sum of the two preceding Fibonacci numbers.

So, for example, the first few Fibonacci numbers are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, and so on.

To determine if a given number is a Fibonacci number, you can check if it can be expressed as the sum of two preceding Fibonacci numbers. One way to do this is to use the following formula:

• A number N is a Fibonacci number if and only if 5N^2 + 4 or 5N^2 - 4 is a perfect square.

If either of these expressions is a perfect square, then N is a Fibonacci number. Otherwise, it is not.

For example, let's say we want to determine if the number 8 is a Fibonacci number. Using the formula above, we get:

• 5(8)^2 + 4 = 324, which is a perfect square (18^2)
• 5(8)^2 - 4 = 316, which is not a perfect square

Therefore, 8 is a Fibonacci number.

## Program Solution

``````//  C program for
//  Determine that if given number is Fibonacci Number
#include <stdio.h>

#include <math.h>

void isFibonacciNo(int n)
{
//  5n²+4
int a = 5 *n *n + 4;
//  5n²-4
int b = 5 *n *n - 4;
int aSqrt = sqrt(a);
int bSqrt = sqrt(b);
if ((aSqrt *aSqrt) == a || (bSqrt *bSqrt) == b)
{
// When aSqrt or bSqrt is perfect square
printf("\n Given %d is fibonacci number ", n);
}
else
{
printf("\n Given %d is not fibonacci number ", n);
}
}
int main(int argc, char
const *argv[])
{
isFibonacciNo(51);
isFibonacciNo(144);
isFibonacciNo(987);
isFibonacciNo(14);
return 0;
}``````

#### Output

`````` Given 51 is not fibonacci number
Given 144 is fibonacci number
Given 987 is fibonacci number
Given 14 is not fibonacci number``````
``````// Java Program
// Determine that if given number is Fibonacci Number
public class FibonacciNumber
{
public void isFibonacciNo(int n)
{
//  5n²+4
int a = 5 * n * n + 4;
//  5n²-4
int b = 5 * n * n - 4;
int aSqrt = (int) Math.sqrt(a);
int bSqrt = (int) Math.sqrt(b);
if ((aSqrt * aSqrt) == a || (bSqrt * bSqrt) == b)
{
// When aSqrt or bSqrt is perfect square
System.out.print("\n Given " + n + " is fibonacci number ");
}
else
{
System.out.print("\n Given " + n + " is not fibonacci number ");
}
}
public static void main(String[] args)
{
// Test
}
}``````

#### Output

`````` Given 51 is not fibonacci number
Given 144 is fibonacci number
Given 987 is fibonacci number
Given 14 is not fibonacci number``````
``````// Include header file
#include <iostream>
#include <math.h>
using namespace std;

// C++ Program
// Determine that if given number is Fibonacci Number
class FibonacciNumber
{
public: void isFibonacciNo(int n)
{
//  5n²+4
int a = 5 *n *n + 4;
//  5n²-4
int b = 5 *n *n - 4;
int aSqrt = (int) sqrt(a);
int bSqrt = (int) sqrt(b);
if ((aSqrt *aSqrt) == a || (bSqrt *bSqrt) == b)
{
// When aSqrt or bSqrt is perfect square
cout << "\n Given " << n << " is fibonacci number ";
}
else
{
cout << "\n Given " << n << " is not fibonacci number ";
}
}
};
int main()
{
// Test
return 0;
}``````

#### Output

`````` Given 51 is not fibonacci number
Given 144 is fibonacci number
Given 987 is fibonacci number
Given 14 is not fibonacci number``````
``````// Include namespace system
using System;
// Csharp Program
// Determine that if given number is Fibonacci Number
public class FibonacciNumber
{
public void isFibonacciNo(int n)
{
//  5n²+4
int a = 5 * n * n + 4;
//  5n²-4
int b = 5 * n * n - 4;
int aSqrt = (int) Math.Sqrt(a);
int bSqrt = (int) Math.Sqrt(b);
if ((aSqrt * aSqrt) == a || (bSqrt * bSqrt) == b)
{
// When aSqrt or bSqrt is perfect square
Console.Write("\n Given " + n + " is fibonacci number ");
}
else
{
Console.Write("\n Given " + n + " is not fibonacci number ");
}
}
public static void Main(String[] args)
{
// Test
}
}``````

#### Output

`````` Given 51 is not fibonacci number
Given 144 is fibonacci number
Given 987 is fibonacci number
Given 14 is not fibonacci number``````
``````package main
import "math"
import "fmt"
// Go Program
// Determine that if given number is Fibonacci Number

func isFibonacciNo(n int) {
//  5n²+4
var a int = 5 * n * n + 4
//  5n²-4
var b int = 5 * n * n - 4
var aSqrt int = int(math.Sqrt(float64(a)))
var bSqrt int = int(math.Sqrt(float64(b)))
if (aSqrt * aSqrt) == a || (bSqrt * bSqrt) == b {
// When aSqrt or bSqrt is perfect square
fmt.Print("\n Given ", n, " is fibonacci number ")
} else {
fmt.Print("\n Given ", n, " is not fibonacci number ")
}
}
func main() {

// Test
isFibonacciNo(51)
isFibonacciNo(144)
isFibonacciNo(987)
isFibonacciNo(14)
}``````

#### Output

`````` Given 51 is not fibonacci number
Given 144 is fibonacci number
Given 987 is fibonacci number
Given 14 is not fibonacci number``````
``````<?php
// Php Program
// Determine that if given number is Fibonacci Number
class FibonacciNumber
{
public	function isFibonacciNo(\$n)
{
//  5n²+4
\$a = 5 * \$n * \$n + 4;
//  5n²-4
\$b = 5 * \$n * \$n - 4;
\$aSqrt = (int) sqrt(\$a);
\$bSqrt = (int) sqrt(\$b);
if ((\$aSqrt * \$aSqrt) == \$a || (\$bSqrt * \$bSqrt) == \$b)
{
// When aSqrt or bSqrt is perfect square
echo("\n Given ".\$n.
" is fibonacci number ");
}
else
{
echo("\n Given ".\$n.
" is not fibonacci number ");
}
}
}

function main()
{
// Test
}
main();``````

#### Output

`````` Given 51 is not fibonacci number
Given 144 is fibonacci number
Given 987 is fibonacci number
Given 14 is not fibonacci number``````
``````// Node JS Program
// Determine that if given number is Fibonacci Number
class FibonacciNumber
{
isFibonacciNo(n)
{
//  5n²+4
var a = 5 * n * n + 4;
//  5n²-4
var b = 5 * n * n - 4;
var aSqrt = parseInt(Math.sqrt(a));
var bSqrt = parseInt(Math.sqrt(b));
if ((aSqrt * aSqrt) == a || (bSqrt * bSqrt) == b)
{
// When aSqrt or bSqrt is perfect square
process.stdout.write("\n Given " + n + " is fibonacci number ");
}
else
{
process.stdout.write("\n Given " + n + " is not fibonacci number ");
}
}
}

function main()
{
// Test
}
main();``````

#### Output

`````` Given 51 is not fibonacci number
Given 144 is fibonacci number
Given 987 is fibonacci number
Given 14 is not fibonacci number``````
``````import math
#  Python 3 Program
#  Determine that if given number is Fibonacci Number
class FibonacciNumber :
def isFibonacciNo(self, n) :
#   5n²+4
a = 5 * n * n + 4
#   5n²-4
b = 5 * n * n - 4
aSqrt = int(math.sqrt(a))
bSqrt = int(math.sqrt(b))
if ((aSqrt * aSqrt) == a or(bSqrt * bSqrt) == b) :
#  When aSqrt or bSqrt is perfect square
print("\n Given ", n ," is fibonacci number ", end = "")
else :
print("\n Given ", n ," is not fibonacci number ", end = "")

def main() :
#  Test

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

#### Output

`````` Given  51  is not fibonacci number
Given  144  is fibonacci number
Given  987  is fibonacci number
Given  14  is not fibonacci number``````
``````#  Ruby Program
#  Determine that if given number is Fibonacci Number
class FibonacciNumber
def isFibonacciNo(n)
#   5n²+4
a = 5 * n * n + 4
#   5n²-4
b = 5 * n * n - 4
aSqrt = Math.sqrt(a).to_i
bSqrt = Math.sqrt(b).to_i
if ((aSqrt * aSqrt) == a || (bSqrt * bSqrt) == b)
#  When aSqrt or bSqrt is perfect square
print("\n Given ", n ," is fibonacci number ")
else

print("\n Given ", n ," is not fibonacci number ")
end

end

end

def main()
#  Test
end

main()``````

#### Output

`````` Given 51 is not fibonacci number
Given 144 is fibonacci number
Given 987 is fibonacci number
Given 14 is not fibonacci number ``````
``````// Scala Program
// Determine that if given number is Fibonacci Number
class FibonacciNumber()
{
def isFibonacciNo(n: Int): Unit = {
//  5n²+4
var a: Int = 5 * n * n + 4;
//  5n²-4
var b: Int = 5 * n * n - 4;
var aSqrt: Int = scala.math.sqrt(a).toInt;
var bSqrt: Int = scala.math.sqrt(b).toInt;
if ((aSqrt * aSqrt) == a || (bSqrt * bSqrt) == b)
{
// When aSqrt or bSqrt is perfect square
print("\n Given " + n + " is fibonacci number ");
}
else
{
print("\n Given " + n + " is not fibonacci number ");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: FibonacciNumber = new FibonacciNumber();
// Test
}
}``````

#### Output

`````` Given 51 is not fibonacci number
Given 144 is fibonacci number
Given 987 is fibonacci number
Given 14 is not fibonacci number``````
``````import Foundation;
// Swift 4 Program
// Determine that if given number is Fibonacci Number
class FibonacciNumber
{
func isFibonacciNo(_ n: Int)
{
//  5n²+4
let a: Int = 5 * n * n + 4;
//  5n²-4
let b: Int = 5 * n * n - 4;
let aSqrt: Int = Int((Double(a)).squareRoot());
let bSqrt: Int = Int((Double(b)).squareRoot());
if ((aSqrt * aSqrt) == a || (bSqrt * bSqrt) == b)
{
// When aSqrt or bSqrt is perfect square
print("\n Given ", n ," is fibonacci number ", terminator: "");
}
else
{
print("\n Given ", n ," is not fibonacci number ", terminator: "");
}
}
}
func main()
{
// Test
}
main();``````

#### Output

`````` Given  51  is not fibonacci number
Given  144  is fibonacci number
Given  987  is fibonacci number
Given  14  is not fibonacci number``````
``````// Kotlin Program
// Determine that if given number is Fibonacci Number
class FibonacciNumber
{
fun isFibonacciNo(n: Int): Unit
{
//  5n²+4
val a: Int = 5 * n * n + 4;
//  5n²-4
val b: Int = 5 * n * n - 4;
val aSqrt: Int = Math.sqrt(a.toDouble()).toInt();
val bSqrt: Int = Math.sqrt(b.toDouble()).toInt();
if ((aSqrt * aSqrt) == a || (bSqrt * bSqrt) == b)
{
// When aSqrt or bSqrt is perfect square
print("\n Given " + n + " is fibonacci number ");
}
else
{
print("\n Given " + n + " is not fibonacci number ");
}
}
}
fun main(args: Array < String > ): Unit
{
// Test
}``````

#### Output

`````` Given 51 is not fibonacci number
Given 144 is fibonacci number
Given 987 is fibonacci number
Given 14 is not fibonacci number``````

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