C++ Program To Print Character Pattern
Last Updated :
10 Aug, 2022
Here we will build a C++ Program To Print Character patterns using 2 Approaches i.e.
- Using for loop
- 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;
}
OutputA
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;
}
OutputA
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;
}
OutputA
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;
}
OutputA
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)
Similar Reads
C++ Program To Print Continuous Character Pattern Here we will build a C++ Program To Print Continuous Character patterns using 2 different methods i.e: Using for loopsUsing while loopsInput: rows = 5Output: A B C D E F G H I J K L M N O 1. Using for loopApproach 1: Assign any character to one variable for the printing pattern. The first for loop i
5 min read
C++ Program To Print Number Pattern Here, we will see a C++ program to print the 3 different number patterns. There are 3 number patterns covered using for loop and while loop with their respective explanation.3 Different Number Patterns: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 1 12 123 1234 12345Pattern 1:In
6 min read
Program to print Kite Pattern The task is to draw a KITE pattern using '$' Example: Output: $ $$$ $$$$$ $$$$$$$ $$$$$$$$$ $$$$$$$ $$$$$ $$$ $ $$$ $$$$$ Below is the code to implement the above problem: Program: C++ // C++ Program to print Kite Pattern #include <bits/stdc++.h> #include <stdlib.h> using namespace std;
8 min read
Program to print reverse character bridge pattern For a given value N, denoting the number of Charters starting from the A, print reverse character bridge pattern.Examples : Input : n = 5 Output : ABCDEDCBA ABCD DCBA ABC CBA AB BA A A Input : n = 8 Output : ABCDEFGHGFEDCBA ABCDEFG GFEDCBA ABCDEF FEDCBA ABCDE EDCBA ABCD DCBA ABC CBA AB BA A A Recomm
5 min read
C++ Program to Print Cross or X Pattern Given a number n, we need to print an X pattern of size n. Input : n = 3Output : $ $ $ $ $Input : n = 5Output : $ $ $ $ $ $ $ $ $Input : n = 4Output : $ $ $$ $$ $ $ We need to print n rows and n columns. So we run two nested loops. The outer loop prints all rows one by one (runs for i = 1 to n). The
2 min read
C++ Program to Print the Pattern 'G" In this article, we will learn how to print the pattern G using stars and white spaces. Given a number n, we will write a program to print the pattern G over n lines or rows.Examples: Input : 7 Output : *** * * * *** * * * * *** Input : 9 Output : ***** * * * * *** * * * * * * ***** In this program,
2 min read
Program to print the given H Pattern Given an integer N, the task is to print the Alphabet H Pattern as given below: 1 N 2 * 3 3 * 2 N * 3 2 1 * 2 3 3 2 * 1 N Examples: Input: N = 3 Output: 1 3 2 2 3 2 1 2 2 1 3 Input: N = 4 Output: 1 4 2 3 3 2 4 3 2 1 3 2 2 3 1 4 Approach: Print the Left value and leave 2 * (index - 1) blank spaces
6 min read
Program to print Step Pattern The program must accept a string S and an integer N as the input. The program must print the desired pattern as shown below: Examples: Input: string = "abcdefghijk", n = 3 Output: a *b **c *d e *f **g *h i *j **k Explanation: Here N is 3. The highest possible height of the string pattern must be 3.
7 min read
C++ Program To Print Triangle Pattern Here we will see how to print triangle patterns using a C++ program. There are 4 patterns discussed here: Right Triangle.Inverted Right Triangle.Equilateral Triangle.Inverted Equilateral Triangle.Inverted Mirrored Right Triangle. Let's start discussing each of these in detail. 1. Right Triangle Belo
6 min read
C++ Program For char to int Conversion In C++, we cannot directly perform numeric operations on characters that represent numeric values. If we attempt to do so, the program will interpret the character's ASCII value instead of the numeric value it represents. We need to convert the character into an integer.Example:Input: '9'Output: 9Ex
2 min read