Table of Contents
C program to generate pascal triangle using Array
In this tutorial, we will discuss the C program to generate a pascal triangle using an Array
In this post, we are going to learn how to display the pascal triangle pattern 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
in this program, the user declares and initializes some variables as integers and then the program will display the pascal triangle number pattern using for loop in C language
#include <stdio.h> #include <stdlib.h> int main() { int arr[30],arrTemp[30]; //declare two one dim arrays int i,j,k,l, rowNo=6; //declare and initialize arrTemp[0]=1; arr[0]=1; //initialize array elements for(j=0; j<rowNo; j++) printf(" "); printf(" 1\n"); for(i=1; i<rowNo; i++){ for(j=0; j<i; j++) printf(" "); for(k=1; k<=rowNo; 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
Program to display pascal triangle Using for loop with user input
Program 2
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 arrOne[30],arrTemp[30];//Declare single dim arrays int i,j,k,l, rowNum;//declare variables printf("Enter the Number o rows : "); //ask input scanf("%d",&rowNum); //Reading the input arrTemp[0]=1;//initialize Single dim temp array index 0 arrOne[0]=1;//initialize Single dim array index 0 for(j=0; j<rowNum; j++) printf(" ");//print space printf(" 1\n"); for(i=1; i<rowNum; i++){ for(j=0; j<i; j++) printf(" "); //print space for(k=1; k<=rowNum; k++){ arrOne[k]=arrTemp[k-1]+arrTemp[k]; }//print numbers arrOne[i]=1; for(l=0; l<=i; l++){ printf("%3d",arrOne[l]); arrTemp[l]=arrOne[l]; } printf("\n"); //move to next line } getch(); return 0; }
When the above code is executed, it produces the following result
Enter the Number o rows :3
Program to display pascal triangle Using while loop
Program 3
in this program , the user declares and initializes some variables as integer and then the program will display the pascal triangle number pattern using while loop in C language
#include <stdio.h> #include <stdlib.h> int main() { int arr[30],arrTemp[30]; //declare two one dim arrays int i,j,k,l, numRow=4; //declare and initialize //printf("Enter the number of rows: "); //scanf("%d",&num_Row); arrTemp[0]=1; arr[0]=1; //initialize array elements j=0; while(j<numRow){ printf(" "); j++; } printf(" 1\n"); i=1; while(i<numRow){ j=0; while(j<i){ printf(" "); j++; } k=1; while(k<=numRow){ 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
Program to display pascal triangle Using while loop with user input
Program 4
This program allows the user to enter the number of rows and it will display the pascal triangle number pattern using while loop in C language according to the rows
#include <stdio.h> #include <stdlib.h> int main() { int arr[30],arrTemp[30]; //declare two one dim arrays int i,j,k,l, numRow; //declare and initialize printf("Enter the value for the number of rows: "); scanf("%d",&numRow); arrTemp[0]=1; arr[0]=1; //initialize array elements j=0; while(j<numRow){ printf(" "); j++; } printf(" 1\n"); i=1; while(i<numRow){ j=0; while(j<i){ printf(" "); j++; } k=1; while(k<=numRow){ 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
Enter the value for the number of rows: 5
Program to display pascal triangle Using do-while loop
Program 5
in this program , the user declares and initializes some variables as integers and then the program will display the pascal triangle number pattern using do-while loop in Java language
#include <stdio.h> #include <stdlib.h> int main() { int arr[30],arrTemp[30]; //declare two one dim arrays int i,j,k,l, rowNum=5; //declare and initialize variables arrTemp[0]=1; arr[0]=1; //initialize elements o an array to 0 index j=0; do{ printf(" "); j++; } while(j<rowNum); printf(" 1\n"); i=1; do{ for(j=0; j<i; j++) printf(" "); k=1; while(k<=rowNum){ 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<rowNum); getch(); return 0; }
When the above code is executed, it produces the following result
Program to display pascal triangle Using do-while loop with user input
Program 6
This program allows the user to enter the number of rows, and it will display the pascal triangle number pattern using do-while loop in C language according to the rows
#include <stdio.h> #include <stdlib.h> int main() { int arr[30],arrTemp[30]; //declare two one dim arrays int i,j,k,l, rowNo; //declare and variables printf("Enter the value for the number of rows: "); //ask input from user scanf("%d",&rowNo); //read the input arrTemp[0]=1; arr[0]=1; //initialize array elements as 1 j=0; do{ printf(" "); j++; } while(j<rowNo); printf(" 1\n"); i=1; do{//outter do-while 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{//inner do-while 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
Enter the value for the number of rows: 5
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 triangle number pattern