A continuous character pattern prints alphabets consecutively across rows, with each row containing one more character than the previous row. Nested loops can be used to control the rows and characters in the pattern.
Examples:
Input: rows = 5
Output: ÂAÂ
B CÂ
D E FÂ
G H I JÂ
K L M N OÂInput: rows = 3
Output:AÂ
B CÂ
D E FÂ
Types of Approaches
Continuous character patterns can be printed using the following approaches. These approaches can be implemented using both for and while loops.
Using for Loop
Approach 1: Using a Character Variable
The pattern starts with A and increments the character after every print. The outer loop controls the rows, while the inner loop determines the number of characters in each row.
- Initialize a character variable with A.
- Use an outer loop to iterate through the rows.
- Use an inner loop to print characters based on the current row.
- Print the current character and increment it after each iteration.
#include <stdio.h>
int main()
{
int i, j;
// Number of rows
int rows = 3;
// Taking first character of alphabet
// which is useful to print pattern
char character = 'A';
// This loop is used to identify
// number rows
for (i = 0; i < rows; i++)
{
// This 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
printf("%c ",character);
// Incrementing character value so
// that it will print the next character
character++;
}
printf("\n");
}
return 0;
}
Output
A B C D E F
Approach 2: Converting an Integer Value to a Character
Characters are represented by integer values in C. For example, the ASCII value of A is 65. By converting the integer value to char and incrementing it after each print, the same continuous pattern can be generated.
- Initialize an integer with the ASCII value of A.
- Use an outer loop to iterate through the rows.
- Use an inner loop to print the required number of characters.
- Convert the integer to char before printing it.
- Increment the integer after each character.
#include <stdio.h>
// Driver code
int main()
{
int i, j;
// Number of rows
int rows = 5;
// Given a number
int number = 65;
// This loop is used to identify
// number of rows
for (i = 0; i < rows; i++)
{
// This 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
printf("%c ", character);
// Incrementing number value so
// that it will print the next
// character
number++;
}
printf("\n");
}
return 0;
}
Output
A B C D E F G H I J K L M N O
Using while Loop
Approach 1: Using a Character Variable
The same pattern can be generated using nested while loops. The outer loop controls the rows, while the inner loop prints the required number of characters in each row.
#include <stdio.h>
// Driver code
int main()
{
int i = 1, j = 0;
// Number of rows
int rows = 5;
// Given a character
char character = 'A';
while (i <= rows)
{
while (j <= i - 1)
{
// Printing character to get
// the required pattern
printf("%c ",character);
j++;
// Incrementing character value
// so that it will print the next
// character
character++;
}
printf("\n");
j = 0;
i++;
}
return 0;
}
Output
A B C D E F G H I J K L M N O
Approach 2: Converting an Integer Value to a Character
An integer value representing the ASCII code of A can also be converted to a character while using nested while loops.
#include <stdio.h>
// Driver code
int main()
{
int i = 1, j = 0;
// Number of rows
int rows = 5;
// Given a number
int number = 65;
while (i <= rows)
{
while (j <= i - 1)
{
// Converting number in to character
char character = (char)(number);
// Printing character to get the
// required pattern
printf("%c ",character);
j++;
// Incrementing number value so
// that it will print the next
// character
number++;
}
printf("\n");
j = 0;
i++;
}
return 0;
}
Output
A B C D E F G H I J K L M N O