Open In App

C++ Program To Print Character Pattern

Improve
Improve
Like Article
Like
Save
Share
Report

Here we will build a C++ Program To Print Character patterns using 2 Approaches i.e.

  1. Using for loop
  2. Using while loop

Printing 1 character pattern in C++ using different approaches.

1. Using for loop

Input:

rows = 5 

Output: 

A 
B B 
C C C 
D D D D 
E E E E E 

Approach 1:

Assign any character to one variable for the printing pattern. The first for loop is used to iterate the number of rows and the second for loop is used to repeat the number of columns. Then print the character based on the number of columns.

C++




// C++ program to print character
// pattern using character
#include <iostream>
using namespace std;
 
int main()
{
    int i, j;
   
    // input entering number of rows
    int rows = 5;
   
    // taking first character of alphabet
    // which is useful to print pattern
    char character = 'A';
   
    // first for loop is used to identify number rows
    for (i = 0; i < rows; i++) {
       
        // second for loop is used to identify number
        // of columns based on the rows
        for (j = 0; j <= i; j++) {
           
            // printing character to get the required
            // pattern
            cout << character << " ";
        }
        cout << "\n";
       
        // incrementing character value so that it
        // will print the next character
        character++;
    }
    return 0;
}


Output

A 
B B 
C C C 
D D D D 
E E E E E 

Approach 2: 

Printing pattern by converting given number into character.

Assign any number to one variable for the printing pattern. The first for loop is used to iterate the number of rows and the second for loop is used to repeat the number of columns. After entering into the loop convert the given number into character to print the required pattern based on the number of columns.

C++




// C++ program to print character pattern by
// converting number in to character
#include <iostream>
using namespace std;
 
int main()
{
    int i, j;
   
    // input entering number of rows
    int rows = 5;
   
    // given a number
    int number = 65;
   
    // first for loop is used to identify number rows
    for (i = 0; i < rows; i++) {
       
        // second for loop is used to identify number
        // of columns based on the rows
        for (j = 0; j <= i; j++) {
           
            // converting number in to character
            char character = char(number);
           
            // printing character to get the required
            // pattern
            cout << character << " ";
        }
        cout << "\n";
       
        // incrementing number value so that it
        // will print the next character
        number++;
    }
    return 0;
}


Output

A 
B B 
C C C 
D D D D 
E E E E E 

2. Using while loops

Input:

rows = 5 

Output: 

A 
B B 
C C C 
D D D D 
E E E E E 

Approach 1:

The while loops check the condition until the condition is false. If the condition is true then it enters into the loop and executes the statements.

C++




// C++ program to print character pattern by
// converting number in to character
#include <iostream>
using namespace std;
 
int main()
{
 
    int i = 1, j = 0;
   
    // input entering number of rows
    int rows = 5;
   
    // given a character
    char character = 'A';
   
    // while loops checks the conditions until the
    // condition is false if condition is true then enters
    // in to the loop and executes the statements
    while (i <= rows) {
        while (j <= i - 1) {
           
            // printing character to get the required
            // pattern
            cout << character << " ";
            j++;
        }
        cout << "\n";
       
        // incrementing character value so that it
        // will print the next character
        character++;
        j = 0;
        i++;
    }
    return 0;
}


Output

A 
B B 
C C C 
D D D D 
E E E E E 

Approach 2:  

Printing pattern by converting given number into character using while loop.

C++




// C++ program to print character pattern by
// converting number in to character
#include <iostream>
using namespace std;
 
int main()
{
 
    int i = 1, j = 0;
   
    // input entering number of rows
    int rows = 5;
   
    // given a number
    int number = 65;
   
    // while loops checks the conditions until the
    // condition is false if condition is true then enters
    // in to the loop and executes the statements
    while (i <= rows) {
        while (j <= i - 1) {
           
            // converting number in to character
            char character = char(number);
           
            // printing character to get the required
            // pattern
            cout << character << " ";
            j++;
        }
        cout << "\n";
       
        // incrementing number value so that it
        // will print the next character
        number++;
        j = 0;
        i++;
    }
    return 0;
}


Output

A 
B B 
C C C 
D D D D 
E E E E E 

Time complexity: O(n2) where n is given no of rows

Space complexity: O(1)



Last Updated : 10 Aug, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads