Printing triangle pattern outline using For Loop in C++


Example : 1

This is a C++ program that uses loops to print a triangular pattern of stars. The pattern is printed in a single loop. Here is an explanation of how the program works:

  • The program first declares three integer variables - i, j, and n.
  • The user is prompted to enter a value for n.
  • The program enters a loop that iterates n times. This loop is responsible for printing the rows of the triangle.
  • Within the loop, there is another loop that iterates n times. This loop is responsible for printing the columns of the triangle.
  • For each element in the triangle, the program checks whether it is on the border or not. If the element is on the border (i.e., if j is equal to 1, or if i is equal to n, or if j is equal to i), then a star is printed. Otherwise, a space is printed.
  • The program then exits the loop and the main() function, returning 0.
  • The triangular pattern is printed to the console.

Source Code

#include<iostream>
using namespace std;
int main()
{
    int i,j,n;
    cout<<"\nEnter the value:";
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cout<<"\n";
        for(j=1;j<=n;j++)
        {
            if(j==1||i==n||j==i)
            {
                cout<<" *";
            }
            else
                cout<<"  ";
        }
    }
    return 0;
}
To download raw file Click Here

Output

Enter the value:10
 *                  
 * *                
 *   *              
 *     *            
 *       *          
 *         *        
 *           *      
 *             *    
 *               *  
 * * * * * * * * * *

Example : 2

This is a C++ program that uses loops to print a triangular pattern of stars. The pattern is printed in a single loop. Here is an explanation of how the program works:

  • The program first declares three integer variables - i, j, and n.
  • The user is prompted to enter a value for n.
  • The program enters a loop that iterates n times. This loop is responsible for printing the rows of the triangle.
  • Within the loop, there is another loop that iterates i times. This loop is responsible for printing the spaces before the stars.
  • After printing the spaces, there is another loop that iterates n-i+1 times. This loop is responsible for printing the stars.
  • For each element in the triangle, the program checks whether it is on the border or not. If the element is on the border (i.e., if i is equal to 1, or if j is equal to n, or if j is equal to i), then a star is printed. Otherwise, a space is printed.
  • The program then exits the loop and the main() function, returning 0.
  • The triangular pattern is printed to the console.

Source Code

#include<iostream>
using namespace std;
int main()
{
    int i,j,n;
    cout<<"\nEnter the value:";
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cout<<"\n";
        for(j=1;j<=i;j++)
        {
            cout<<"  ";
        }
        for(j=n;j>=i;j--)
        {
            if(i==1||j==n||j==i)
            {
                cout<<" *";
            }
            else
            {
                cout<<"  ";
            }
        }
    }
    return 0;
}
To download raw file Click Here

Output

Enter the value:10
   * * * * * * * * * *
     *               *
       *             *
         *           *
           *         *
             *       *
               *     *
                 *   *
                   * *
                     *

Example : 3

This is a C++ program that prints a pattern of stars in the shape of a right triangle. Here is an explanation of how the program works:

  • The program first declares three integer variables - i, j, and n.
  • The user is prompted to enter a value for n.
  • The program enters a loop that iterates n times. This loop is responsible for printing the rows of the triangle.
  • Within the loop, there is another loop that iterates n-i times. This loop is responsible for printing the spaces before the stars.
  • After printing the spaces, there is another loop that iterates i times. This loop is responsible for printing the stars.
  • For each element in the triangle, the program checks whether it is on the border or not. If the element is on the border (i.e., if j is equal to 1, or if i is equal to n, or if j is equal to i), then a star is printed. Otherwise, a space is printed.
  • The program then exits the loop and the main() function, returning 0.
  • The triangular pattern is printed to the console.

Source Code

#include<iostream>
using namespace std;
int main()
{
    int i,j,n;
    cout<<"\nEnter the value:";
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cout<<"\n";
        for(j=1;j<=(n-i);j++)
        {
            cout<<"  ";
        }
        for(j=1;j<=i;j++)
        {
            if(j==1||i==n||j==i)
            {
                cout<<" *";
            }
            else
            {
                cout<<"  ";
            }
        }
    }
    return 0;
}

To download raw file Click Here

Output

Enter the value:10
                   *
                 * *
               *   *
             *     *
           *       *
         *         *
       *           *
     *             *
   *               *
 * * * * * * * * * *

Example : 4

This is a C++ program that prints a pattern of stars in the shape of a pyramid. Here is an explanation of how the program works:

  • The program first declares three integer variables - i, j, and n.
  • The user is prompted to enter a value for n.
  • The program enters a loop that iterates n times. This loop is responsible for printing the rows of the pyramid.
  • Within the loop, there is another loop that iterates n-i times. This loop is responsible for printing the spaces before the stars.
  • After printing the spaces, there is another loop that iterates i+1 times. This loop is responsible for printing the stars.
  • For each element in the pyramid, the program checks whether it is on the border or not. If the element is on the border (i.e., if i is equal to 0, or if j is equal to 0, or if j is equal to n-i-1), then a star is printed. Otherwise, a space is printed.
  • The program then exits the loop and the main() function, returning 0.
  • The pyramid pattern is printed to the console.

Source Code

#include<iostream>
using namespace std;
int main()
{
    int i,j,n;
    cout<<"\nEnter the Limit:";
    cin>>n;
    for(i=0;i<n;i++)
    {
        for(j=0;j<n-i;j++)
        {
            if(i==0||j==0||j==n-i-1)
            {
               cout<<" *";
            }
            else
            {
                cout<<"  ";
            }
        }
        cout<<"\n";
    }
    return 0;
}

To download raw file Click Here

Output

Enter the Limit:10
 * * * * * * * * * *
 *               *
 *             *
 *           *
 *         *
 *       *
 *     *
 *   *
 * *
 *


Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples