Categories: C++

Pattern Program Using Star

C++ implementation for pattern printing.

Pattern 1:

Program code:

#include <iostream>
using namespace std;
void pattern(int n)
{
 // Outer loop to handle number of rows
 for (int i = 0; i < n; i++) {

  // Inner loop to handle number of columns
 
  for (int j = 0; j <= i; j++) {

   // Printing stars
   cout << "* ";
  }

  // Ending line after each row
  cout << endl;
 }
}

// Driver Function
int main()
{
 int n = 5;
 pattern(n);
 return 0;
}
Output:
* 
* * 
* * * 
* * * * 
* * * * * 
Pattern 2:

Program code:


#include <iostream>
using namespace std;
void pattern(int n)
{
 // Outer loop to handle number of rows
 for (int i = n; i > 0; i--) {

  // Inner loop to handle number of columns
 
  for (int j = 0; j <= i; j++) {

   // Printing stars
   cout << "* ";
  }

  // Ending line after each row
  cout << endl;
 }
}

// Driver Function
int main()
{
 int n = 5;
 pattern(n);
 return 0;
}
Output:
* * * * * * 
* * * * * 
* * * * 
* * * 
* * 
*
Pattern 3:

Program code:


#include <iostream>
using namespace std;
void pattern(int n)
{
 int i = 0, j = 0, k = 0;
    while (i < n) {
 
        // for number of spaces
        while (k < (n - i - 1)) {
            cout << "  ";
            k++;
        }
 
        // resetting k because we want to run k from
        // beginning.
        k = 0;
        while (j <= i) {
            cout << "* ";
            j++;
        }
 
        // resetting k so as it can start from 0.
        j = 0;
        i++;
        cout << endl;
    }
}

// Driver Function
int main()
{
 int n = 5;
 pattern(n);
 return 0;
}
Output:
        * 
      * * 
    * * * 
  * * * * 
* * * * * 
Pattern 4:

Program code:


#include <iostream>
using namespace std;
void pattern(int n)
{
 // number of spaces
    int k = 2 * n - 2;
 
    // Outer loop to handle number of rows
    
    for (int i = n; i > 0; i--) {
 
        // Inner loop to handle number spaces
        for (int j = 0; j < n - i; j++)
            cout << "  ";
 
        // Decrementing k after each loop
        k = k - 2;
 
        // Inner loop to handle number of columns
        
        for (int j = 0; j < i; j++) {
            // Printing stars
            cout << "* ";
        }
 
        // Ending line after each row
        cout << endl;
    }
}

// Driver Function
int main()
{
 int n = 5;
 pattern(n);
 return 0;
}
Output:
* * * * * 
  * * * * 
    * * * 
      * * 
        * 

Note: also read about Sum Of Series 1 + 2 + 4 + 8 + 16 + 32 + . . n

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

What is object oriented design patterns

A design pattern is a reusable solution to a commonly occurring problem in software design. They…

4 months ago

Factory Method Design Pattern in OODP

Factory Method is a creational design pattern that deals with the object creation. It separates…

4 months ago

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

10 months ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

10 months ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

10 months ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

11 months ago