Posted on by Kalkicode
Code Pattern

Program for pascals triangle in c

Pascal triangle

C Program for pascals triangle. Here more solutions.

// Include header file
#include <stdio.h>

/*
  C program for printing Pascal's triangle patterns
*/
// Printing Pascal's triangle patterns by number of rows
void pascalTriangle(int row)
{
	// Loop controlling variables
	int i = 0;
	int j = 0;
	int 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
			printf("\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 = k * (i - j + 1) / j;
			}
			// Display the value of calculate number
			printf("\t%d\t", k);
		}
		// Include new line
		printf("\n");
	}
	// Include new line
	printf("\n");
}
int main()
{
	// Test
	// When Row : 6
	pascalTriangle(6);
	// When Row : 10
	pascalTriangle(10);
	return 0;
}

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