Printing common pattern examples using For Loop in C++


Example : 1

This is a C++ program that prompts the user to input a value, and then prints a square pattern of asterisks with side length equal to the input value. Here is a brief explanation of the program:

  • The program declares three integer variables: i, n, and a.
  • The program prompts the user to input a value, and reads it from the standard input using the cin object.
  • The program enters an outer for loop that iterates a times, from 1 to a inclusive. In each iteration, the loop body is executed.
  • In the outer loop body, the program outputs a newline character using the cout object. This is done to move the cursor to the next line before printing the asterisks.
  • The program enters an inner for loop that iterates a times, from 1 to a inclusive. In each iteration, the loop body is executed.
  • In the inner loop body, the program outputs an asterisk followed by a space using the cout object. This is done to print a square pattern of asterisks with side length equal to a.
  • After the inner loop terminates, the outer loop moves to the next iteration and repeats the process until all a rows have been printed.
  • Finally, the program returns 0 to the operating system, indicating successful execution.

Source Code

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

To download raw file Click Here

Output

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

Example : 2

This is a C++ program that prompts the user to input a value, and then prints a triangle pattern of asterisks with base length equal to the input value. Here is a brief explanation of the program:

  • The program declares three integer variables: i, n, and a.
  • The program prompts the user to input a value, and reads it from the standard input using the cin object.
  • The program enters an outer for loop that iterates a times, from 1 to a inclusive. In each iteration, the loop body is executed.
  • In the outer loop body, the program outputs a newline character using the cout object. This is done to move the cursor to the next line before printing the asterisks.
  • The program enters an inner for loop that iterates i times, from 0 to i-1 inclusive. In each iteration, the loop body is executed.
  • In the inner loop body, the program outputs a space character using the cout object. The number of spaces printed is equal to the current iteration index i.
  • After the inner loop terminates, the program enters another inner for loop that iterates a-i+1 times, from 0 to a-i inclusive. In each iteration, the loop body is executed.
  • In the second inner loop body, the program outputs an asterisk followed by a space using the cout object. This is done to print a triangle pattern of asterisks with base length equal to a.
  • After the second inner loop terminates, the outer loop moves to the next iteration and repeats the process until all a rows have been printed.
  • Finally, the program returns 0 to the operating system, indicating successful execution.

Source Code

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

Output

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

Example : 3

This is a C++ program to print a pyramid of asterisks (*) with each row having one more asterisk than the previous row.

The program prompts the user to enter a value, which represents the number of rows in the pyramid. The for loop then iterates from 1 to a, with each iteration representing a row in the pyramid.

Within each row, the program first prints a number of spaces equal to the row number (i), to align the asterisks in the center of the pyramid. It then prints a number of asterisks equal to (a-i+1) to create the pyramid shape.

Source Code

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

Output

Enter the value: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