The letter C star pattern draws a curved left side with a bar at the top and the bottom, and leaves the right side open. Two empty corners on the left make it look round.
Quick answer: Loop over 7 rows and 5 columns. Print the top and bottom bars in columns 2 to 5, and the left side in column 1 on the rows between them. The two left corners stay empty, so the bars start one space in.
This program is part of the Star Patterns collection.
Table of contents8 sections
Letter C Star Pattern output
Output (7 rows):
**** * * * * * ****

How the letter c star pattern works
A C is three strokes: two bars and one side. The bars skip column 1 and the side skips rows 1 and 7, so the corner cells are never printed. Nothing is drawn on the right, which is what keeps the letter open. Every letter on this site is drawn on a grid of rows and columns, and each stroke is one simple test on i and j.
- Run the outer loop for
ifrom1ton, one row per pass. - Go through
n − 2positions in the row. Print stars where(i = 1 and j > 1) or (i = n and j > 1) or (j = 1 and i > 1 and i < n); print spaces everywhere else. - Move to the next line.
Key takeaways
- A C is two bars and a left side, with the right side open.
- The bars start in column 2, so the left corners look rounded.
- Rows 2 to 6 print a single star, because nothing follows the left side.
- Close the right side and the letter becomes an O.
Letter C Star Pattern in C
#include <stdio.h>
int main(void) {
int n = 7;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - 2; j++) {
if ((i == 1 && j > 1) || (i == n && j > 1) || (j == 1 && i > 1 && i < n))
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}Letter C Star Pattern in C++
#include <iostream>
using namespace std;
int main() {
int n = 7;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - 2; j++) {
if ((i == 1 && j > 1) || (i == n && j > 1) || (j == 1 && i > 1 && i < n))
cout << "*";
else
cout << " ";
}
cout << endl;
}
return 0;
}Letter C Star Pattern in Java
public class LetterCStar {
public static void main(String[] args) {
int n = 7;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - 2; j++) {
if ((i == 1 && j > 1) || (i == n && j > 1) || (j == 1 && i > 1 && i < n))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}Letter C Star Pattern in Python
n = 7
for i in range(1, n + 1):
for j in range(1, n - 2 + 1):
if (i == 1 and j > 1) or (i == n and j > 1) or (j == 1 and i > 1 and i < n):
print("*", end="")
else:
print(" ", end="")
print()Time and space complexity
The program prints one character for each position in the pattern, so for n rows it runs in O(n²) time. It only needs a few loop variables, so the extra memory is O(1).
Frequently asked questions
How do I turn the C into an O?
Add a right side in column 5 on the middle rows and stop the bars at column 4. See the letter O star pattern.
Why do the top and bottom rows start with a space?
Column 1 is the corner of the letter. Leaving it empty rounds the corners, so each bar starts with one space.
How do I let the user choose the size?
Read n from the keyboard instead of fixing it in the code: scanf("%d", &n); in C, cin >> n; in C++, new Scanner(System.in).nextInt() in Java and int(input()) in Python.






