- On
- By
- 1 Comment
- Categories: do-while, for loop, Loop, Number pattern, pyramid triangle, While loop
Pascal Triangle program in C++ language
Pascal Triangle program in C++ language
In this tutorial, we will discuss the concept of Pascal Triangle program in C++ language
In this topic, we are going to learn how to write a program to print Pascal triangle pattern using number in C++ programming language
Here, we use for, while and do-while loops for printing pascal triangle
Display pascal pattern in C++ using loops
C++ Code display triangle using for loop
In this program, the user is asked to enter number of rows and then it will display triangle number pattern using for loop in C++ language
Program 1
#include <iostream> #include <conio.h> using namespace std; int main() { int rows,i,j,space,counter=1; cout<<"Enter the No of rows: "; cin>>rows; //input rows for print pascal triangle for( i=0; i<rows; i++){//outer for loop to print no of rows for(space=1; space<=rows-i; space++ ){ cout<<" ";//inner for loop to print space } for(j=0; j<=i; j++){//inner for loop to print number if(j==0 || i==0) counter=1; else counter=counter*(i-j+1)/j; cout<<counter<<" "; } cout<<"\n"; } getch(); return 0; }
When the above code is executed, it produces the following result
C++ Code display triangle using while loop
In this program, the user is asked to enter the number of rows and then it will display a triangle number pattern using the while loop in the C++ language
Program 2
#include <iostream> #include <conio.h> using namespace std; int main() { int rows,i,j,space,counter=1; cout<<"Enter the No of rows: "; cin>>rows; //input number of rows for print pascal triangle i=0; while(i<rows){ space=1; while( space<=rows-i ){ cout<<" "; space++; } j=0; while(j<=i){ if(j==0 || i==0) counter=1; else counter=counter*(i-j+1)/j; cout<<counter<<" "; j++; } cout<<"\n"; i++; } getch(); return 0; }
When the above code is executed, it produces the following result
C++ Code displays triangle using a do-while loop
In this program, the user is asked to enter a number of rows and then it will display a triangle number pattern using the do-while loop in C++ language
Program 3
#include <iostream> #include <conio.h> using namespace std; int main() { int rows,i,j,space,counter=1; cout<<"Enter the No of rows: "; cin>>rows;//input number of rows for print pascal triangle i=0; do{//outer do while loop space=1; do{//inner do-while loop cout<<" "; space++; }while( space<=rows-i ); j=0; do{//inner do-while loop if(j==0 || i==0) counter=1; else counter=counter*(i-j+1)/j; cout<<counter<<" ";//print pascal triangle j++; } while(j<=i); cout<<"\n"; i++; }while(i<rows); getch(); return 0; }
When the above code is executed, it produces the following result
Suggested for you
Data type and variable in Java language
The operator in the Java language
Similar post
Java code to print pascal triangle
C code to print pascal’s triangle
C++ code to print pascal’s triangle
Java code to print pascal’s triangle using an array
Java program to print pascal’s triangle using array using user input
C program to print a pascal’s triangle using an array
C program to print pascal’s triangle using array using user input
Java program to pascal’s triangle number pattern using 2 D array
C program to pascal’s triangle number pattern using 2 D array
C code to Alphabet 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 number pattern in C language using while loop
Alphabet pattern in Java language
Alphabet pattern in Java language using while loop
Alphabet pattern in C++ language
Alphabet pattern in C++ language using while loop
1 Comment
aaa September 12, 2022 at 8:54 pm