Array

C++ Program to generate pascal’s triangle using Array

C++ Program to generate pascal’s triangle using Array

In this tutorial, we will discuss the C++ Program to generate a pascal’s triangle using the Array

In this post, we are going to learn how to display the pascal triangle number pattern in C++ language using a single dim array with for, while, and do-while loop

Pascal triangle

Program to generate pascal’s 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 show the pascal triangle number pattern using for loop in the C++ language

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int arr[5],arrTemp[5],i=1,j=0,colNum,rowNum;
    //declare and initialize variables
    arr[0]=1;
    arr[1]=1;
//initialize array elements
    for(rowNum=0; rowNum<5; rowNum++)
        {
    for(colNum=4; colNum>rowNum; colNum--){
        cout<<" ";}//print space
             for(colNum=0; colNum<=rowNum; colNum++){
                 if(rowNum==0)
                    cout<<"1";//i the row is zero print "1"
                 else
                 {
                     if(colNum==0 || colNum==rowNum){
                        cout<<"1 ";}
                     else{
                        arrTemp[i]=arr[j]+arr[j+1];
                        cout<<arrTemp[i]<<" ";
                        //print number with space
                        i++;
                        j++;
                     }
                     }
                 }
cout<<endl;
 arrTemp[i]=1;
 if(rowNum>1)
    {
        j=0;
         arr[j]=1;
         for(j=1,i=1; j<=rowNum; 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

Output 1

 

Program to display pascal triangle Using while loop

Program 1

in this program, the user declares and initializes some variables as integers and then the program will show the pascal triangle number pattern using the While loop in the C++ language

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int arr[5],arrTemp[5],i=1,j=0,colNum,rowNum;
    //declare and initialize variables
    arr[0]=1;
    arr[1]=1;
//initialize array elements
    for(rowNum=0; rowNum<5; rowNum++)
        {
    for(colNum=4; colNum>rowNum; colNum--){
        cout<<" ";}//print space
             for(colNum=0; colNum<=rowNum; colNum++){
                 if(rowNum==0)
                    cout<<"1";//i the row is zero print "1"
                 else
                 {
                     if(colNum==0 || colNum==rowNum){
                        cout<<"1 ";}
                     else{
                        arrTemp[i]=arr[j]+arr[j+1];
                        cout<<arrTemp[i]<<" ";
                        //print number with space
                        i++;
                        j++;
                     }
                     }
                 }
cout<<endl;
 arrTemp[i]=1;
 if(rowNum>1)
    {
        j=0;
         arr[j]=1;
         for(j=1,i=1; j<=rowNum; 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

Program to generate pascal triangle in C++ using Array

 

Program to display pascal triangle Using do-while 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 the do-while loop in the C++ language

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int arr[5],arrTemp[5],i=1,j=0,colNum,rowNum;
    //declare and initialize variables
    arr[0]=1;
    arr[1]=1;
//initialize array elements
rowNum=0;
        do{
            colNum=4;
    do{
        cout<<" ";//print space
         colNum--;
         }while(colNum>=rowNum);
         //print space
         colNum=0;
             do{
                 if(rowNum==0)
                    cout<<"1";// the row is zero print "1"
                 else
                 {
                     if(colNum==0 || colNum==rowNum){
                        cout<<"1 ";}
                     else{
                        arrTemp[i]=arr[j]+arr[j+1];
                        cout<<arrTemp[i]<<" ";
                        //print number with space
                        i++;
                        j++;
                     }
                     }
                      colNum++;
                 }while(colNum<=rowNum);
cout<<endl;
 arrTemp[i]=1;
 if(rowNum>1)
    {
        j=0;
         arr[j]=1;
         for(j=1,i=1; j<=rowNum; j++, i++)
            arr[j]=arrTemp[i];
         i=1;
         j=0;
    }
    rowNum++;
    }  while(rowNum<5);
    cout<<endl;
    getch();
 return 0;
}


When the above code is executed, it produces the following result

Output 3

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

Java program to triangle number pattern using while loop

Java program to pyramid triangle star pattern

 

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

4 weeks ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

4 weeks ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.