Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
C Sharp Exercise: print Pascal triangle pattern

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

Posted on October 3, 2022

Table of Contents

  • C++ Program to generate pascal’s triangle using Array
    • Program to generate pascal’s triangle
      • Program to display pascal triangle Using for loop
      • Program to display pascal triangle Using while loop
      • Program to display pascal triangle Using do-while loop

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

Java program to print pascal triangle
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

Program to generate pascal triangle in C++ using Array
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 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

Program to generate pascal triangle in C++ using Array
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

 

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes