There are 2 ways to print continuous character patterns in C i.e:
- Using for loop
- Using while loop
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: Using Character
- Assign any character to one variable for the printing pattern.
- The first for loop is used to iterate the number of rows.
- 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.
Below is the C program to print continuous character patterns using character using for loop:
C
#include <stdio.h>
int main()
{
int i, j;
int rows = 3;
char character = 'A' ;
for (i = 0; i < rows; i++) {
for (j = 0; j <= i; j++) {
printf ( "%c " , character);
character++;
}
printf ( "\n" );
}
return 0;
}
|
Time complexity: O(R*R) where R is the given number of rows.
Auxiliary space: O(1), as constant space is being used.
Approach 2: Converting a given number into a character
- Assign any number to one variable for the printing pattern.
- The first for loop is used to iterate the number of rows.
- 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.
Below is the C program to print continuous character patterns by converting numbers into a character:
C
#include <stdio.h>
int main()
{
int i, j;
int rows = 5;
int number = 65;
for (i = 0; i < rows; i++) {
for (j = 0; j <= i; j++) {
char character = ( char )(number);
printf ( "%c " , character);
number++;
}
printf ( "\n" );
}
return 0;
}
|
Output
A
B C
D E F
G H I J
K L M N O
Time complexity: O(R*R) where R is the given number of rows.
Auxiliary space: O(1), as constant space is being used.
2. Using while loop:
Approach 1: Using character
The while loops check the condition until the condition is false. If the condition is true then enter into a loop and execute the statements. Below is the C program to print continuous character patterns using character:
C
#include <stdio.h>
int main()
{
int i = 1, j = 0;
int rows = 5;
char character = 'A' ;
while (i <= rows) {
while (j <= i - 1) {
printf ( "%c " , character);
j++;
character++;
}
printf ( "\n" );
j = 0;
i++;
}
return 0;
}
|
Output
A
B C
D E F
G H I J
K L M N O
Time complexity: O(R*R) where R is the given number of rows.
Auxiliary space: O(1), as constant space is being used.
Approach 2: Converting a given number into a character
Below is the C program to print a continuous character pattern by converting a given number into a character using a while loop:
C
#include <stdio.h>
int main()
{
int i = 1, j = 0;
int rows = 5;
int number = 65;
while (i <= rows) {
while (j <= i - 1) {
char character = ( char )(number);
printf ( "%c " , character);
j++;
number++;
}
printf ( "\n" );
j = 0;
i++;
}
return 0;
}
|
Output
A
B C
D E F
G H I J
K L M N O
Time complexity: O(R*R) where R is the given number of rows.
Auxiliary space: O(1), as constant space is being used.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
22 Sep, 2022
Like Article
Save Article