Skip to main content

Program for pascals triangle in node js

Pascal triangle

Js Program for pascals triangle. Here mentioned other language solution.

/*
  Node JS program for printing Pascal's triangle patterns
*/
// Printing Pascal's triangle patterns by number of rows
function pascalTriangle(row)
{
	// Loop controlling variables
	var i = 0;
	var j = 0;
	var k = 0;
	// Loop from 0..n
	for (i = 0; i < row; i++)
	{
		// Print initial white space
		for (j = i + 1; j < row; j++)
		{
			// include space
			process.stdout.write("\t");
		}
		// Print resultant value
		for (j = 0; j <= i; j++)
		{
			if (j == 0 || i == 0)
			{
				// Set initial values
				k = 1;
			}
			else
			{
				// Pascal's triangle number calculation
				k = parseInt(k * (i - j + 1) / j);
			}
			// Display the value of calculate number
			process.stdout.write("\t" + k + "\t");
		}
		// Include new line
		console.log();
	}
	// Include new line
	console.log();
}

function main()
{
	// Test
	// When Row : 6
	pascalTriangle(6);
	// When Row : 10
	pascalTriangle(10);
}
// Start program execution
main();

Output

						1
					1		1
				1		2		1
			1		3		3		1
		1		4		6		4		1
	1		5		10		10		5		1

										1
									1		1
								1		2		1
							1		3		3		1
						1		4		6		4		1
					1		5		10		10		5		1
				1		6		15		20		15		6		1
			1		7		21		35		35		21		7		1
		1		8		28		56		70		56		28		8		1
	1		9		36		84		126		126		84		36		9		1




Comment

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