In this tutorial, we will discuss the concept of the C code to Generate Pascal’s triangle using a 2 D array
In this topic, we are going to learn how to write a program to print Pascal triangle number patterns using a two dimension Array in the C programming language
Here, we use for, while, and do-while loops for printing pascal triangle
In this program, the user declares and initializes integer variables, it will display a pascal triangle number pattern using for loop in the C language according to the rows
Program 1
#include <stdio.h> #include <stdlib.h> int main() { int arr[50][50]; int i=0,j=0,num=0; printf("Enter the number o rows: "); scanf("%d",&num); for(i=0; i<num; i++){ for(j=0; j<num-1-i; ++j) printf(" "); 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]); } printf("\n"); } getch(); return 0; }
When the above code is executed, it produces the following result
In this program, the user declares and initializes integer variables, it will display a pascal triangle number pattern using a while loop in the C language according to the rows
Program 1
#include <stdio.h> #include <stdlib.h> int main() { int arr[50][50]; int i=0,j=0,num=0; printf("Enter the number o rows: "); scanf("%d",&num); i=0; while(i<num){ j=0; while(j<num-1-i){ printf(" "); ++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
In this program, the user declares and initializes integer variables, it will show a pascal triangle number pattern using a do-while loop in the C language according to the rows
Program 3
#include <stdio.h> #include <stdlib.h> int main() { int arr[50][50]; int i=0,j=0,num=0; printf("Enter the number o rows: "); scanf("%d",&num); i=0; do{ j=0; do{ printf(" "); ++j; }while(j<=num-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<num); getch(); return 0; }
When the above code is executed, it produces the following result
Suggested for you
Data type and variable in Java language
The operator in the Java language
Similar post
Java program to print pascal triangle
C program to print pascal triangle
C++ program to print pascal triangle
Java program to print pascal triangle using an array
Java program to print pascal triangle using array using user input
C program to print a pascal triangle using an array
C program to print pascal triangle using array using user input
Java program to pascal triangle number pattern using 2 D array
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…