Check if a number is fibonacci number or not

Here given code implementation process.

//C Program
//Check if a number is fibonacci number or not
#include <stdio.h>

//check whether number is a Fibonacci number
int fibonacci(int n)
{
  //Set the initial value of variable
  //This is two initial value
  int first = 0;
  int second = 1;
  int status = 0;

  while(first<=n)
  {
    
    if(first==n)
    {
      status=1;
      break;
    } 
    //This is next result
    second += first;

    first   = second - first;

  }

  return status;
  
}
//Controlling the request of fibonacci number
void is_fibonacci(int number)
{
  if(fibonacci(number)==0)
  {
    printf("%d Is not a fibonacci number\n",number);
  }
  else
  {
    printf("%d Is a fibonacci number\n",number);
  }

}
int main()
{

  is_fibonacci(51);
  is_fibonacci(144);
  is_fibonacci(987);
  is_fibonacci(14);
  return 0;
}

Output

51 Is not a fibonacci number
144 Is a fibonacci number
987 Is a fibonacci number
14 Is not a fibonacci number
/*
 C++ Program
 Check if a number is fibonacci number or not
*/
#include<iostream>

using namespace std;
class MyNumber {
	public:

		//check whether number is a Fibonacci number
		bool fibonacci(int n) {
			//Set the initial value of variable
			//This is two initial value
			int first = 0;
			int second = 1;
			bool status = false;
			while (first <= n) {
				if (first == n) {
					status = true;
					break;
				}
				//This is next result
				second += first;
				first = second - first;
			}
			return status;
		}
	//Controlling the request of fibonacci number
	void is_fibonacci(int number) {
		if (this->fibonacci(number) == false) {
			cout << number << " Is not a fibonacci number\n";
		} else {
			cout << number << " Is a fibonacci number\n";
		}
	}
};
int main() {
	MyNumber obj ;
	//Test case
	obj.is_fibonacci(51);
	obj.is_fibonacci(144);
	obj.is_fibonacci(987);
	obj.is_fibonacci(14);
	return 0;
}

Output

51 Is not a fibonacci number
144 Is a fibonacci number
987 Is a fibonacci number
14 Is not a fibonacci number
/*
  Java Program
  Check if a number is fibonacci number or not
*/

public class MyNumber {


	//check whether number is a Fibonacci number
	public boolean fibonacci(int n) {
		//Set the initial value of variable
		//This is two initial value
		int first = 0;
		int second = 1;
		boolean status = false;

		while (first <= n) {

			if (first == n) {
				status = true;
				break;
			}
			//This is next result
			second += first;

			first = second - first;

		}

		return status;

	}
	//Controlling the request of fibonacci number
	public void is_fibonacci(int number) {
		if (fibonacci(number) == false) {
			System.out.print(number + " Is not a fibonacci number\n");
		} else {
			System.out.print(number + " Is a fibonacci number\n");
		}

	}

	public static void main(String[] args) {

		MyNumber obj = new MyNumber();
		//Test case
		obj.is_fibonacci(51);
		obj.is_fibonacci(144);
		obj.is_fibonacci(987);
		obj.is_fibonacci(14);
	}
}

Output

51 Is not a fibonacci number
144 Is a fibonacci number
987 Is a fibonacci number
14 Is not a fibonacci number
/*
  C# Program
  Check if a number is fibonacci number or not
*/
using System;
public class MyNumber {


	//check whether number is a Fibonacci number
	public Boolean fibonacci(int n) {
		//Set the initial value of variable
		//This is two initial value
		int first = 0;
		int second = 1;
		Boolean status = false;

		while (first <= n) {

			if (first == n) {
				status = true;
				break;
			}
			//This is next result
			second += first;

			first = second - first;

		}

		return status;

	}
	//Controlling the request of fibonacci number
	public void is_fibonacci(int number) {
		if (fibonacci(number) == false) {
			Console.Write(number + " Is not a fibonacci number\n");
		} else {
			Console.Write(number + " Is a fibonacci number\n");
		}

	}

	public static void Main(String[] args) {

		MyNumber obj = new MyNumber();
		//Test case
		obj.is_fibonacci(51);
		obj.is_fibonacci(144);
		obj.is_fibonacci(987);
		obj.is_fibonacci(14);
	}
}

Output

51 Is not a fibonacci number
144 Is a fibonacci number
987 Is a fibonacci number
14 Is not a fibonacci number
# Python 3 Program
# Check if a number is fibonacci number or not
class MyNumber :
	#check whether number is a Fibonacci number
	def fibonacci(self, n) :
		#This is two initial value
		#Set the initial value of variable
		first = 0
		second = 1
		status = False
		while (first <= n) :
			if (first == n) :
				status = True
				break
			
			#This is next result
			second += first
			first = second - first
		
		return status
	
	#Controlling the request of fibonacci number
	def is_fibonacci(self, number) :
		if (self.fibonacci(number) == False) :
			print(number ," Is not a fibonacci number\n")
		else :
			print(number ," Is a fibonacci number\n")
		
	

def main() :
	obj = MyNumber()
	#Test case
	obj.is_fibonacci(51)
	obj.is_fibonacci(144)
	obj.is_fibonacci(987)
	obj.is_fibonacci(14)


if __name__ == "__main__":
	main()

Output

51 Is not a fibonacci number
144 Is a fibonacci number
987 Is a fibonacci number
14 Is not a fibonacci number
# Ruby Program 
# Check if a number is fibonacci number or not
class MyNumber 
	#check whether number is a Fibonacci number
	def fibonacci(n) 
		#This is two initial value

		#Set the initial value of variable
		first = 0
		second = 1
		status = false
		while (first <= n) 
			if (first == n) 
				status = true
				break
			end
			#This is next result
			second += first
			first = second - first
		end
		return status
	end
	#Controlling the request of fibonacci number
	def is_fibonacci(number) 
		if (self.fibonacci(number) == false) 
			print(number ," Is not a fibonacci number\n")
		else 
			print(number ," Is a fibonacci number\n")
		end
	end
end
def main() 
	obj = MyNumber.new()
	#Test case
	obj.is_fibonacci(51)
	obj.is_fibonacci(144)
	obj.is_fibonacci(987)
	obj.is_fibonacci(14)
end
main()

Output

51 Is not a fibonacci number
144 Is a fibonacci number
987 Is a fibonacci number
14 Is not a fibonacci number
/*
 Scala Program
 Check if a number is fibonacci number or not
*/
import scala.util.control.Breaks._
class MyNumber {
	//check whether number is a Fibonacci number
	def fibonacci(n: Int): Boolean = {
		//Set the initial value of variable
		//This is two initial value
		var first: Int = 0;
		var second: Int = 1;
		var status: Boolean = false;
		breakable {
			while (first <= n) {
				if (first == n) {
					status = true;
					break;
				}
				//This is next result
				second += first;
				first = second - first;
			}
		}
		return status;
	}
	//Controlling the request of fibonacci number
	def is_fibonacci(number: Int): Unit = {
		if (this.fibonacci(number) == false) {
			print(s"$number Is not a fibonacci number\n");
		} else {
			print(s"$number Is a fibonacci number\n");
		}
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		var obj: MyNumber = new MyNumber();
		//Test case
		obj.is_fibonacci(51);
		obj.is_fibonacci(144);
		obj.is_fibonacci(987);
		obj.is_fibonacci(14);
	}
}

Output

51 Is not a fibonacci number
144 Is a fibonacci number
987 Is a fibonacci number
14 Is not a fibonacci number
/*
  Swift 4 Program
  Check if a number is fibonacci number or not
*/
class MyNumber {
	//check whether number is a Fibonacci number
	func fibonacci(_ n: Int) -> Bool {
		//Set the initial value of variable
		//This is two initial value
		var first: Int = 0;
		var second: Int = 1;
		var status: Bool = false;
		while (first <= n) {
			if (first == n) {
				status = true;
				break;
			}
			//This is next result
			second += first;
			first = second - first;
		}
		return status;
	}
	//Controlling the request of fibonacci number
	func is_fibonacci(_ number: Int) {
		if (self.fibonacci(number) == false) {
			print(number ," Is not a fibonacci number");
		} else {
			print(number ," Is a fibonacci number");
		}
	}
}
func main() {
	let obj: MyNumber = MyNumber();
	//Test case
	obj.is_fibonacci(51);
	obj.is_fibonacci(144);
	obj.is_fibonacci(987);
	obj.is_fibonacci(14);
}
main();

Output

51  Is not a fibonacci number
144  Is a fibonacci number
987  Is a fibonacci number
14  Is not a fibonacci number
<?php
/*
  Php Program
  Check if a number is fibonacci number or not
*/
class MyNumber {
	//check whether number is a Fibonacci number

	public 	function fibonacci($n) {
		//Set the initial value of variable
		//This is two initial value
		$first = 0;
		$second = 1;
		$status = false;
		while ($first <= $n) {
			if ($first == $n) {
				$status = true;
				break;
			}
			//This is next result
			$second += $first;
			$first = $second - $first;
		}
		return $status;
	}
	//Controlling the request of fibonacci number

	public 	function is_fibonacci($number) {
		if ($this->fibonacci($number) == false) {
			echo($number ." Is not a fibonacci number\n");
		} else {
			echo($number ." Is a fibonacci number\n");
		}
	}
};
function main() {
	$obj = new MyNumber();
	//Test case

	$obj->is_fibonacci(51);
	$obj->is_fibonacci(144);
	$obj->is_fibonacci(987);
	$obj->is_fibonacci(14);
}
main();

Output

51 Is not a fibonacci number
144 Is a fibonacci number
987 Is a fibonacci number
14 Is not a fibonacci number
/*
 Node Js Program
 Check if a number is fibonacci number or not
*/
class MyNumber {
	//check whether number is a Fibonacci number
	fibonacci(n) {
		//Set the initial value of variable
		//This is two initial value
		var first = 0;
		var second = 1;
		var status = false;
		while (first <= n) {
			if (first == n) {
				status = true;
				break;
			}
			//This is next result
			second += first;
			first = second - first;
		}
		return status;
	}
	//Controlling the request of fibonacci number
	is_fibonacci(number) {
		if (this.fibonacci(number) == false) {
			process.stdout.write(number + " Is not a fibonacci number\n");
		} else {
			process.stdout.write(number + " Is a fibonacci number\n");
		}
	}
}

function main(args) {
	var obj = new MyNumber();
	//Test case
	obj.is_fibonacci(51);
	obj.is_fibonacci(144);
	obj.is_fibonacci(987);
	obj.is_fibonacci(14)
}
main();

Output

51 Is not a fibonacci number
144 Is a fibonacci number
987 Is a fibonacci number
14 Is not a fibonacci number


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.

New Comment







© 2021, kalkicode.com, All rights reserved