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

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.