Pascal triangle
Table of Contents
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
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 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 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
Multiply two numbers in Java using scanner| 5 different ways In this article, we will…
5 Different ways to Divide two numbers in Java using scanner In this article, we…
Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…
10 ways to subtract two numbers in Java In this article, we will discuss the…
Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…
How to Divide two numbers in Java| 5 different ways In this article, we will…
This website uses cookies.