- C Programming Examples
- C Programming Examples
- C Print Hello World
- C Get Input from User
- C Print Integer
- C Add Two Numbers
- C Check Even or Odd
- C Check Prime or Not
- C Check Alphabet or Not
- C Check Vowel or Not
- C Check Leap Year or Not
- C Check Reverse equal Original
- C Add Subtract Multiply Divide
- C Make Calculator
- C Add Digits of Number
- C Calculate Average, Percentage
- C Calculate Arithmetic Mean
- C Calculate Student Grade
- C Print Table of Number
- C Print Prime Numbers
- C Add n Numbers
- C Interchange Numbers
- C Reverse a Number
- C Swap Two Numbers
- C Count Positive Negative Zero
- C Find Largest of Two Numbers
- C Find Largest of Three Numbers
- C Find Factorial of Number
- C Find LCM & HCF
- C Find LCM of n Numbers
- C Find HCF of n Numbers
- C Area & Perimeter of Square
- C Area & Perimeter of Rectangle
- C Area & Circumference of Circle
- C Convert Fahrenheit to Celsius
- C Convert Celsius to Fahrenheit
- C Print ASCII Value
- C Print Fibonacci Series
- C Check Palindrome or Not
- C Check Armstrong or Not
- C Generate Armstrong Numbers
- C Find nCr and nPr
- C Decimal to Binary
- C Decimal to Octal
- C Decimal to Hexadecimal
- C Binary to Decimal
- C Binary to Octal
- C Binary to Hexadecimal
- C Octal to Decimal
- C Octal to Binary
- C Octal to Hexadecimal
- C Hexadecimal to Decimal
- C Hexadecimal to Binary
- C Hexadecimal to Octal
- C Pattern Printing Programs
- C Print Diamond Pattern
- C Print Floyd's Triangle
- C Print Pascal's Triangle
- C Print Smiling face
- C 1D Array Programs
- C Linear Search
- C Binary Search
- C Add Two Numbers using Pointer
- C Find Largest Element in Array
- C Find Smallest Element in Array
- C Reverse an Array
- C Insert Element in Array
- C Delete Element from Array
- C Merge Two Arrays
- C Bubble Sort
- C Selection Sort
- C Insertion Sort
- C 2D Array Programs
- C Add Two Matrices
- C Subtract Two Matrices
- C Transpose a Matrix
- C Multiply Two Matrices
- C 3D Array Programs
- C Print String
- C Find Length of String
- C Compare Two String
- C Copy a String
- C Concatenate String
- C Reverse a String
- C Delete Vowels from String
- C Delete Word from String
- C Find Frequency of Character
- C Count Word in String
- C Remove Spaces from String
- C Sort a String
- C Uppercase to Lowercase
- C Lowercase to Uppercase
- C Swap Two Strings
- C Check Anagram or Not
- C Generate Random Numbers
- C Read a File
- C Write Content to File
- C Read & Display File
- C Copy a File
- C Merge Two Files
- C List Files in Directory
- C Delete a File
- C Encrypt & Decrypt a File
- C Print Date
- C Get IP Address
- C Shutdown Computer
- C Check Palindrome String
- C Inches to Centimeters
- C Kilogram to Gram
- C Count Even Odd
- C Count Vowels Consonants
- C Find Profit Loss
- C Sum All Matrix Elements
- C First & Last Digit Sum
- C Largest Element in Matrix
- C Pass Array to Function
- C Address of Variable
- C Array Element at Even
- C Array Element at Odd
- C Print Even Array Elements
- C Print Odd Array Elements
- C Reverse File
- C Print Message with Time
- C Print Number in Words
- C Print Successive Character
- C Sum of their Square
- C Replace Vowel in String
- C Sort Words in Ascending
- C Sort Words in Descending
- C Sort String in Alphabetical
- C Sum of All Digit
- C Product of All Digit
- C Check Perfect Number
- C Smallest of Two Numbers
- C Smallest of Three Numbers
- C Count Character without Space
- C Count All Character in File
- C Calculate Basic, Gross Salary
- C Calculate Discount Purchase
- C Calculate Parcel Charge
- C Calculate Wage of Labor
- C Print Common Elements
- C Remove Extra Spaces
- C Round Number to Integer
- C Sum/Product of Even/Odd
- C Print Series upto n Term
- C Print Row Column Total
- C Print Second Largest/Smallest
- C Print Phone Bill
- C Print Total Digit in Number
- C Find Factors of Number
- C Programming Tutorial
- C Tutorial
- C Programming Library
- C Standard Library
- C Programming Test
- C Programming Test
- Give Online Test
- All Test List

« Previous Program Next Program »

In this article, you will learn and get code on printing of Pascal's triangle in C programming. But before going through the program, if you are not aware of Pascal's triangle, then I recommend you to refer the short description on Pascal's Triangle. There, in very short time, you will get everything that is required to create a program on it. But for now, the following picture tells everything about it.

This figure defines Pascal's triangle. But I've written the simplest algorithm to create a Pascal's triangle in that article.

Let's create a program to print Pascal's triangle without using any function and formula. This program only follows the algorithm to expand Pascal's triangle using loops and logic.

#include<stdio.h> #include<conio.h> int main() { int row, col, i=1, j=0, arr[5], arrTemp[5]; arr[0] = 1; arr[1] = 1; for(row=0; row<5; row++) { for(col=4; col>row; col--) printf(" "); for(col=0; col<=row; col++) { if(row==0) printf("1"); else { if(col==0 || col==row) printf("1 "); else { arrTemp[i] = arr[j]+arr[j+1]; printf("%d ", arrTemp[i]); i++; j++; } } } printf("\n"); arrTemp[i] = 1; if(row>1) { j=0; arr[j]=1; for(j=1, i=1; j<=row; j++, i++) arr[j] = arrTemp[i]; i=1; j=0; } } getch(); return 0; }

This program was build and run under **Code::Blocks** IDE. Here is its sample output.

In above program, we have used two arrays in a way that the first array say **arr[]**
holds the column's value of previous row, whereas the second array say **arrTemp[]**
holds the column's value of next row. That is,:

- Initially, 1 gets initialized to index no. 0 and 1 for first array say
**arr[]** - That is,
*arr[0]=1*and*arr[1]=1* - Now using this array, the columns value of next row gets calculated in a way that
the values at zeroth and first index gets added and initialized to first index of second array say
**arrTemp[]**

The dry run of above program goes like:

- Initially,
**i=1**,**j=0**,**arr[0]=1**,**arr[1]=1** - Now using for loop, 0 initialized to
*row*(now**row=0**) - Checks whether it is less than 5 or not. The condition evaluates to be true, therefore program flow goes
inside the loop. Inside it, using the first
*for*loop, 4 spaces gets printed - Now program flow goes to second
*for loop* - There 0 is initialized to
*col*(now**col=0**) - Checks whether
*col*is less than or equal to the value of*row*or not - The condition evaluates to be true, therefore program flow goes inside this loop. Using if statement,
checks whether the value of
*row*is equal to 0 or not - The condition evaluates to be true, therefore program flow goes inside
*if*block and prints 1 on output. The*else*block gets skipped - Now program flow goes to update part of inner first
*for loop*, increments the value of*col* - Now
**col=1** - Process step no.6 with new value of
*col* - The condition evaluates to be false
- Now the next statement is

`printf("\n");`

that tells the compiler to start next output things from next or new line - Now 1 is initialized to
*arrTemp[i]*. Because previous value of*i*was 1. Therefore**arrTemp[1] = 1** - An
*if*block is created to check whether the value of*row*is greater than 1 or not - The condition evaluates to be false, therefore
*if*block gets skipped. - And program flow goes to the update part of outer
*for*loop. The value of*row*gets incremented - Now
**row=1** - Process step no.3 to 5
- Process step no.6 with new value of
*row*(that is 1) - Now
**col=0**again - Read step no.7
- The condition evaluates to be false, because
*row*is not equal to 0 - Therefore program flow goes to
*else*block. Inside the*else*block, checks whether the value of*col*is equal to 0 or row's value or not - Because it's value is equal to 0, therefore 1 gets printed
- Increments the value of
*col*(now**col=1**) - And checks whether it is less than or equal to
*row*or not. Condition evaluates to be true, program flow again goes inside the loop - Again
*else*block gets executed - And again inside it, the condition of
*if*block evaluates to be true, because*col (1)*equals*row (1)*. Therefore 1 gets printed again - Again increments the value of
*col*(now**col=2**) and checks the condition. This time, condition evaluates to be false - Process step no.13 to 17
- Now
**row=2** - Process step no.3 to 5
- Process step no.6 with new value of
*row*(that is 2) - Now
**col=0**again - Process step no.7, 23, 24, 25, 26, 27, 28
- Now
**col=2** - This time, the condition of
*if*block evaluates to be false, therefore program flow goes to*else*block - There,
*arrTemp[i] = arr[j]+arr[j+1]*or*arrTemp[1] = arr[0]+arr[0+1]*or*arrTemp[1] = 1+1*or**arrTemp[1] = 2** - Now
**i=2**and**j=1** - Read step no.9
- Now
**col=3** - Process step no.6 with new value of
*col* - Process step no.12 to 15
- The condition evaluates to be true, therefore program flow goes inside the
*if*block - Now
**j=0**,*arr[j]=1*or**arr[0]=1** - The
*for loop*, gets executed. After successfully executing it - We will have,
**arr[0]=1**,**arr[1]=2**, arr[2]=1 - Now
**i=1**and**j=0** - Process step no.17
- Now
**row=3** - Process continue from step no.33 until the value of
*row*equals 5. That is the condition of outer*for loop*evaluates to be false

Now let's create another program that does the same job, but uses a formula to find the column value (one by one) and gets printed directly without so much logic as given in above program. But to improve programming skills, better to approach with previous one.

#include<stdio.h> #include<conio.h> long int fact(int); int main() { int i, c; for(i=0; i<5; i++) { for(c=4; c>i; c--) printf(" "); for(c=0; c<=i; c++) printf("%ld ", fact(i)/(fact(c)*fact(i-c))); printf("\n"); } getch(); return 0; } long int fact(int n) { int i, res=1; for(i=1; i<=n; i++) res = res*i; return res; }

It will produce the same output as of previous one. The formula is given in the separate tutorial on Pascal's triangle. That is, the column value of every row of a Pascal's triangle can be calculated as:

value = (row!)/((column!)*(row-columns)!)

where **row** is the row number, and **col** is the column number.

**Note** - Row and column both starts from 0.

The **!** represents factorial. To understand, how to find factorial of a number, then refer to
find factorial in C.

For example, the value at 2^{nd} column of 4^{th} row will be:

value = (row!)/((column!)*(row-columns)!) = (4!)/((2!)*(4-2)!) = (24)/(2*(2!)) = 24/(2*2) = 24/4 = 6

So 6 is the number present at fourth row and second column, that is actually at fifth row and third column.

This program asks from user to define the size of Pascal's triangle, that is upto how many rows, he/she wants to print Pascal's triangle:

#include<stdio.h> #include<conio.h> long int fact(int); int main() { int i, c, rowLimit; printf("Enter the Number of Rows: "); scanf("%d", &rowLimit); for(i=0; i<rowLimit; i++) { for(c=(rowLimit-1); c>i; c--) printf(" "); for(c=0; c<=i; c++) printf("%ld ", fact(i)/(fact(c)*fact(i-c))); printf("\n"); } getch(); return 0; } long int fact(int n) { int i, res=1; for(i=1; i<=n; i++) res = res*i; return res; }

Here is its sample run:

Now supply the number of rows, that is upto how many rows, Pascal's triangle to expand. Let's suppose user has
entered 8 as number of rows and pressed `ENTER`

key. Then here is the output produced by above program:

« Previous Program Next Program »

© Copyright 2021. All Rights Reserved.