Codeforcoding

Cpp program to hollow triangle star pattern

Cpp program to hollow triangle star pattern

In this tutorial, we will discuss the Cpp program to hollow triangle star pattern

We will learn how to create Floyd’s triangle hollow pattern in C++ language using nested for loop

Floyd’s triangle star pattern 1

Program 1

C++ program to display hollow right triangle star pattern

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int i,j,rows;
    cout << "Enter number of rows" << endl;
    cin>>rows; //get input from user
    for(i=1; i<=rows; i++){
        for(j=1; j<=i; j++){
        if(j==1 || j==i || i==rows){
            cout<<"*";
        }
        else{
            cout<<" ";
        }
    }
    cout<<"\n";
    }
    return 0;
}

When the above code is executed, it produces the following results:

Cpp program to hollow triangle star pattern
floyd’s triangle in C++

 

Floyd’s triangle star pattern 2

Program 2

C++ program to display hollow mirrored right triangle star pattern

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
    int i,j,rows;
    cout << "Enter the number of rows " << endl;
    cin>>rows;//get input from user
    for(i=1; i<=rows; i++){ //iterates for print row
        for(j=i; j<=rows; j++){
        cout<<" ";//print space
    }
        for(j=1; j<=i; j++){
                if(i==rows || j==1 || j==i)
                {
                    cout<<"*";
                }
                else{
                    cout<<" ";
                }

    }
    cout<<"\n";
    }
    getch();
    return 0;
}

When the above code is executed, it produces the following results:

Output

 

Floyd’s triangle star pattern 3

Program 3

C++ program to display hollow inverted  right triangle star pattern

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

int main()
{
    int i,j,rows;
    cout << "Enter the number of rows" << endl;
    cin>>rows; //get input from user
    for(i=1; i<=rows; i++){//parent for loop
        //this for loop used to iterate rows
        for(j=i; j<=rows; j++)
        {
            if(i==1 || j==i|| j==rows ){
                cout<<"*"; //print star
            }
            else{
                cout<<" ";
            }
        }
        cout<<"\n";//move to next line

    }
    getch();
    return 0;
}

When the above code is executed, it produces the following results:

Output

Floyd’s triangle star pattern 4

Program 4

C++ program to display hollow inverted mirrored right triangle star pattern

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

int main()
{
    int i,j,rows;
    cout << "Enter the number of rows" << endl;
    cin>>rows; //get input from user
    for(i=1; i<=rows; i++){//parent for loop
        //this for loop used to iterate rows
        for(j=1; j<=i; j++)
        {
            cout<<" "; //print space
        }
            for(j=i; j<=rows; j++)
            {
                 if(i==1 || j==i|| j==rows ){
                cout<<"*"; //print star
            }
            else{
                cout<<" ";
            }
            }
        cout<<"\n";//move to next line
    }
    getch();
    return 0;
}

When the above code is executed, it produces the following results:

Output

 

Similar post

Floyd’s triangle number pattern using for loop in C

Floyd’s triangle pattern using nested for loop in Java

Floyd’s triangle pattern using nested while loop in Java

Hollow pyramid triangle pattern in C++ language

Pyramid star pattern in Cpp

Pyramid star pattern in C

Pyramid star pattern in Java 

 

Suggested for you

Loops in Java language

Loops in C language

Loops in C++ language

 

For loop in C++ language

For loop in Java language

For loop in C language

For loop in Python language

 

While loop in Java language

While loop in C language

While loop in C++ language

While loop in Python language

 

Nested for loop in Java language

Nested for loop in C++ language

Nested for loop in C language

Nested for loop in Python language

 

 

Java code to hollow triangle pattern
Cpp program to Floyd's triangle star pattern
Exit mobile version