Table of Contents
C exercise: Pascal’s triangle pattern using 2D Array
In this tutorial, we will discuss the title of the C exercise: Pascal’s triangle using a 2D Array
In this post, we are going to learn how to display the pascal triangle pattern using a 2D array in C language using for, while, and do-while loop
C program to print pascal triangle
Program to display pascal triangle Using for loop
Program 1
This program allows the user to enter the number of rows, and it will display the pascal triangle number pattern using for loop in C language according to the rows
#include <stdio.h> #include <stdlib.h> int main() { int arr[50][50]; //declare 2D array int i=0,j=0,count=0; //declare and initialize variables printf("Enter the number for rows: "); //Ask input from the user scanf("%d",&count); //Reading input for(i=0; i<count; i++){ for(j=0; j<count-1-i; ++j) printf(" ");//print space for(j=0; j<=i; ++j){ if(j==0 || j==i) arr[i][j]=1; else arr[i][j]=arr[i-1][j-1]+arr[i-1][j]; printf("%d ", arr[i][j]); } //print number printf("\n"); //move to new line } getch(); return 0; }
When the above code is executed, it produces the following result
Program to display pascal triangle Using while loop
Program 2
This program allows the user to enter the number of rows, and it will display the pascal triangle number pattern using a while loop in C language according to the rows
#include <stdio.h> #include <stdlib.h> int main() { int arr[50][50]; //declare 2D array int i=0,j=0,count=0; //declare and initialize variables printf("Enter the number o rows: "); //Ask input from the user scanf("%d",&count); //Reading the input i=0; while(i<count){//outer while j=0; while(j<count-1-i){//inner while printf(" "); //print space ++j; } j=0; while(j<=i){ if(j==0 || j==i) arr[i][j]=1; else arr[i][j]=arr[i-1][j-1]+arr[i-1][j]; printf("%d ", arr[i][j]); ++j; } printf("\n"); i++; } getch(); return 0; }
When the above code is executed, it produces the following result
Program to display pascal triangle Using a do-while loop
Program 1
This program allows the user to enter the number of rows, and it will display the pascal triangle number pattern using a do-while loop in C language according to the rows
#include <stdio.h> #include <stdlib.h> int main() { int arr[50][50];//declare 2D array int i=0,j=0,count=0; //declare and initialize variables printf("Enter the number o rows: "); //Ask input from the user scanf("%d",&count); //Reading the input i=0; do{ j=0; do{ printf(" "); ++j; }while(j<=count-1-i); j=0; do{ if(j==0 || j==i) arr[i][j]=1; else arr[i][j]=arr[i-1][j-1]+arr[i-1][j]; printf("%d ", arr[i][j]); ++j; }while(j<=i); printf("\n"); i++; }while(i<count); getch(); return 0; }
When the above code is executed, it produces the following result
Suggested for you
For loop in C language while loop in C language Do while loop in C language
Nested for loop in C language Nested while loop in C language
if statement in C language Operator in C language
Similar post
C program to print pascal triangle
C++ program to print pascal triangle
Java program to print pascal triangle
C program to print pascal triangle using 1 D array
C++ program to print pascal triangle using 1 D array
Java program to print pascal triangle using 1 D array
Java program to print pascal triangle using 2 D array
Java program to triangle number pattern