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

Program for print pascal triangle in C++ language

Posted on December 6, 2019December 6, 2019

Program for print pascal triangle in C++ language

In this tutorial, we will discuss the concept of Program for print pascal triangle in C++ language.

In this post, we will learn how to display pascal triangle using for , while and dowhile loop in C+ language

Program for print pascal triangle in C++ language
Pascal triangle

Print pascal triangle using for loop

This program allows the user to enter the number of rows and it will display pascal triangle number pattern using for loop in C++ language

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

int main()
{
    int rows,i,j,space,counter=1;
 cout<<"Enter the row for pascal triangle: ";
 cin>>rows;//takes input from the user
 for(i=0; i<rows; i++){
   for(space=1; space<=rows-i; space++)
   cout<<" ";
//first inner loop print space

   for(j=0; j<=i; j++){
   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

Print pascal triangle using the while loop

This program allows the user to enter the number of rows and it will display pascal triangle number pattern using while loop in C++ language

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

int main()
{
    int rows,i,j,space,counter=1;
 cout<<"Enter the row for pascal triangle: ";
 cin>>rows;
 i=0;
 while(i<rows){
        space=1;
   while( space<=rows-i ){
   cout<<" ";  //first inner loop print space
   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

 

Print pascal triangle using the do-while loop

This program allows the user to enter the number of rows and it will display pascal triangle number pattern using do while loop in C++ language

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

int main()
{
    int rows,i,j,space,counter=1;
 cout<<"Enter the row for pascal triangle: ";
 cin>>rows;
 i=0;
do{
        space=1;
   do{
   cout<<" "; //first inner loop print space
   space++;
   }while( space<=rows-i );
j=0;
   do{
   if(j==0 || i==0)
    counter=1;
   else
     counter=counter*(i-j+1)/j;
     cout<<counter<<" ";
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

For loop in C++             while loop in C++            Do while loop in C++

if statement in C++         Operator in C++

 

Similar post

Java program to print pascal triangle

C++ program to print pascal triangle

Triangle star pattern in C++language

 

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