Progams to print Triangles in C++ using " * "
Pattern programs in C++:
In C++ programming language we can use nested loops to print the patterns of
character numbers and stars(*).We use outer for loop for the number of rows and inner for loop is responsible of column.
There are some examples to print the patterns using number and star.
#1 Program to print Triangle using "*" in c++
Source Code
#include < iostream >
using namespace std;
int main ( )
{
int a , b , c ;
for (a=1; a<=5; a++)
{
for ( b = 4 ; b >= a ; b--)
{
cout<<" ";
}
for (c= 1; c <=( 2 * a - 1 ) ; c++)
{
cout << "*" ;
}
cout<<"\n" ;
}
getch( );
}
Output
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
#2 Program to print Triangle using "*" in c++
Source Code
#include < iostream >
#include<conio.h>
void main ( ) {
int a, b , c ;
for(a=5;a>=1;a--)
{
for ( b= 5; b>a ; b-- ) {
cout<< " " ;
}
for (c= 1 ; k < (a*2) ; k++){
cout<< "*";
cout<<endl;
}
getch ();
}
Output
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
#3 Program to print Half pyramid using "*" in c++
Source Code
#include <iostream>
using namespace std;
int main ( )
{
int rows;
cout << " Enter no. of rows : \n ";
cin >> rows;
for(int x = 1; x <= rows; x++)
{
for( int y = 1; y <= x; y++)
{
cout << "* ";
}
cout<<endl;
}
return 0;
}
Output
*
* *
* * *
* * * *
* * * * *
#4 Program to Half pyramid using "*" in c++
Source Code
#include< iostream >
#include<conio.h>
void main ( )
{
int x , y ;
for ( x=5 ; x >= 1 ; --x )
{
for (y=1 ; y<=x ; ++y)
{
cout << "*" ;
}
cout << endl;
}
getch();
}
Output
* * * * *
* * * *
* * *
* *
*
#5: Program to print half pyramid using numbers
Source Code
#include < iostream >
using namespace std:
int main ( ){
int row;
cout<<" Enter number of rows :\n";
cin>>row;
for(int x=1 ; x<=row ; x++){
for(int y= 1; y<=x; y++){
cout<<x<<" ";
}
cout<<endl;
}
return 0;
}
OutPut:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
2 Comments
coding is a best part of a engineering,remind old days coding good article :)
ReplyDeleteAwesome! Very informative read, thanks for sharing!
ReplyDelete