Find Nth Odd fibonacci number

Here given code implementation process.

// C program
// Find Nth Odd fibonacci number
#include <stdio.h>

//Find the nth odd element in fibonacci series
void odd_fibonacci(int n_th)
{
	if (n_th <= 0)
	{
		return;
	}
	//Get location of nth odd element in fibonacci series
	int n = (3 * n_th + 1) / 2;
	//Define resultant variable
	int result = 0;
	int a = 0;
	int b = 1;
	//Find nth element in fibonacci serie
	for (int i = 1; i <= n; i++)
	{
		result = a;
		b = b + a;
		a = b - a;
	}
	//Display the calculated result result
	printf("\n %d-th Odd fibonacci element is : %d ", n_th, result);
}
int main()
{
	//Test case
	odd_fibonacci(2);
	odd_fibonacci(9);
	odd_fibonacci(6);
	odd_fibonacci(4);
	odd_fibonacci(7);
	return 0;
}

Output

 2-th Odd fibonacci element is : 1
 9-th Odd fibonacci element is : 233
 6-th Odd fibonacci element is : 21
 4-th Odd fibonacci element is : 5
 7-th Odd fibonacci element is : 55
// Java program 
// Find nth odd fibonacci number
class FibonacciNo
{
	//Find the nth odd element in fibonacci series
	public void odd_fibonacci(int n_th)
	{
		if (n_th <= 0)
		{
			return;
		}
		//Get location of nth odd element in fibonacci series
		int n = (3 * n_th + 1) / 2;
		//Define resultant variable
		int result = 0;
		int a = 0;
		int b = 1;
		//Find nth element in fibonacci serie
		for (int i = 1; i <= n; i++)
		{
			result = a;
			b = b + a;
			a = b - a;
		}
		//Display the calculated result result
		System.out.print("\n " + n_th + "-th Odd fibonacci element is : " + result );
	}
	public static void main(String[] args)
	{
		FibonacciNo obj = new FibonacciNo();
		//Test case
		obj.odd_fibonacci(2);
		obj.odd_fibonacci(9);
		obj.odd_fibonacci(6);
		obj.odd_fibonacci(4);
		obj.odd_fibonacci(7);
	}
}

Output

 2-th Odd fibonacci element is : 1
 9-th Odd fibonacci element is : 233
 6-th Odd fibonacci element is : 21
 4-th Odd fibonacci element is : 5
 7-th Odd fibonacci element is : 55
//Include header file
#include <iostream>
using namespace std;

// C++ program 
// Find nth odd fibonacci number

class FibonacciNo
{
	public:
		//Find the nth odd element in fibonacci series
		void odd_fibonacci(int n_th)
		{
			if (n_th <= 0)
			{
				return;
			}
			//Get location of nth odd element in fibonacci series
			int n = (3 *n_th + 1) / 2;
			//Define resultant variable
			int result = 0;
			int a = 0;
			int b = 1;
			//Find nth element in fibonacci serie
			for (int i = 1; i <= n; i++)
			{
				result = a;
				b = b + a;
				a = b - a;
			}
			//Display the calculated result result
			cout << "\n " << n_th << "-th Odd fibonacci element is : " << result;
		}
};
int main()
{
	FibonacciNo obj = FibonacciNo();
	//Test case
	obj.odd_fibonacci(2);
	obj.odd_fibonacci(9);
	obj.odd_fibonacci(6);
	obj.odd_fibonacci(4);
	obj.odd_fibonacci(7);
	return 0;
}

Output

 2-th Odd fibonacci element is : 1
 9-th Odd fibonacci element is : 233
 6-th Odd fibonacci element is : 21
 4-th Odd fibonacci element is : 5
 7-th Odd fibonacci element is : 55
//Include namespace system
using System;

// C# program 
// Find nth odd fibonacci number

class FibonacciNo
{
	//Find the nth odd element in fibonacci series
	public void odd_fibonacci(int n_th)
	{
		if (n_th <= 0)
		{
			return;
		}
		//Get location of nth odd element in fibonacci series
		int n = (3 * n_th + 1) / 2;
		//Define resultant variable
		int result = 0;
		int a = 0;
		int b = 1;
		//Find nth element in fibonacci serie
		for (int i = 1; i <= n; i++)
		{
			result = a;
			b = b + a;
			a = b - a;
		}
		//Display the calculated result result
		Console.Write("\n " + n_th + "-th Odd fibonacci element is : " + result);
	}
	public static void Main(String[] args)
	{
		FibonacciNo obj = new FibonacciNo();
		//Test case
		obj.odd_fibonacci(2);
		obj.odd_fibonacci(9);
		obj.odd_fibonacci(6);
		obj.odd_fibonacci(4);
		obj.odd_fibonacci(7);
	}
}

Output

 2-th Odd fibonacci element is : 1
 9-th Odd fibonacci element is : 233
 6-th Odd fibonacci element is : 21
 4-th Odd fibonacci element is : 5
 7-th Odd fibonacci element is : 55
<?php
// Php program 
// Find nth odd fibonacci number

class FibonacciNo
{
	//Find the nth odd element in fibonacci series
	public	function odd_fibonacci($n_th)
	{
		if ($n_th <= 0)
		{
			return;
		}
		//Get location of nth odd element in fibonacci series
		$n = intval((3 * $n_th + 1) / 2);
		//Define resultant variable
		$result = 0;
		$a = 0;
		$b = 1;
		//Find nth element in fibonacci serie
		for ($i = 1; $i <= $n; $i++)
		{
			$result = $a;
			$b = $b + $a;
			$a = $b - $a;
		}
		//Display the calculated result result
		echo "\n ". $n_th ."-th Odd fibonacci element is : ". $result;
	}
}

function main()
{
	$obj = new FibonacciNo();
	//Test case
	$obj->odd_fibonacci(2);
	$obj->odd_fibonacci(9);
	$obj->odd_fibonacci(6);
	$obj->odd_fibonacci(4);
	$obj->odd_fibonacci(7);
}
main();

Output

 2-th Odd fibonacci element is : 1
 9-th Odd fibonacci element is : 233
 6-th Odd fibonacci element is : 21
 4-th Odd fibonacci element is : 5
 7-th Odd fibonacci element is : 55
// Node Js program 
// Find nth odd fibonacci number
class FibonacciNo
{
	//Find the nth odd element in fibonacci series
	odd_fibonacci(n_th)
	{
		if (n_th <= 0)
		{
			return;
		}
		//Get location of nth odd element in fibonacci series
		var n = parseInt((3 * n_th + 1) / 2);
		//Define resultant variable
		var result = 0;
		var a = 0;
		var b = 1;
		//Find nth element in fibonacci serie
		for (var i = 1; i <= n; i++)
		{
			result = a;
			b = b + a;
			a = b - a;
		}
		//Display the calculated result result
		process.stdout.write("\n " + n_th + "-th Odd fibonacci element is : " + result);
	}
}

function main()
{
	var obj = new FibonacciNo();
	//Test case
	obj.odd_fibonacci(2);
	obj.odd_fibonacci(9);
	obj.odd_fibonacci(6);
	obj.odd_fibonacci(4);
	obj.odd_fibonacci(7);
}
main();

Output

 2-th Odd fibonacci element is : 1
 9-th Odd fibonacci element is : 233
 6-th Odd fibonacci element is : 21
 4-th Odd fibonacci element is : 5
 7-th Odd fibonacci element is : 55
#  Python 3 program 
#  Find nth odd fibonacci number

class FibonacciNo :
	# Find the nth odd element in fibonacci series
	def odd_fibonacci(self, n_th) :
		if (n_th <= 0) :
			return
		
		# Get location of nth odd element in fibonacci series
		n = int((3 * n_th + 1) / 2)
		# Define resultant variable
		result = 0
		a = 0
		b = 1
		i = 1
		# Find nth element in fibonacci serie
		while (i <= n) :
			result = a
			b = b + a
			a = b - a
			i += 1
		
		# Display the calculated result result
		print("\n ", n_th ,"-th Odd fibonacci element is : ", result, end = "")
	

def main() :
	obj = FibonacciNo()
	# Test case
	obj.odd_fibonacci(2)
	obj.odd_fibonacci(9)
	obj.odd_fibonacci(6)
	obj.odd_fibonacci(4)
	obj.odd_fibonacci(7)

if __name__ == "__main__": main()

Output

  2 -th Odd fibonacci element is :  1
  9 -th Odd fibonacci element is :  233
  6 -th Odd fibonacci element is :  21
  4 -th Odd fibonacci element is :  5
  7 -th Odd fibonacci element is :  55
#  Ruby program 
#  Find nth odd fibonacci number

class FibonacciNo 
	# Find the nth odd element in fibonacci series
	def odd_fibonacci(n_th) 
		if (n_th <= 0) 
			return
		end

		# Get location of nth odd element in fibonacci series
		n = (3 * n_th + 1) / 2
		# Define resultant variable
		result = 0
		a = 0
		b = 1
		i = 1
		# Find nth element in fibonacci serie
		while (i <= n) 
			result = a
			b = b + a
			a = b - a
			i += 1
		end

		# Display the calculated result result
		print("\n ", n_th ,"-th Odd fibonacci element is : ", result)
	end

end

def main() 
	obj = FibonacciNo.new()
	# Test case
	obj.odd_fibonacci(2)
	obj.odd_fibonacci(9)
	obj.odd_fibonacci(6)
	obj.odd_fibonacci(4)
	obj.odd_fibonacci(7)
end

main()

Output

 2-th Odd fibonacci element is : 1
 9-th Odd fibonacci element is : 233
 6-th Odd fibonacci element is : 21
 4-th Odd fibonacci element is : 5
 7-th Odd fibonacci element is : 55
// Scala program 
// Find nth odd fibonacci number
class FibonacciNo
{
	//Find the nth odd element in fibonacci series
	def odd_fibonacci(n_th: Int): Unit = {
		if (n_th <= 0)
		{
			return;
		}
		//Get location of nth odd element in fibonacci series
		var n: Int = ((3 * n_th + 1) / 2).toInt;
		//Define resultant variable
		var result: Int = 0;
		var a: Int = 0;
		var b: Int = 1;
		var i: Int = 1;
		//Find nth element in fibonacci serie
		while (i <= n)
		{
			result = a;
			b = b + a;
			a = b - a;
			i += 1;
		}
		//Display the calculated result result
		print("\n " + n_th + "-th Odd fibonacci element is : " + result);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: FibonacciNo = new FibonacciNo();
		//Test case
		obj.odd_fibonacci(2);
		obj.odd_fibonacci(9);
		obj.odd_fibonacci(6);
		obj.odd_fibonacci(4);
		obj.odd_fibonacci(7);
	}
}

Output

 2-th Odd fibonacci element is : 1
 9-th Odd fibonacci element is : 233
 6-th Odd fibonacci element is : 21
 4-th Odd fibonacci element is : 5
 7-th Odd fibonacci element is : 55
// Swift 4 program 
// Find nth odd fibonacci number
class FibonacciNo
{
	//Find the nth odd element in fibonacci series
	func odd_fibonacci(_ n_th: Int)
	{
		if (n_th <= 0)
		{
			return;
		}
		//Get location of nth odd element in fibonacci series
		let n: Int = (3 * n_th + 1) / 2;
		//Define resultant variable
		var result: Int = 0;
		var a: Int = 0;
		var b: Int = 1;
		var i: Int = 1;
		//Find nth element in fibonacci serie
		while (i <= n)
		{
			result = a;
			b = b + a;
			a = b - a;
			i += 1;
		}
		//Display the calculated result result
		print("\n ", n_th ,"-th Odd fibonacci element is : ", result, terminator: "");
	}
}
func main()
{
	let obj: FibonacciNo = FibonacciNo();
	//Test case
	obj.odd_fibonacci(2);
	obj.odd_fibonacci(9);
	obj.odd_fibonacci(6);
	obj.odd_fibonacci(4);
	obj.odd_fibonacci(7);
}
main();

Output

  2 -th Odd fibonacci element is :  1
  9 -th Odd fibonacci element is :  233
  6 -th Odd fibonacci element is :  21
  4 -th Odd fibonacci element is :  5
  7 -th Odd fibonacci element is :  55

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