The letter U star pattern draws two upright sides joined by a rounded bottom. Leaving out the two bottom corners is what makes it look like a U and not a square cup.
Quick answer: Loop over 7 rows and 5 columns. Print the two sides in the first and last column on every row except the last. On the last row, print only the middle three columns, which closes the bottom with rounded corners.
This program is part of the Star Patterns collection.
Table of contents8 sections
Letter U Star Pattern output
Output (7 rows):
* * * * * * * * * * * * ***

How the letter u star pattern works
The sides stop one row early, and the bottom bar starts one column in from each side. The empty corner cells are what turn a box into a curve. The same trick rounds the top of the letter A. 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((j = 1 or j = n − 2) and i < n) or (i = n and j > 1 and j < n − 2); print spaces everywhere else. - Move to the next line.
Key takeaways
- The U is two sides and a short bottom bar.
- Empty corner cells make the bottom look curved.
- Turn it upside down and you get an arch shape like an n.
- Fill the corners and the letter becomes a square cup.
Letter U 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 (((j == 1 || j == n - 2) && i < n) || (i == n && j > 1 && j < n - 2))
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}Letter U 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 (((j == 1 || j == n - 2) && i < n) || (i == n && j > 1 && j < n - 2))
cout << "*";
else
cout << " ";
}
cout << endl;
}
return 0;
}Letter U Star Pattern in Java
public class LetterUStar {
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 (((j == 1 || j == n - 2) && i < n) || (i == n && j > 1 && j < n - 2))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}Letter U Star Pattern in Python
n = 7
for i in range(1, n + 1):
for j in range(1, n - 2 + 1):
if ((j == 1 or j == n - 2) and i < n) or (i == n and j > 1 and j < n - 2):
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 print the letter V instead?
A V has slanted sides that meet at a point, so it needs diagonals instead of columns. See the letter V star pattern.
How do I print the letter O?
Add a top bar with the same rounded corners: print the middle three columns on row 1 as well.
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.






