In this tutorial, we will discuss the concept of the C program to Generate a Pascal triangle using a 1 D array
In this topic, we are going to learn how to write a program to print Pascal triangle number patterns using a single 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[30],arrTemp[30],i,j,k,l, rows=5; arrTemp[0]=1; arr[0]=1; for(j=0; j<rows; j++) printf(" "); printf(" 1\n"); for(i=1; i<rows; i++){ for(j=0; j<i; j++) printf(" "); for(k=1; k<=rows; k++){ arr[k]=arrTemp[k-1]+arrTemp[k]; } arr[i]=1; for(l=0; l<=i; l++){ printf("%3d",arr[l]); arrTemp[l]=arr[l]; } 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 while loop in the C language according to the rows
Program 1
#include <stdio.h> #include <stdlib.h> int main() { int arr[30],arrTemp[30]; //declare two one dim arrays int i,j,k,l, rowNo=4; //declare and initialize arrTemp[0]=1; arr[0]=1; //initialize array elements j=0; while(j<rowNo){ printf(" "); j++; } printf(" 1\n"); i=1; while(i<rowNo){ for(j=0; j<i; j++) printf(" "); k=1; while(k<=rowNo){ arr[k]=arrTemp[k-1]+arrTemp[k]; k++; } arr[i]=1; l=0; while(l<=i){ printf("%3d",arr[l]); arrTemp[l]=arr[l]; l++; } 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 display a pascal triangle number pattern using do-while loop in the C language according to the rows
Program 1
#include <stdio.h> #include <stdlib.h> int main() { int arr[30],arrTemp[30]; //declare two one dim arrays int i,j,k,l, rowNo=5; //declare and initialize arrTemp[0]=1; arr[0]=1; //initialize array elements j=0; do{ printf(" "); j++; } while(j<rowNo); printf(" 1\n"); i=1; do{ for(j=0; j<i; j++) printf(" "); k=1; while(k<=rowNo){ arr[k]=arrTemp[k-1]+arrTemp[k]; k++; } arr[i]=1; l=0; do{ printf("%3d",arr[l]); arrTemp[l]=arr[l]; l++; } while(l<=i); printf("\n"); i++; }while(i<rowNo); getch(); return 0; }
When the above code is executed, it produces the following result
Suggested for you
Data type and variable in C language
The operator in the C 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 array
Java program to print pascal triangle using array usin user input
C code to Alphabet triangle pattern using the do-while loop
C++ code to Alphabet triangle pattern using the do-while loop
Java code to Alphabet triangle pattern using the do-while loop
Alphabet pattern in C language
Alphabet triangle pattern in C language using while loop
Alphabet pattern in Java language
Alphabet triangle pattern in Java language using while loop
Alphabet pattern in C++ language
Program to display pascal triangle pattern in C language
Explanation of one dimensional array In this post, we will discuss the concept of "Explanation…
Python program to calculate the sum of odd and even numbers in a list In…
Python code to Calculate sum of odd and even in a list In this tutorial,…
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…