Catalan number series

Here given code implementation process.

//C Program
//Print catalan number
#include <stdio.h>
//Returns a given catalan number
unsigned long int catalan( unsigned long int number) {
  
   if (number <= 1){
      // Base to control recursion process
      // Here are ending of execution  recursion 
      return 1;
   }

   unsigned long int result = 0;

   for (unsigned long i=0; i<number; i++){
      // Recursively calculating of catalan number 
      result += catalan(i)*catalan((number-i)-1);
   }

   return result;
}
//Function which is handling the request of printing catalan number
void catalan_number(int size)
{
  printf("\nInitial %d catalan numbers is : \n", size);

  for (unsigned long i = 0; i < size; i++)
  { 
    // Display number
    printf("%ld ", catalan(i));
  }
}

int main() {

  // Test Case
  catalan_number(10);
  catalan_number(15);
  return 0;
}

Output

Initial 10 catalan numbers is :
1 1 2 5 14 42 132 429 1430 4862
Initial 15 catalan numbers is :
1 1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440
/*
 C++ Program
 Print catalan number
*/
#include<iostream>
using namespace std;

class MyNumber {
	public:

		//Returns a given catalan number
		unsigned long catalan(unsigned long number) {
			if (number <= 1) {
				return 1;
			}
			int result = 0;
			for (unsigned long i = 0; i < number; i++) {
				// Recursively calculating of catalan number 
				result += this->catalan(i) *this->catalan((number - i) - 1);
			}
			return result;
		}
	//Function which is handling the request of printing catalan number
	void catalan_number(int size) {
		cout << "\nInitial " << size << " catalan numbers is : \n";
		for (unsigned long i = 0; i < size; i++) {
			// Display number

			cout << " " << this->catalan(i);
		}
	}
};
int main() {
	MyNumber obj ;
	// Test Case
	obj.catalan_number(10);
	obj.catalan_number(15);
	return 0;
}

Output

Initial 10 catalan numbers is :
 1 1 2 5 14 42 132 429 1430 4862
Initial 15 catalan numbers is :
 1 1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440
/*
  Java Program
  Print catalan number
*/

public class MyNumber {

  //Returns a given catalan number
  public int catalan(int number) {
    
    if (number <= 1){
      // Base to control recursion process
      // Here are ending of execution  recursion 
      return 1;
    }

    int result = 0;

    for (int i = 0; i < number; i++){
      // Recursively calculating of catalan number 
      result += catalan(i)*catalan((number-i)-1);
    }

    return result;
  }
  //Function which is handling the request of printing catalan number
  public void catalan_number(int size)
  {
    System.out.print("\nInitial "+size+" catalan numbers is : \n");

    for (int i = 0; i < size; i++)
    { 
      // Display number
      System.out.print(" "+catalan(i));
    }
  }


  public static void main(String[] args) {

    MyNumber obj = new MyNumber();
  
    // Test Case
    obj.catalan_number(10);
    obj.catalan_number(15);
  }
}

Output

Initial 10 catalan numbers is :
1 1 2 5 14 42 132 429 1430 4862
Initial 15 catalan numbers is :
1 1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440
/*
  C# Program
  Print catalan number
*/
using System;
public class MyNumber {

	//Returns a given catalan number
	public int catalan(int number) {

		if (number <= 1) {
			// Base to control recursion process
			// Here are ending of execution  recursion 
			return 1;
		}

		int result = 0;

		for (int i = 0; i < number; i++) {
			// Recursively calculating of catalan number 
			result += catalan(i) * catalan((number - i) - 1);
		}

		return result;
	}
	//Function which is handling the request of printing catalan number
	public void catalan_number(int size) {
		Console.Write("\nInitial " + size + " catalan numbers is : \n");

		for (int i = 0; i < size; i++) {
			// Display number
			Console.Write(" " + catalan(i));
		}
	}


	public static void Main(String[] args) {

		MyNumber obj = new MyNumber();

		// Test Case
		obj.catalan_number(10);
		obj.catalan_number(15);
	}
}

Output

Initial 10 catalan numbers is :
 1 1 2 5 14 42 132 429 1430 4862
Initial 15 catalan numbers is :
 1 1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440
# Python 3 Program
# Print catalan number
class MyNumber :
  #Returns a given catalan number
  def catalan(self, number) :
    if (number <= 1) :
      return 1
    
    result = 0
    i = 0
    while (i < number) :
      # Recursively calculating of catalan number 
      result += self.catalan(i) * self.catalan((number - i) - 1)
      i += 1
    
    return result
  
  #Function which is handling the request of printing catalan number
  def catalan_number(self, size) :
    print("\nInitial ", size ," catalan numbers is : ")
    i = 0
    while (i < size) :
      print(" ", self.catalan(i),end="")
      i += 1
    
  

def main() :
  obj = MyNumber()
  obj.catalan_number(10)
  obj.catalan_number(15)


if __name__ == "__main__":
  main()

Output

Initial 10 catalan numbers is :
 1 1 2 5 14 42 132 429 1430 4862
Initial 15 catalan numbers is :
 1 1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440
# Ruby Program 
# Print catalan number
class MyNumber 
	#Returns a given catalan number
	def catalan(number) 
		if (number <= 1) 
			return 1
		end
		result = 0
		i = 0
		while (i < number) 
			# Recursively calculating of catalan number 
			result += self.catalan(i) * self.catalan((number - i) - 1)
			i += 1
		end
		return result
	end
	#Function which is handling the request of printing catalan number
	def catalan_number(size) 
		print("\nInitial ", size ," catalan numbers is  :\n")
		i = 0
		while (i < size) 
			print(" ", self.catalan(i))
			i += 1
		end
	end
end
def main() 
	obj = MyNumber.new()
	obj.catalan_number(10)
	obj.catalan_number(15)
end
main()

Output

Initial 10 catalan numbers is  :
 1 1 2 5 14 42 132 429 1430 4862
Initial 15 catalan numbers is  :
 1 1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440
/*
 Scala Program
 Print catalan number
*/
class MyNumber {
	//Returns a given catalan number
	def catalan(number: Int): Int = {
		if (number <= 1) {
			return 1;
		}
		var result: Int = 0;
		var i: Int = 0;
		while (i < number) {
			// Recursively calculating of catalan number 
			result += this.catalan(i) * this.catalan((number - i) - 1);
			i += 1;
		}
		return result;
	}
	//Function which is handling the request of printing catalan number
	def catalan_number(size: Int): Unit = {
		print(s"\nInitial $size catalan numbers is : \n");
		var i: Int = 0;
		while (i < size) {
			print(s" ${this.catalan(i)}");
			i += 1;
		}
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		var obj: MyNumber = new MyNumber();
  		//Test Case
        obj.catalan_number(10)
        obj.catalan_number(15);
	}
}

Output

Initial 10 catalan numbers is :
 1 1 2 5 14 42 132 429 1430 4862
Initial 15 catalan numbers is :
 1 1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440
/*
  Swift 4 Program
  Print catalan number
*/
class MyNumber {
	//Returns a given catalan number
	func catalan(_ number: Int) -> Int {
		if (number <= 1) {
			return 1;
		}
		var result: Int = 0;
		var i: Int = 0;
		while (i < number) {
			// Recursively calculating of catalan number 
			result += self.catalan(i) * self.catalan((number - i) - 1);
			i += 1;
		}
		return result;
	}
	//Function which is handling the request of printing catalan number
	func catalan_number(_ size: Int) {
		print("\nInitial ", size ," catalan numbers is : ");
		var i: Int = 0;
		while (i < size) {
			print(" ", self.catalan(i),terminator:"");
			i += 1;
		}
	}
}
func main() {
	let obj: MyNumber = MyNumber();
	//Test Case
	obj.catalan_number(10);
	obj.catalan_number(15);
}
main();

Output

Initial  10  catalan numbers is :
  1  1  2  5  14  42  132  429  1430  4862
Initial  15  catalan numbers is :
  1  1  2  5  14  42  132  429  1430  4862  16796  58786  208012  742900  2674440
<?php
/*
  Php Program
  Print catalan number
*/
class MyNumber {
	//Returns a given catalan number

	public 	function catalan($number) {
		if ($number <= 1) {
			return 1;
		}
		$result = 0;
		for ($i = 0; $i < $number; $i++) {
			// Recursively calculating of catalan number 
			$result += $this->catalan($i) *$this->catalan(($number - $i) - 1);
		}
		return $result;
	}
	//Function which is handling the request of printing catalan number

	public 	function catalan_number($size) {
		echo("\nInitial ". $size ." catalan numbers is : \n");
		for ($i = 0; $i < $size; $i++) {
			// Display number

			echo(" ". $this->catalan($i));
		}
	}
};

function main() {
	$obj = new MyNumber();
	// Test Case

	$obj->catalan_number(10);
	$obj->catalan_number(15);
}
main();

Output

Initial 10 catalan numbers is :
 1 1 2 5 14 42 132 429 1430 4862
Initial 15 catalan numbers is :
 1 1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440
/*
 Node Js Program
 Print catalan number
*/
class MyNumber {
	//Returns a given catalan number
	catalan(number) {
		if (number <= 1) {
			return 1;
		}
		var result = 0;
		for (var i = 0; i < number; i++) {
			// Recursively calculating of catalan number 
			result += this.catalan(i) *this.catalan((number - i) - 1);
		}
		return result;
	}
	//Function which is handling the request of printing catalan number
	catalan_number(size) {
		process.stdout.write("\nInitial " + size + " catalan numbers is : \n");
		for (var i = 0; i < size; i++) {
			// Display number

			process.stdout.write(" " + this.catalan(i));
		}
	}
}

function main(args) {
	var obj = new MyNumber();
	// Test Case
	obj.catalan_number(10);
	obj.catalan_number(15)
}
main();

Output

Initial 10 catalan numbers is :
 1 1 2 5 14 42 132 429 1430 4862
Initial 15 catalan numbers is :
 1 1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440


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