Bernoulli's triangle

Here given code implementation process.

// C Program for
// Bernoulli's triangle
#include <stdio.h>

void bernoulliTriangle(int n)
{
	if (n <= 0)
	{
		return;
	}
	// This problem are need N X N space
	// But we are solve this problem using using only two rows
	// Here 2 indicates row
	// And n+1 indicate column value
	int dp[2][n + 1];
	// Set starting column value
	dp[0][0] = 1;
	dp[1][0] = 1;
	// We can solve this problem only use two rows
	// So initial select second row which position is 1
	int row = 1;
	printf("\n Given n : %d \n", n);
	for (int i = 1; i <= n; ++i)
	{
		// Set intial value of column
		dp[0][i] = 0;
		dp[1][i] = 0;
		// This loop are used to print result in current row
		for (int j = 0; j < i; ++j)
		{
			if (j > 0)
			{
				if (j + 1 == i)
				{
					// Last element of current row
					dp[row][j] = dp[row][j - 1] + 1;
				}
				else if (row == 1)
				{
					// Change second row
					dp[row][j] = dp[0][j] + dp[0][j - 1];
				}
				else
				{
					// Change first row
					dp[row][j] = dp[1][j] + dp[1][j - 1];
				}
			}
			// Display the value
			printf("  %d", dp[row][j]);
		}
		printf("\n");
		// Change row
		if (row == 1)
		{
			row = 0;
		}
		else
		{
			row = 1;
		}
	}
}
int main()
{
	/*
	    n = 6
	    --------- 
	    1
	    1  2
	    1  3  4
	    1  4  7  8
	    1  5  11  15  16
	    1  6  16  26  31  32
	*/
	bernoulliTriangle(6);
	/* 
	    n = 10
	    ----------
	    1
	    1  2
	    1  3  4
	    1  4  7  8
	    1  5  11  15  16
	    1  6  16  26  31  32
	    1  7  22  42  57  63  64
	    1  8  29  64  99  120  127  128
	    1  9  37  93  163  219  247  255  256
	    1  10  46  130  256  382  466  502  511  512
	*/
	bernoulliTriangle(10);
	return 0;
}

Output

 Given n : 6
  1
  1  2
  1  3  4
  1  4  7  8
  1  5  11  15  16
  1  6  16  26  31  32

 Given n : 10
  1
  1  2
  1  3  4
  1  4  7  8
  1  5  11  15  16
  1  6  16  26  31  32
  1  7  22  42  57  63  64
  1  8  29  64  99  120  127  128
  1  9  37  93  163  219  247  255  256
  1  10  46  130  256  382  466  502  511  512
// Java program for
// Bernoulli's triangle
public class Triangle
{
	public void bernoulliTriangle(int n)
	{
		if (n <= 0)
		{
			return;
		}
		// This problem are need N X N space
		// But we are solve this problem using using only two rows
		// Here 2 indicates row
		// And n+1 indicate column value
		int[][] dp = new int[2][n + 1];
		// Set starting column value
		dp[0][0] = 1;
		dp[1][0] = 1;
		// We can solve this problem only use two rows
		// So initial select second row which position is 1
		int row = 1;
		System.out.print("\n Given n : " + n + " \n");
		for (int i = 1; i <= n; ++i)
		{
			// Set intial value of column
			dp[0][i] = 0;
			dp[1][i] = 0;
			// This loop are used to print result in current row
			for (int j = 0; j < i; ++j)
			{
				if (j > 0)
				{
					if (j + 1 == i)
					{
						// Last element of current row
						dp[row][j] = dp[row][j - 1] + 1;
					}
					else if (row == 1)
					{
						// Change second row
						dp[row][j] = dp[0][j] + dp[0][j - 1];
					}
					else
					{
						// Change first row
						dp[row][j] = dp[1][j] + dp[1][j - 1];
					}
				}
				// Display the value
				System.out.print(" " + dp[row][j]);
			}
			System.out.print("\n");
			// Change row
			if (row == 1)
			{
				row = 0;
			}
			else
			{
				row = 1;
			}
		}
	}
	public static void main(String[] args)
	{
		Triangle task = new Triangle();
		/*
		    n = 6
		    --------- 
		    1
		    1  2
		    1  3  4
		    1  4  7  8
		    1  5  11  15  16
		    1  6  16  26  31  32
		*/
		task.bernoulliTriangle(6);
		/* 
		    n = 10
		    ----------
		    1
		    1  2
		    1  3  4
		    1  4  7  8
		    1  5  11  15  16
		    1  6  16  26  31  32
		    1  7  22  42  57  63  64
		    1  8  29  64  99  120  127  128
		    1  9  37  93  163  219  247  255  256
		    1  10  46  130  256  382  466  502  511  512
		*/
		task.bernoulliTriangle(10);
	}
}

Output

 Given n : 6
 1
 1 2
 1 3 4
 1 4 7 8
 1 5 11 15 16
 1 6 16 26 31 32

 Given n : 10
 1
 1 2
 1 3 4
 1 4 7 8
 1 5 11 15 16
 1 6 16 26 31 32
 1 7 22 42 57 63 64
 1 8 29 64 99 120 127 128
 1 9 37 93 163 219 247 255 256
 1 10 46 130 256 382 466 502 511 512
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Bernoulli's triangle
class Triangle
{
	public: void bernoulliTriangle(int n)
	{
		if (n <= 0)
		{
			return;
		}
		// This problem are need N X N space
		// But we are solve this problem using using only two rows
		// Here 2 indicates row
		// And n+1 indicate column value
		int dp[2][n + 1];
		// Set starting column value
		dp[0][0] = 1;
		dp[1][0] = 1;
		// We can solve this problem only use two rows
		// So initial select second row which position is 1
		int row = 1;
		cout << "\n Given n : " << n << " \n";
		for (int i = 1; i <= n; ++i)
		{
			// Set intial value of column
			dp[0][i] = 0;
			dp[1][i] = 0;
			// This loop are used to print result in current row
			for (int j = 0; j < i; ++j)
			{
				if (j > 0)
				{
					if (j + 1 == i)
					{
						// Last element of current row
						dp[row][j] = dp[row][j - 1] + 1;
					}
					else if (row == 1)
					{
						// Change second row
						dp[row][j] = dp[0][j] + dp[0][j - 1];
					}
					else
					{
						// Change first row
						dp[row][j] = dp[1][j] + dp[1][j - 1];
					}
				}
				// Display the value
				cout << " " << dp[row][j];
			}
			cout << "\n";
			// Change row
			if (row == 1)
			{
				row = 0;
			}
			else
			{
				row = 1;
			}
		}
	}
};
int main()
{
	Triangle *task = new Triangle();
	/*
	    n = 6
	    --------- 
	    1
	    1  2
	    1  3  4
	    1  4  7  8
	    1  5  11  15  16
	    1  6  16  26  31  32
	*/
	task->bernoulliTriangle(6);
	/*
	    n = 10
	    ----------
	    1
	    1  2
	    1  3  4
	    1  4  7  8
	    1  5  11  15  16
	    1  6  16  26  31  32
	    1  7  22  42  57  63  64
	    1  8  29  64  99  120  127  128
	    1  9  37  93  163  219  247  255  256
	    1  10  46  130  256  382  466  502  511  512
	*/
	task->bernoulliTriangle(10);
	return 0;
}

Output

 Given n : 6
 1
 1 2
 1 3 4
 1 4 7 8
 1 5 11 15 16
 1 6 16 26 31 32

 Given n : 10
 1
 1 2
 1 3 4
 1 4 7 8
 1 5 11 15 16
 1 6 16 26 31 32
 1 7 22 42 57 63 64
 1 8 29 64 99 120 127 128
 1 9 37 93 163 219 247 255 256
 1 10 46 130 256 382 466 502 511 512
// Include namespace system
using System;
// Csharp program for
// Bernoulli's triangle
public class Triangle
{
	public void bernoulliTriangle(int n)
	{
		if (n <= 0)
		{
			return;
		}
		// This problem are need N X N space
		// But we are solve this problem using using only two rows
		// Here 2 indicates row
		// And n+1 indicate column value
		int[,] dp = new int[2,n + 1];
		// Set starting column value
		dp[0,0] = 1;
		dp[1,0] = 1;
		// We can solve this problem only use two rows
		// So initial select second row which position is 1
		int row = 1;
		Console.Write("\n Given n : " + n + " \n");
		for (int i = 1; i <= n; ++i)
		{
			// Set intial value of column
			dp[0,i] = 0;
			dp[1,i] = 0;
			// This loop are used to print result in current row
			for (int j = 0; j < i; ++j)
			{
				if (j > 0)
				{
					if (j + 1 == i)
					{
						// Last element of current row
						dp[row,j] = dp[row,j - 1] + 1;
					}
					else if (row == 1)
					{
						// Change second row
						dp[row,j] = dp[0,j] + dp[0,j - 1];
					}
					else
					{
						// Change first row
						dp[row,j] = dp[1,j] + dp[1,j - 1];
					}
				}
				// Display the value
				Console.Write(" " + dp[row,j]);
			}
			Console.Write("\n");
			// Change row
			if (row == 1)
			{
				row = 0;
			}
			else
			{
				row = 1;
			}
		}
	}
	public static void Main(String[] args)
	{
		Triangle task = new Triangle();
		/*
		    n = 6
		    --------- 
		    1
		    1  2
		    1  3  4
		    1  4  7  8
		    1  5  11  15  16
		    1  6  16  26  31  32
		*/
		task.bernoulliTriangle(6);
		/*
		    n = 10
		    ----------
		    1
		    1  2
		    1  3  4
		    1  4  7  8
		    1  5  11  15  16
		    1  6  16  26  31  32
		    1  7  22  42  57  63  64
		    1  8  29  64  99  120  127  128
		    1  9  37  93  163  219  247  255  256
		    1  10  46  130  256  382  466  502  511  512
		*/
		task.bernoulliTriangle(10);
	}
}

Output

 Given n : 6
 1
 1 2
 1 3 4
 1 4 7 8
 1 5 11 15 16
 1 6 16 26 31 32

 Given n : 10
 1
 1 2
 1 3 4
 1 4 7 8
 1 5 11 15 16
 1 6 16 26 31 32
 1 7 22 42 57 63 64
 1 8 29 64 99 120 127 128
 1 9 37 93 163 219 247 255 256
 1 10 46 130 256 382 466 502 511 512
package main
import "fmt"
// Go program for
// Bernoulli's triangle

func bernoulliTriangle(n int) {
	if n <= 0 {
		return
	}
	// This problem are need N X N space
	// But we are solve this problem using using only two rows
	// Here 2 indicates row
	// And n+1 indicate column value
	var dp = make([][] int, 2)
	for i := 0; i < 2; i++ {
		dp[i] = make([] int, n + 1)
	}
	// Set starting column value
	dp[0][0] = 1
	dp[1][0] = 1
	// We can solve this problem only use two rows
	// So initial select second row which position is 1
	var row int = 1
	fmt.Print("\n Given n : ", n, " \n")
	for i := 1 ; i <= n ; i++ {
		// Set intial value of column
		dp[0][i] = 0
		dp[1][i] = 0
		// This loop are used to print result in current row
		for j := 0 ; j < i ; j++ {
			if j > 0 {
				if j + 1 == i {
					// Last element of current row
					dp[row][j] = dp[row][j - 1] + 1
				} else if row == 1 {
					// Change second row
					dp[row][j] = dp[0][j] + dp[0][j - 1]
				} else {
					// Change first row
					dp[row][j] = dp[1][j] + dp[1][j - 1]
				}
			}
			// Display the value
			fmt.Print(" ", dp[row][j])
		}
		fmt.Print("\n")
		// Change row
		if row == 1 {
			row = 0
		} else {
			row = 1
		}
	}
}
func main() {
	
	/*
	    n = 6
	    --------- 
	    1
	    1  2
	    1  3  4
	    1  4  7  8
	    1  5  11  15  16
	    1  6  16  26  31  32
	*/
	bernoulliTriangle(6)
	/*
	    n = 10
	    ----------
	    1
	    1  2
	    1  3  4
	    1  4  7  8
	    1  5  11  15  16
	    1  6  16  26  31  32
	    1  7  22  42  57  63  64
	    1  8  29  64  99  120  127  128
	    1  9  37  93  163  219  247  255  256
	    1  10  46  130  256  382  466  502  511  512
	*/
	bernoulliTriangle(10)
}

Output

 Given n : 6
 1
 1 2
 1 3 4
 1 4 7 8
 1 5 11 15 16
 1 6 16 26 31 32

 Given n : 10
 1
 1 2
 1 3 4
 1 4 7 8
 1 5 11 15 16
 1 6 16 26 31 32
 1 7 22 42 57 63 64
 1 8 29 64 99 120 127 128
 1 9 37 93 163 219 247 255 256
 1 10 46 130 256 382 466 502 511 512
<?php
// Php program for
// Bernoulli's triangle
class Triangle
{
	public	function bernoulliTriangle($n)
	{
		if ($n <= 0)
		{
			return;
		}
		// This problem are need N X N space
		// But we are solve this problem using using only two rows
		// Here 2 indicates row
		// And n+1 indicate column value
		$dp = array_fill(0, 2, array_fill(0, $n + 1, 0));
		// Set starting column value
		$dp[0][0] = 1;
		$dp[1][0] = 1;
		// We can solve this problem only use two rows
		// So initial select second row which position is 1
		$row = 1;
		echo("\n Given n : ".$n." \n");
		for ($i = 1; $i <= $n; ++$i)
		{
			// Set intial value of column
			$dp[0][$i] = 0;
			$dp[1][$i] = 0;
			// This loop are used to print result in current row
			for ($j = 0; $j < $i; ++$j)
			{
				if ($j > 0)
				{
					if ($j + 1 == $i)
					{
						// Last element of current row
						$dp[$row][$j] = $dp[$row][$j - 1] + 1;
					}
					else if ($row == 1)
					{
						// Change second row
						$dp[$row][$j] = $dp[0][$j] + $dp[0][$j - 1];
					}
					else
					{
						// Change first row
						$dp[$row][$j] = $dp[1][$j] + $dp[1][$j - 1];
					}
				}
				// Display the value
				echo(" ".$dp[$row][$j]);
			}
			echo("\n");
			// Change row
			if ($row == 1)
			{
				$row = 0;
			}
			else
			{
				$row = 1;
			}
		}
	}
}

function main()
{
	$task = new Triangle();
	/*
	    n = 6
	    --------- 
	    1
	    1  2
	    1  3  4
	    1  4  7  8
	    1  5  11  15  16
	    1  6  16  26  31  32
	*/
	$task->bernoulliTriangle(6);
	/*
	    n = 10
	    ----------
	    1
	    1  2
	    1  3  4
	    1  4  7  8
	    1  5  11  15  16
	    1  6  16  26  31  32
	    1  7  22  42  57  63  64
	    1  8  29  64  99  120  127  128
	    1  9  37  93  163  219  247  255  256
	    1  10  46  130  256  382  466  502  511  512
	*/
	$task->bernoulliTriangle(10);
}
main();

Output

 Given n : 6
 1
 1 2
 1 3 4
 1 4 7 8
 1 5 11 15 16
 1 6 16 26 31 32

 Given n : 10
 1
 1 2
 1 3 4
 1 4 7 8
 1 5 11 15 16
 1 6 16 26 31 32
 1 7 22 42 57 63 64
 1 8 29 64 99 120 127 128
 1 9 37 93 163 219 247 255 256
 1 10 46 130 256 382 466 502 511 512
// Node JS program for
// Bernoulli's triangle
class Triangle
{
	bernoulliTriangle(n)
	{
		if (n <= 0)
		{
			return;
		}
		// This problem are need N X N space
		// But we are solve this problem using using only two rows
		// Here 2 indicates row
		// And n+1 indicate column value
      
		var dp = Array(2).fill(0).map(() => new Array(n + 1).fill(0));
		// Set starting column value
		dp[0][0] = 1;
		dp[1][0] = 1;
		// We can solve this problem only use two rows
		// So initial select second row which position is 1
		var row = 1;
		process.stdout.write("\n Given n : " + n + " \n");
		for (var i = 1; i <= n; ++i)
		{
			// Set intial value of column
			dp[0][i] = 0;
			dp[1][i] = 0;
			// This loop are used to print result in current row
			for (var j = 0; j < i; ++j)
			{
				if (j > 0)
				{
					if (j + 1 == i)
					{
						// Last element of current row
						dp[row][j] = dp[row][j - 1] + 1;
					}
					else if (row == 1)
					{
						// Change second row
						dp[row][j] = dp[0][j] + dp[0][j - 1];
					}
					else
					{
						// Change first row
						dp[row][j] = dp[1][j] + dp[1][j - 1];
					}
				}
				// Display the value
				process.stdout.write(" " + dp[row][j]);
			}
			process.stdout.write("\n");
			// Change row
			if (row == 1)
			{
				row = 0;
			}
			else
			{
				row = 1;
			}
		}
	}
}

function main()
{
	var task = new Triangle();
	/*
	    n = 6
	    --------- 
	    1
	    1  2
	    1  3  4
	    1  4  7  8
	    1  5  11  15  16
	    1  6  16  26  31  32
	*/
	task.bernoulliTriangle(6);
	/*
	    n = 10
	    ----------
	    1
	    1  2
	    1  3  4
	    1  4  7  8
	    1  5  11  15  16
	    1  6  16  26  31  32
	    1  7  22  42  57  63  64
	    1  8  29  64  99  120  127  128
	    1  9  37  93  163  219  247  255  256
	    1  10  46  130  256  382  466  502  511  512
	*/
	task.bernoulliTriangle(10);
}
main();

Output

 Given n : 6
 1
 1 2
 1 3 4
 1 4 7 8
 1 5 11 15 16
 1 6 16 26 31 32

 Given n : 10
 1
 1 2
 1 3 4
 1 4 7 8
 1 5 11 15 16
 1 6 16 26 31 32
 1 7 22 42 57 63 64
 1 8 29 64 99 120 127 128
 1 9 37 93 163 219 247 255 256
 1 10 46 130 256 382 466 502 511 512
#  Python 3 program for
#  Bernoulli's triangle
class Triangle :
	def bernoulliTriangle(self, n) :
		if (n <= 0) :
			return
		
		#  This problem are need N X N space
		#  But we are solve this problem using using only two rows
		#  Here 2 indicates row
		#  And n+1 indicate column value
		dp = [[0] * (n + 1) for _ in range(2) ]
		#  Set starting column value
		dp[0][0] = 1
		dp[1][0] = 1
		#  We can solve this problem only use two rows
		#  So initial select second row which position is 1
		row = 1
		print("\n Given n : ", n ," ")
		i = 1
		while (i <= n) :
			#  Set intial value of column
			dp[0][i] = 0
			dp[1][i] = 0
			j = 0
			#  This loop are used to print result in current row
			while (j < i) :
				if (j > 0) :
					if (j + 1 == i) :
						#  Last element of current row
						dp[row][j] = dp[row][j - 1] + 1
					elif (row == 1) :
						#  Change second row
						dp[row][j] = dp[0][j] + dp[0][j - 1]
					else :
						#  Change first row
						dp[row][j] = dp[1][j] + dp[1][j - 1]
					
				
				#  Display the value
				print(" ", dp[row][j], end = "")
				j += 1
			
			print(end = "\n")
			#  Change row
			if (row == 1) :
				row = 0
			else :
				row = 1
			
			i += 1
		
	

def main() :
	task = Triangle()
	#    n = 6
	#    --------- 
	#    1
	#    1  2
	#    1  3  4
	#    1  4  7  8
	#    1  5  11  15  16
	#    1  6  16  26  31  32
	task.bernoulliTriangle(6)
	#    n = 10
	#    ----------
	#    1
	#    1  2
	#    1  3  4
	#    1  4  7  8
	#    1  5  11  15  16
	#    1  6  16  26  31  32
	#    1  7  22  42  57  63  64
	#    1  8  29  64  99  120  127  128
	#    1  9  37  93  163  219  247  255  256
	#    1  10  46  130  256  382  466  502  511  512
	task.bernoulliTriangle(10)

if __name__ == "__main__": main()

Output

 Given n :  6
  1
  1  2
  1  3  4
  1  4  7  8
  1  5  11  15  16
  1  6  16  26  31  32

 Given n :  10
  1
  1  2
  1  3  4
  1  4  7  8
  1  5  11  15  16
  1  6  16  26  31  32
  1  7  22  42  57  63  64
  1  8  29  64  99  120  127  128
  1  9  37  93  163  219  247  255  256
  1  10  46  130  256  382  466  502  511  512
#  Ruby program for
#  Bernoulli's triangle
class Triangle 
	def bernoulliTriangle(n) 
		if (n <= 0) 
			return
		end

		#  This problem are need N X N space
		#  But we are solve this problem using using only two rows
		#  Here 2 indicates row
		#  And n+1 indicate column value
		dp = Array.new(2) {Array.new(n + 1) {0}}
		#  Set starting column value
		dp[0][0] = 1
		dp[1][0] = 1
		#  We can solve this problem only use two rows
		#  So initial select second row which position is 1
		row = 1
		print("\n Given n : ", n ," \n")
		i = 1
		while (i <= n) 
			#  Set intial value of column
			dp[0][i] = 0
			dp[1][i] = 0
			j = 0
			#  This loop are used to print result in current row
			while (j < i) 
				if (j > 0) 
					if (j + 1 == i) 
						#  Last element of current row
						dp[row][j] = dp[row][j - 1] + 1
					elsif (row == 1) 
						#  Change second row
						dp[row][j] = dp[0][j] + dp[0][j - 1]
					else
 
						#  Change first row
						dp[row][j] = dp[1][j] + dp[1][j - 1]
					end

				end

				#  Display the value
				print(" ", dp[row][j])
				j += 1
			end

			print("\n")
			#  Change row
			if (row == 1) 
				row = 0
			else
 
				row = 1
			end

			i += 1
		end

	end

end

def main() 
	task = Triangle.new()
	#    n = 6
	#    --------- 
	#    1
	#    1  2
	#    1  3  4
	#    1  4  7  8
	#    1  5  11  15  16
	#    1  6  16  26  31  32
	task.bernoulliTriangle(6)
	#    n = 10
	#    ----------
	#    1
	#    1  2
	#    1  3  4
	#    1  4  7  8
	#    1  5  11  15  16
	#    1  6  16  26  31  32
	#    1  7  22  42  57  63  64
	#    1  8  29  64  99  120  127  128
	#    1  9  37  93  163  219  247  255  256
	#    1  10  46  130  256  382  466  502  511  512
	task.bernoulliTriangle(10)
end

main()

Output

 Given n : 6 
 1
 1 2
 1 3 4
 1 4 7 8
 1 5 11 15 16
 1 6 16 26 31 32

 Given n : 10 
 1
 1 2
 1 3 4
 1 4 7 8
 1 5 11 15 16
 1 6 16 26 31 32
 1 7 22 42 57 63 64
 1 8 29 64 99 120 127 128
 1 9 37 93 163 219 247 255 256
 1 10 46 130 256 382 466 502 511 512
// Scala program for
// Bernoulli's triangle
class Triangle()
{
	def bernoulliTriangle(n: Int): Unit = {
		if (n <= 0)
		{
			return;
		}
		// This problem are need N X N space
		// But we are solve this problem using using only two rows
		// Here 2 indicates row
		// And n+1 indicate column value
		var dp: Array[Array[Int]] = Array.fill[Int](2, n + 1)(0);
		// Set starting column value
		dp(0)(0) = 1;
		dp(1)(0) = 1;
		// We can solve this problem only use two rows
		// So initial select second row which position is 1
		var row: Int = 1;
		print("\n Given n : " + n + " \n");
		var i: Int = 1;
		while (i <= n)
		{
			var j: Int = 0;
			// This loop are used to print result in current row
			while (j < i)
			{
				if (j > 0)
				{
					if (j + 1 == i)
					{
						// Last element of current row
						dp(row)(j) = dp(row)(j - 1) + 1;
					}
					else if (row == 1)
					{
						// Change second row
						dp(row)(j) = dp(0)(j) + dp(0)(j - 1);
					}
					else
					{
						// Change first row
						dp(row)(j) = dp(1)(j) + dp(1)(j - 1);
					}
				}
				// Display the value
				print(" " + dp(row)(j));
				j += 1;
			}
			print("\n");
			// Change row
			if (row == 1)
			{
				row = 0;
			}
			else
			{
				row = 1;
			}
			i += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Triangle = new Triangle();
		/*
		    n = 6
		    --------- 
		    1
		    1  2
		    1  3  4
		    1  4  7  8
		    1  5  11  15  16
		    1  6  16  26  31  32
		*/
		task.bernoulliTriangle(6);
		/*
		    n = 10
		    ----------
		    1
		    1  2
		    1  3  4
		    1  4  7  8
		    1  5  11  15  16
		    1  6  16  26  31  32
		    1  7  22  42  57  63  64
		    1  8  29  64  99  120  127  128
		    1  9  37  93  163  219  247  255  256
		    1  10  46  130  256  382  466  502  511  512
		*/
		task.bernoulliTriangle(10);
	}
}

Output

 Given n : 6
 1
 1 2
 1 3 4
 1 4 7 8
 1 5 11 15 16
 1 6 16 26 31 32

 Given n : 10
 1
 1 2
 1 3 4
 1 4 7 8
 1 5 11 15 16
 1 6 16 26 31 32
 1 7 22 42 57 63 64
 1 8 29 64 99 120 127 128
 1 9 37 93 163 219 247 255 256
 1 10 46 130 256 382 466 502 511 512
// Swift 4 program for
// Bernoulli's triangle
class Triangle
{
	func bernoulliTriangle(_ n: Int)
	{
		if (n <= 0)
		{
			return;
		}
		// This problem are need N X N space
		// But we are solve this problem using using only two rows
		// Here 2 indicates row
		// And n+1 indicate column value
		var dp: [[Int]] = 
          Array(repeating: Array(repeating: 0, 
                count: n + 1), count: 2);
		// Set starting column value
		dp[0][0] = 1;
		dp[1][0] = 1;
		// We can solve this problem only use two rows
		// So initial select second row which position is 1
		var row: Int = 1;
		print("\n Given n : ", n ," ");
		var i: Int = 1;
		while (i <= n)
		{
			var j: Int = 0;
			// This loop are used to print result in current row
			while (j < i)
			{
				if (j > 0)
				{
					if (j + 1 == i)
					{
						// Last element of current row
						dp[row][j] = dp[row][j - 1] + 1;
					}
					else if (row == 1)
					{
						// Change second row
						dp[row][j] = dp[0][j] + dp[0][j - 1];
					}
					else
					{
						// Change first row
						dp[row][j] = dp[1][j] + dp[1][j - 1];
					}
				}
				// Display the value
				print(" ", dp[row][j], terminator: "");
				j += 1;
			}
			print(terminator: "\n");
			// Change row
			if (row == 1)
			{
				row = 0;
			}
			else
			{
				row = 1;
			}
			i += 1;
		}
	}
}
func main()
{
	let task: Triangle = Triangle();
	/*
	    n = 6
	    --------- 
	    1
	    1  2
	    1  3  4
	    1  4  7  8
	    1  5  11  15  16
	    1  6  16  26  31  32
	*/
	task.bernoulliTriangle(6);
	/*
	    n = 10
	    ----------
	    1
	    1  2
	    1  3  4
	    1  4  7  8
	    1  5  11  15  16
	    1  6  16  26  31  32
	    1  7  22  42  57  63  64
	    1  8  29  64  99  120  127  128
	    1  9  37  93  163  219  247  255  256
	    1  10  46  130  256  382  466  502  511  512
	*/
	task.bernoulliTriangle(10);
}
main();

Output

 Given n :  6
  1
  1  2
  1  3  4
  1  4  7  8
  1  5  11  15  16
  1  6  16  26  31  32

 Given n :  10
  1
  1  2
  1  3  4
  1  4  7  8
  1  5  11  15  16
  1  6  16  26  31  32
  1  7  22  42  57  63  64
  1  8  29  64  99  120  127  128
  1  9  37  93  163  219  247  255  256
  1  10  46  130  256  382  466  502  511  512
// Kotlin program for
// Bernoulli's triangle
class Triangle
{
	fun bernoulliTriangle(n: Int): Unit
	{
		if (n <= 0)
		{
			return;
		}
		// This problem are need N X N space
		// But we are solve this problem using using only two rows
		// Here 2 indicates row
		// And n+1 indicate column value
		var dp: Array < Array < Int >> = Array(2)
		{
			Array(n + 1)
			{
				0
			}
		};
		// Set starting column value
		dp[0][0] = 1;
		dp[1][0] = 1;
		// We can solve this problem only use two rows
		// So initial select second row which position is 1
		var row: Int = 1;
		print("\n Given n : " + n + " \n");
		var i: Int = 1;
		while (i <= n)
		{
			var j: Int = 0;
			// This loop are used to print result in current row
			while (j < i)
			{
				if (j > 0)
				{
					if (j + 1 == i)
					{
						// Last element of current row
						dp[row][j] = dp[row][j - 1] + 1;
					}
					else if (row == 1)
					{
						// Change second row
						dp[row][j] = dp[0][j] + dp[0][j - 1];
					}
					else
					{
						// Change first row
						dp[row][j] = dp[1][j] + dp[1][j - 1];
					}
				}
				// Display the value
				print(" " + dp[row][j]);
				j += 1;
			}
			print("\n");
			// Change row
			if (row == 1)
			{
				row = 0;
			}
			else
			{
				row = 1;
			}
			i += 1;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Triangle = Triangle();
	/*
	    n = 6
	    --------- 
	    1
	    1  2
	    1  3  4
	    1  4  7  8
	    1  5  11  15  16
	    1  6  16  26  31  32
	*/
	task.bernoulliTriangle(6);
	/*
	    n = 10
	    ----------
	    1
	    1  2
	    1  3  4
	    1  4  7  8
	    1  5  11  15  16
	    1  6  16  26  31  32
	    1  7  22  42  57  63  64
	    1  8  29  64  99  120  127  128
	    1  9  37  93  163  219  247  255  256
	    1  10  46  130  256  382  466  502  511  512
	*/
	task.bernoulliTriangle(10);
}

Output

 Given n : 6
 1
 1 2
 1 3 4
 1 4 7 8
 1 5 11 15 16
 1 6 16 26 31 32

 Given n : 10
 1
 1 2
 1 3 4
 1 4 7 8
 1 5 11 15 16
 1 6 16 26 31 32
 1 7 22 42 57 63 64
 1 8 29 64 99 120 127 128
 1 9 37 93 163 219 247 255 256
 1 10 46 130 256 382 466 502 511 512

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