- On
- By
- 0 Comment
- Categories: Array, Loop, Number pattern
C++ code to Generate Pascal triangle using 1 D array
C++ code to Generate Pascal triangle using 1 D array
In this tutorial, we will discuss the concept of the C++ code 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

Display the pascal triangle in C++ using loops
C++ Code to display pascal triangle using for loop
In this program, the user declares and initializes integer variables, it will show a pascal triangle number pattern using for loop in the C++ language according to the rows
Program 1
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int arr[5],arrTemp[5],i=1,j=0,cols,rows;
arr[0]=1;
arr[1]=1;
for(rows=0; rows<5; rows++)
{
for(cols=4; cols>rows; cols--){
cout<<" ";}
for(cols=0; cols<=rows; cols++){
if(rows==0)
cout<<"1";
else
{
if(cols==0 || cols==rows){
cout<<"1 ";}
else{
arrTemp[i]=arr[j]+arr[j+1];
cout<<arrTemp[i]<<" ";
i++;
j++;
}
}
}
cout<<endl;
arrTemp[i]=1;
if(rows>1)
{
j=0;
arr[j]=1;
for(j=1,i=1; j<=rows; j++, i++)
arr[j]=arrTemp[i];
i=1;
j=0;
}
}
cout<<endl;
getch();
return 0;
}
When the above code is executed, it produces the following result
C++ Code to display pascal triangle using while loop
In this program, the user declares and initializes integer variables, it will show a pascal triangle number pattern using the while loop in the C++ language according to the rows
Program 2
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int arr[5],arrTemp[5],i=1,j=0,cols,rows;
arr[0]=1;
arr[1]=1;
rows=0;
while(rows<5)
{
cols=4;
while( cols>rows){
cols--;
cout<<" ";}
cols=0;
while(cols<=rows){
if(rows==0)
cout<<"1";
else
{
if(cols==0 || cols==rows){
cout<<"1 ";}
else{
arrTemp[i]=arr[j]+arr[j+1];
cout<<arrTemp[i]<<" ";
i++;
j++;
}
}
cols++;
}
cout<<endl;
arrTemp[i]=1;
if(rows>1)
{
j=0;
arr[j]=1;
for(j=1,i=1; j<=rows; j++, i++)
arr[j]=arrTemp[i];
i=1;
j=0;
}
rows++;
}
cout<<endl;
getch();
return 0;
}
When the above code is executed, it produces the following result
C++ Code to display pascal triangle using do-while loop
In this program, the user declares and initializes integer variables, it will show a pascal triangle number pattern using the do-while loop in the C++ language according to the rows
Program 3
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int arr[5],arrTemp[5],i=1,j=0,cols,rows;
arr[0]=1;
arr[1]=1;
rows=0;
do{
cols=5;
do{
cols--;
cout<<" ";
}while( cols>rows);
cols=0;
do{
if(rows==0)
cout<<"1";
else
{
if(cols==0 || cols==rows){
cout<<"1 ";}
else{
arrTemp[i]=arr[j]+arr[j+1];
cout<<arrTemp[i]<<" ";
i++;
j++;
}
}
cols++;
}while(cols<=rows);
cout<<endl;
arrTemp[i]=1;
if(rows>1)
{
j=0;
arr[j]=1;
for(j=1,i=1; j<=rows; j++, i++)
arr[j]=arrTemp[i];
i=1;
j=0;
}
rows++;
}while(rows<5);
cout<<endl;
getch();
return 0;
}
When the above code is executed, it produces the following result
Suggested for you
The operator in the C++ language
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 with 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