C Program to Print Continuous Character Pattern
Here, we will see how to print continuous character patterns using a C program. Below are the 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
There are 2 ways to print continuous character patterns in C:
- Using for loop.
- Using while loop.
Let’s discuss each of these in detail.
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;
}
|
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 in to 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
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 given no of rows
Auxiliary space: O(1)
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(n2) where n is given rows
Auxiliary Space: O(n)
Last Updated :
07 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...