Find nth Catalan Number

Here given code implementation process.

// C Program for
// Find nth Catalan Number
#include <stdio.h>

void nthCatalanNumber(int n)
{
	if (n <= 0)
	{
		return;
	}
	int m = (n - 1) *2;
	unsigned long result = 1;
	// Binomial coefficient and find catalan number
	for (int i = 0; i < n - 1; ++i)
	{
		result = result *(m - i);
		result = result / (i + 1);
	}
	result = result / n;
	// Display calculated result
	printf("\n %d-th Catalan Number Is : %ld", n, result);
}
int main()
{
	//  Catalan Number
	//  --------------
	//  1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 
	//  16796, 58786, 208012, 742900, 2674440, 
	//  9694845, 35357670, 129644790, 477638700, 
	//  1767263190, 6564120420, 24466267020, 91482563640, 
	//  343059613650, 1289904147324, 4861946401452 .....
	// Test 
	nthCatalanNumber(10);
	nthCatalanNumber(7);
	nthCatalanNumber(12);
	return 0;
}

Output

 10-th Catalan Number Is : 4862
 7-th Catalan Number Is : 132
 12-th Catalan Number Is : 58786
// Java program for
// Find nth Catalan Number
public class CatalanNumber
{
	public void nthCatalanNumber(int n)
	{
		if (n <= 0)
		{
			return;
		}
		int m = (n - 1) * 2;
		long result = 1;
		// Binomial coefficient and find catalan number
		for (int i = 0; i < n - 1; ++i)
		{
			result = result * (m - i);
			result = result / (i + 1);
		}
		result = result / n;
		// Display calculated result
		System.out.print("\n " + n + "-th Catalan Number Is : " + result);
	}
	public static void main(String[] args)
	{
		CatalanNumber task = new CatalanNumber();
		//  Catalan Number
		//  --------------
		//  1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 
		//  16796, 58786, 208012, 742900, 2674440, 
		//  9694845, 35357670, 129644790, 477638700, 
		//  1767263190, 6564120420, 24466267020, 91482563640, 
		//  343059613650, 1289904147324, 4861946401452 ..... etc
		// Test 
		task.nthCatalanNumber(10);
		task.nthCatalanNumber(7);
		task.nthCatalanNumber(12);
	}
}

Output

 10-th Catalan Number Is : 4862
 7-th Catalan Number Is : 132
 12-th Catalan Number Is : 58786
// Include header file
#include <iostream>
using namespace std;

// C++ program for
// Find nth Catalan Number

class CatalanNumber
{
	public: void nthCatalanNumber(int n)
	{
		if (n <= 0)
		{
			return;
		}
		int m = (n - 1) *2;
		long result = 1;
		// Binomial coefficient and find catalan number
		for (int i = 0; i < n - 1; ++i)
		{
			result = result *(m - i);
			result = result / (i + 1);
		}
		result = result / n;
		// Display calculated result
		cout << "\n " << n << "-th Catalan Number Is : " << result;
	}
};
int main()
{
	CatalanNumber *task = new CatalanNumber();
	//  Catalan Number
	//  --------------
	//  1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 
	//  16796, 58786, 208012, 742900, 2674440, 
	//  9694845, 35357670, 129644790, 477638700, 
	//  1767263190, 6564120420, 24466267020, 91482563640, 
	//  343059613650, 1289904147324, 4861946401452 ..... etc
	// Test 
	task->nthCatalanNumber(10);
	task->nthCatalanNumber(7);
	task->nthCatalanNumber(12);
	return 0;
}

Output

 10-th Catalan Number Is : 4862
 7-th Catalan Number Is : 132
 12-th Catalan Number Is : 58786
// Include namespace system
using System;
// Csharp program for
// Find nth Catalan Number
public class CatalanNumber
{
	public void nthCatalanNumber(int n)
	{
		if (n <= 0)
		{
			return;
		}
		int m = (n - 1) * 2;
		long result = 1;
		// Binomial coefficient and find catalan number
		for (int i = 0; i < n - 1; ++i)
		{
			result = result * (m - i);
			result = result / (i + 1);
		}
		result = result / n;
		// Display calculated result
		Console.Write("\n " + n + "-th Catalan Number Is : " + result);
	}
	public static void Main(String[] args)
	{
		CatalanNumber task = new CatalanNumber();
		//  Catalan Number
		//  --------------
		//  1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 
		//  16796, 58786, 208012, 742900, 2674440, 
		//  9694845, 35357670, 129644790, 477638700, 
		//  1767263190, 6564120420, 24466267020, 91482563640, 
		//  343059613650, 1289904147324, 4861946401452 ..... etc
		// Test 
		task.nthCatalanNumber(10);
		task.nthCatalanNumber(7);
		task.nthCatalanNumber(12);
	}
}

Output

 10-th Catalan Number Is : 4862
 7-th Catalan Number Is : 132
 12-th Catalan Number Is : 58786
package main
import "fmt"
// Go program for
// Find nth Catalan Number

func nthCatalanNumber(n int) {
	if n <= 0 {
		return
	}
	var m int = (n - 1) * 2
	var result int = 1
	// Binomial coefficient and find catalan number
	for i := 0 ; i < n - 1 ; i++ {
		result = result * (m - i)
		result = result / (i + 1)
	}
	result = result / n
	// Display calculated result
	fmt.Print("\n ", n, "-th Catalan Number Is : ", result)
}
func main() {

	//  Catalan Number
	//  --------------
	//  1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 
	//  16796, 58786, 208012, 742900, 2674440, 
	//  9694845, 35357670, 129644790, 477638700, 
	//  1767263190, 6564120420, 24466267020, 91482563640, 
	//  343059613650, 1289904147324, 4861946401452 ..... etc
	// Test 
	nthCatalanNumber(10)
	nthCatalanNumber(7)
	nthCatalanNumber(12)
}

Output

 10-th Catalan Number Is : 4862
 7-th Catalan Number Is : 132
 12-th Catalan Number Is : 58786
<?php
// Php program for
// Find nth Catalan Number
class CatalanNumber
{
	public	function nthCatalanNumber($n)
	{
		if ($n <= 0)
		{
			return;
		}
		$m = ($n - 1) * 2;
		$result = 1;
		// Binomial coefficient and find catalan number
		for ($i = 0; $i < $n - 1; ++$i)
		{
			$result = $result * ($m - $i);
			$result = $result / ($i + 1);
		}
		$result = $result / $n;
		// Display calculated result
		echo("\n ".$n.
			"-th Catalan Number Is : ".$result);
	}
}

function main()
{
	$task = new CatalanNumber();
	//  Catalan Number
	//  --------------
	//  1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 
	//  16796, 58786, 208012, 742900, 2674440, 
	//  9694845, 35357670, 129644790, 477638700, 
	//  1767263190, 6564120420, 24466267020, 91482563640, 
	//  343059613650, 1289904147324, 4861946401452 ..... etc
	// Test 
	$task->nthCatalanNumber(10);
	$task->nthCatalanNumber(7);
	$task->nthCatalanNumber(12);
}
main();

Output

 10-th Catalan Number Is : 4862
 7-th Catalan Number Is : 132
 12-th Catalan Number Is : 58786
// Node JS program for
// Find nth Catalan Number
class CatalanNumber
{
	nthCatalanNumber(n)
	{
		if (n <= 0)
		{
			return;
		}
		var m = (n - 1) * 2;
		var result = 1;
		// Binomial coefficient and find catalan number
		for (var i = 0; i < n - 1; ++i)
		{
			result = result * (m - i);
			result = result / (i + 1);
		}
		result = result / n;
		// Display calculated result
		process.stdout.write("\n " + 
                             n + "-th Catalan Number Is : " + result);
	}
}

function main()
{
	var task = new CatalanNumber();
	//  Catalan Number
	//  --------------
	//  1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 
	//  16796, 58786, 208012, 742900, 2674440, 
	//  9694845, 35357670, 129644790, 477638700, 
	//  1767263190, 6564120420, 24466267020, 91482563640, 
	//  343059613650, 1289904147324, 4861946401452 ..... etc
	// Test 
	task.nthCatalanNumber(10);
	task.nthCatalanNumber(7);
	task.nthCatalanNumber(12);
}
main();

Output

 10-th Catalan Number Is : 4862
 7-th Catalan Number Is : 132
 12-th Catalan Number Is : 58786
#  Python 3 program for
#  Find nth Catalan Number
class CatalanNumber :
	def nthCatalanNumber(self, n) :
		if (n <= 0) :
			return
		
		m = (n - 1) * 2
		result = 1
		i = 0
		#  Binomial coefficient and find catalan number
		while (i < n - 1) :
			result = result * (m - i)
			result = result / (i + 1)
			i += 1
		
		result = int(result / n)
		#  Display calculated result
		print("\n ", n ,"-th Catalan Number Is : ", result, end = "",sep="")
	

def main() :
	task = CatalanNumber()
	#   Catalan Number
	#   --------------
	#   1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 
	#   16796, 58786, 208012, 742900, 2674440, 
	#   9694845, 35357670, 129644790, 477638700, 
	#   1767263190, 6564120420, 24466267020, 91482563640, 
	#   343059613650, 1289904147324, 4861946401452 ..... etc
	#  Test 
	task.nthCatalanNumber(10)
	task.nthCatalanNumber(7)
	task.nthCatalanNumber(12)

if __name__ == "__main__": main()

Output

 10-th Catalan Number Is : 4862
 7-th Catalan Number Is : 132
 12-th Catalan Number Is : 58786
#  Ruby program for
#  Find nth Catalan Number
class CatalanNumber 
	def nthCatalanNumber(n) 
		if (n <= 0) 
			return
		end

		m = (n - 1) * 2
		result = 1
		i = 0
		#  Binomial coefficient and find catalan number
		while (i < n - 1) 
			result = result * (m - i)
			result = result / (i + 1)
			i += 1
		end

		result = result / n
		#  Display calculated result
		print("\n ", n ,"-th Catalan Number Is : ", result)
	end

end

def main() 
	task = CatalanNumber.new()
	#   Catalan Number
	#   --------------
	#   1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 
	#   16796, 58786, 208012, 742900, 2674440, 
	#   9694845, 35357670, 129644790, 477638700, 
	#   1767263190, 6564120420, 24466267020, 91482563640, 
	#   343059613650, 1289904147324, 4861946401452 ..... etc
	#  Test 
	task.nthCatalanNumber(10)
	task.nthCatalanNumber(7)
	task.nthCatalanNumber(12)
end

main()

Output

 10-th Catalan Number Is : 4862
 7-th Catalan Number Is : 132
 12-th Catalan Number Is : 58786
// Scala program for
// Find nth Catalan Number
class CatalanNumber()
{
	def nthCatalanNumber(n: Int): Unit = {
		if (n <= 0)
		{
			return;
		}
		var m: Int = (n - 1) * 2;
		var result: Long = 1;
		var i: Int = 0;
		// Binomial coefficient and find catalan number
		while (i < n - 1)
		{
			result = result * (m - i);
			result = result / (i + 1);
			i += 1;
		}
		result = result / n;
		// Display calculated result
		print("\n " + n + "-th Catalan Number Is : " + result);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: CatalanNumber = new CatalanNumber();
		//  Catalan Number
		//  --------------
		//  1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 
		//  16796, 58786, 208012, 742900, 2674440, 
		//  9694845, 35357670, 129644790, 477638700, 
		//  1767263190, 6564120420, 24466267020, 91482563640, 
		//  343059613650, 1289904147324, 4861946401452 ..... etc
		// Test 
		task.nthCatalanNumber(10);
		task.nthCatalanNumber(7);
		task.nthCatalanNumber(12);
	}
}

Output

 10-th Catalan Number Is : 4862
 7-th Catalan Number Is : 132
 12-th Catalan Number Is : 58786
// Swift 4 program for
// Find nth Catalan Number
class CatalanNumber
{
	func nthCatalanNumber(_ n: Int)
	{
		if (n <= 0)
		{
			return;
		}
		let m: Int = (n - 1) * 2;
		var result: Int = 1;
		var i: Int = 0;
		// Binomial coefficient and find catalan number
		while (i < n - 1)
		{
			result = result * (m - i);
			result = result / (i + 1);
			i += 1;
		}
		result = result / n;
		// Display calculated result
		print("\n \(n)-th Catalan Number Is : ", 
              result, terminator: "");
	}
}
func main()
{
	let task: CatalanNumber = CatalanNumber();
	//  Catalan Number
	//  --------------
	//  1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 
	//  16796, 58786, 208012, 742900, 2674440, 
	//  9694845, 35357670, 129644790, 477638700, 
	//  1767263190, 6564120420, 24466267020, 91482563640, 
	//  343059613650, 1289904147324, 4861946401452 ..... etc
	// Test 
	task.nthCatalanNumber(10);
	task.nthCatalanNumber(7);
	task.nthCatalanNumber(12);
}
main();

Output

 10-th Catalan Number Is :  4862
 7-th Catalan Number Is :  132
 12-th Catalan Number Is :  58786
// Kotlin program for
// Find nth Catalan Number
class CatalanNumber
{
	fun nthCatalanNumber(n: Int): Unit
	{
		if (n <= 0)
		{
			return;
		}
		val m: Int = (n - 1) * 2;
		var result: Long = 1;
		var i: Int = 0;
		// Binomial coefficient and find catalan number
		while (i < n - 1)
		{
			result = result * (m - i);
			result = result / (i + 1);
			i += 1;
		}
		result = result / n;
		// Display calculated result
		print("\n " + n + "-th Catalan Number Is : " + result);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: CatalanNumber = CatalanNumber();
	//  Catalan Number
	//  --------------
	//  1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 
	//  16796, 58786, 208012, 742900, 2674440, 
	//  9694845, 35357670, 129644790, 477638700, 
	//  1767263190, 6564120420, 24466267020, 91482563640, 
	//  343059613650, 1289904147324, 4861946401452 ..... etc
	// Test 
	task.nthCatalanNumber(10);
	task.nthCatalanNumber(7);
	task.nthCatalanNumber(12);
}

Output

 10-th Catalan Number Is : 4862
 7-th Catalan Number Is : 132
 12-th Catalan Number Is : 58786


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