C++ Program To Print Continuous Character Pattern
Last Updated :
30 Aug, 2022
Here we will build a C++ Program To Print Continuous Character patterns using 2 different methods i.e:
- Using for loops
- Using while loops
Input:
rows = 5
Output:
A
B C
D E F
G H I J
K L M N O
1. Using for loop
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 and increment the character value at each column to print a continuous character pattern.
C++
// C++ program to print continuous 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 << " ";
// incrementing character value so that it
// will print the next character
character++;
}
cout << "\n";
}
return 0;
}
OutputA
B C
D E F
G H I J
K L M N O
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 and increment the character value at each column to print a continuous character pattern.
C++
// C++ program to print continuous 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 << " ";
// incrementing number value so that it
// will print the next character
number++;
}
cout << "\n";
}
return 0;
}
OutputA
B C
D E F
G H I J
K L M N O
Using while loops:
Input:
rows=5
Output:
A
B C
D E F
G H I J
K L M N O
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 the continuous
// character pattern using while loop
#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++;
// incrementing character value so that it
// will print the next character
character++;
}
cout << "\n";
j = 0;
i++;
}
return 0;
}
OutputA
B C
D E F
G H I J
K L M N O
Approach 2:
Printing pattern by converting given number into character using while loop.
C++
// C++ program to print continuous
// 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++;
// incrementing number value so that it
// will print the next character
number++;
}
cout << "\n";
j = 0;
i++;
}
return 0;
}
OutputA
B C
D E F
G H I J
K L M N O
Time complexity: O(n2) where n is given no of rows
Space complexity: O(1)
Similar Reads
C++ Program To Print Character Pattern Here we will build a C++ Program To Print Character patterns using 2 Approaches i.e. Using for loopUsing 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
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
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 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 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 hollow Triangle pattern Given the number of lines as N, the task is to form the given hollow Triangle pattern.Examples: Input: N = 6 Output: ************ ***** ***** **** **** *** *** ** ** * * Approach: Input number of rows to print from the user as N.To iterate through rows run an outer loop from number of rows till it i
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 Extract Characters From a String Character extraction can be done by iterating through the string in the form of a character array. It basically means plucking out a certain amount of characters from an array or a string. Now, to take input in C we do it by using the following methods: scanf("%c",&str[i]); - Using a loopscanf("
2 min read
How to Match a Pattern in a String in C++? In C++, strings are sequences of characters stored in a char array. Matching a pattern in a string involves searching for a specific sequence of characters (the pattern) within a given string. In this article, we will learn how to match a pattern in a string in C++. Example: Input:Text: "GeeksForGee
2 min read