C PROGRAMMING PROGRAM • PATTERN PROGRAMS
Print Pascal's Triangle
Learn how to print pascal's triangle using a clear C program.
PROBLEM UNDERSTANDING
Input and expected output
Sample input
5
Sample output
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
COMPLETE C PROGRAM
Complete C implementation
#include <stdio.h>
int main(void)
{
int rows;
printf("Enter number of rows: ");
scanf("%d", &rows);
for (int row = 0; row < rows; row++) {
unsigned long long value = 1;
for (int space = 0; space < rows - row - 1; space++) printf(" ");
for (int column = 0; column <= row; column++) {
printf("%llu ", value);
value = value * (unsigned long long) (row - column) / (unsigned long long) (column + 1);
}
printf("\n");
}
return 0;
}CURRENT STEP
SELECTED LINE
EXPECTED OUTPUT FOR THE SAMPLE
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
Step 0 of 0
PROGRAM EXPLANATION
Algorithm and explanation
- Read the required input values.
- Generate every binomial coefficient from the previous coefficient in the same row.
- Display the computed result.
Generate every binomial coefficient from the previous coefficient in the same row.
EFFICIENCY
Time and space complexity
Time complexity
O(n²)
Auxiliary space
O(1)
DEBUGGING CHECKLIST
Common mistakes
Check this
Use the correct format specifier for every variable.
Check this
Initialize variables before using their values.
Check this
Check braces, semicolons and input order carefully.
Try it yourself
Practice: Run the program with the sample input, predict its output, and then test one boundary case of your own.
