C Program to Print Pascal's Pattern Triangle Pyramid
C Program to Print Pascal's Pattern Triangle Pyramid
A school pattern exercise asks for pascal’s triangle. Describe what one row contains before putting that row inside the outer loop.
Pascal’s triangle begins and ends each row with one; an interior entry sums the two entries above it.
Before this exercise, review loop counters and stopping conditions. The worked program below applies those ideas to this specific problem.
Explain
Explain why pascal’s triangle uses this sequence of operations.
Trace
Trace the sample values and predict the printed result.
Apply
Print exactly two rows and predict both the characters and the leading spaces.
Pascal’s Triangle: follow the data
Given data
A school pattern exercise asks for pascal’s triangle. Describe what one row contains before putting that row inside the outer loop.
Rule
Pascal’s triangle begins and ends each row with one; an interior entry sums the two entries above it.
Observed result
1 1 1 1 2 1
Understand the operation
The multiplicative row formula starts from one and updates the next binomial coefficient using the row and column indexes.
The outer loop chooses a row; an inner loop chooses positions within that row. A newline belongs after the entire row, rather than after every printed character. Spaces are output data too, so count them explicitly.
Read the complete C program
The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.
C17 · complete program
#include <stdio.h>
int main(void) {
int rows = 3;
for (int row = 0; row < rows; row++) {
for (int s = 0; s < rows - row - 1; s++) putchar(' ');
unsigned long long value = 1;
for (int col = 0; col <= row; col++) {
if (col != 0) putchar(' ');
printf("%llu", value);
value = value * (unsigned)(row - col) / (unsigned)(col + 1);
}
putchar('\n');
}
return 0;
}
Expected output
1
1 1
1 2 1
Run in C compiler →Follow the values through the code
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
int rows = 3;
for (int row = 0; row < rows; row++) {
for (int s = 0; s < rows - row - 1; s++) putchar(' ');
unsigned long long value = 1;
for (int col = 0; col <= row; col++) {
if (col != 0) putchar(' ');
printf("%llu", value);
value = value * (unsigned)(row - col) / (unsigned)(col + 1);
}
putchar('\n');
}
return 0;
}
The sample requests 3 rows, with indexes zero through 2.
The multiplicative row formula starts from one and updates the next binomial coefficient using the row and column indexes.
Follow the worked calculation
Start with the sample
The sample requests 3 rows, with indexes zero through 2.
Apply the rule
The multiplicative row formula starts from one and updates the next binomial coefficient using the row and column indexes.
Check the result
1 1 1 1 2 1
1 1 1 1 2 1
Try a small variation
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
int rows = 3;
for (int row = 0; row < rows; row++) {
for (int s = 0; s < rows - row - 1; s++) putchar(' ');
unsigned long long value = 1;
for (int col = 0; col <= row; col++) {
if (col != 0) putchar(' ');
printf("%llu", value);
value = value * (unsigned)(row - col) / (unsigned)(col + 1);
}
putchar('\n');
}
return 0;
}
Print exactly two rows and predict both the characters and the leading spaces.
Worked answer
The two-row answer reuses the row rule with a smaller requested count. Read the monospaced output: a leading space changes alignment even though it does not look like a printed symbol.
C17 · complete program
#include <stdio.h>
int main(void) {
int rows = 2;
for (int row = 0; row < rows; row++) {
for (int s = 0; s < rows - row - 1; s++) putchar(' ');
unsigned long long value = 1;
for (int col = 0; col <= row; col++) {
if (col != 0) putchar(' ');
printf("%llu", value);
value = value * (unsigned)(row - col) / (unsigned)(col + 1);
}
putchar('\n');
}
return 0;
}
Expected output
1
1 1
Run in C compiler →Recognize the important boundaries
This fixture uses at most four rows. A general coefficient update can overflow its multiplication even when the final quotient fits; enforce bounds or use checked arithmetic.
Keep the newline after each completed row. Spaces position the characters; moving that newline inside the inner loop changes the shape into separate lines.
Check your understanding
Choose the answer, then compare the reasoning for every option.
Use the idea independently
Pascal’s triangle begins and ends each row with one; an interior entry sums the two entries above it.
Continue with nested loop control.
Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.
Reaching the recap and completing the quizzes are separate milestones.