C++ Program To Print Character Pattern - GeeksforGeeks (2024)

Last Updated : 10 Aug, 2022

Improve

Improve

Like Article

Like

Save

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:

AB BC C CD D D DE 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:

AB BC C CD D D DE 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)



Like Article

Suggest improvement

Next

C Program To Print Character Pyramid Pattern

Share your thoughts in the comments

Please Login to comment...

C++ Program To Print Character Pattern - GeeksforGeeks (2024)

References

Top Articles
Latest Posts
Article information

Author: Msgr. Benton Quitzon

Last Updated:

Views: 6780

Rating: 4.2 / 5 (63 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Msgr. Benton Quitzon

Birthday: 2001-08-13

Address: 96487 Kris Cliff, Teresiafurt, WI 95201

Phone: +9418513585781

Job: Senior Designer

Hobby: Calligraphy, Rowing, Vacation, Geocaching, Web surfing, Electronics, Electronics

Introduction: My name is Msgr. Benton Quitzon, I am a comfortable, charming, thankful, happy, adventurous, handsome, precious person who loves writing and wants to share my knowledge and understanding with you.