Here, we will create a two-dimensional array and read the total number of lines to be printed and print the Pascal triangle on the console screen.
Program:
The source code to generate a pascal triangle using an array is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.
// C program to generate pascal triangle using array
#include <stdio.h>
int main()
{
int arr[50][50];
int i = 0;
int j = 0;
int n = 0;
printf("Enter the number of lines: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
for (j = 0; j < n - 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");
}
return 0;
}
Here, we will create a two-dimensional array and read the total number of lines to be printed and print the Pascal triangle on the console screen.
Program:
The source code to generate a pascal triangle using an array is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.
Output:
Explanation:
Here, we created a 2D array arr and read the size from the user, and printed the Pascal triangle on the console screen.
need an explanation for this answer? contact us directly to get an explanation for this answer