Start learning free

LESSON 177 OF 347 · FUNCTIONS & CLASSES

Print a Rhombus of Stars in C#

Practice O(n2) Read the full explanation

Given an odd positive integer n print a rhombus of stars as in the example below

Example:

Input: n = 5
Output:
  *  
 *** 
*****
 *** 
  *  

Explanation:
There are n = 5 lines. Each line contains exactly n = 5 characters.
1st line contains two spaces, one '*' and two spaces.
2nd line contains one space, three '*' and one space.
3rd line contains five stars.
4th line contains one space, three '*' and one space.
5th line contains two spaces, one '*' and two spaces.
Note: Be careful to add the necessary spaces at the end of every line!


STEP 1 OF 7
    Ctrl + Enter to check

    C# · FUNCTIONS & CLASSES

    Print Rhombus in C#, explained

    Problem overview

    The method PrintRhombus takes an odd positive integer n and prints n lines, each exactly n characters wide, forming a diamond of stars. For n = 5 the lines are two spaces, one star, two spaces; then one space, three stars, one space; then five stars; and the pattern shrinks back down. The statement points out that the spaces at the end of each line matter, so every line must be padded to the full width.

    In the listing below, square brackets are added only so the trailing spaces can be seen. The method itself prints no brackets.

    [  *  ]
    [ *** ]
    [*****]
    [ *** ]
    [  *  ]
    

    Approach: track stars and spaces per line

    Every line has the same shape: some spaces, some stars, and the same number of spaces again. Because the shape is symmetrical, two numbers describe each line: stars and spaces (the number of spaces on each side). Their relation is stars + 2 * spaces = n.

    The first line has one star and n / 2 spaces on each side. Moving down through the top half, each line has one more star on each side, so stars grows by 2, and spaces shrinks by 1. From the middle line on, it runs in reverse. Repeating a character a given number of times is needed three times per line, so it goes into a helper, BuildString.

    Tracing n = 5, with the values used to print each line:

    Line ispacesstars
    021
    113
    205
    313
    421

    Step by step

    1. Start with one star. int stars = 1; is the count for the first line.
    2. Compute the side spaces. int spaces = n / 2;. Dividing two int values drops the remainder, so 5 / 2 is 2. Since n is odd, this leaves exactly room for the single star.
    3. Loop over the lines. for (int i = 0; i < n; i++) runs once per line.
    4. Build and print the line. Join the left spaces, the stars and the right spaces with +, then print the result with Console.WriteLine. The right-hand spaces are part of the line, and the statement requires them.
    5. Update for the next line. If i < n / 2, the rhombus is still widening: add 2 to stars and subtract 1 from spaces. Otherwise it narrows: subtract 2 from stars and add 1 to spaces. The middle line, where i == n / 2, goes into the else branch, so the shrinking starts right after it.
    6. Write BuildString. It's a public method that returns a string and takes an int n and a char character.
    7. Fill it. Start from an empty string, loop n times with str += character;, and return the string.

    The solution in C#

    class Solution {
    
        public string BuildString(int n, char character) {
            string str = "";
            for (int i = 0; i < n; i++) {
                str += character;
            }
            return str;
        }
    
        public void PrintRhombus(int n) {
            int stars = 1;
            int spaces = n / 2;
            for (int i = 0; i < n; i++) {
                string str = BuildString(spaces, ' ') +
                BuildString(stars, '*') +
                BuildString(spaces, ' ');
                Console.WriteLine(str);
                if (i < n / 2) {
                    stars += 2;
                    spaces -= 1;
                } else {
                    stars -= 2;
                    spaces += 1;
                }
            }
        }
    }
    

    C# details worth noting:

    • char versus string. The helper's parameter is a char, so the calls pass ' ' and '*' with single quotes.
    • string += char appends the character. The left side is a string, so the character is converted to text and joined. If you added two char values together, you'd get an int instead.
    • A statement can span lines. The expression for str continues over three lines and ends only at the ;. Putting the + at the end of a line is a common style.
    • The library shortcut. new string(' ', spaces) does what BuildString does. Writing the helper yourself practises loops and string building.
    • Indentation doesn't matter. The compiler follows braces, so the lines of the multi-line expression can be indented any way.

    Complexity

    The output has n lines of n characters each, so O(n²) characters are produced no matter how they are built. This implementation does a little more work: += in BuildString copies the string every time, so building a run of k characters costs O(k²) character copies, and a line costs O(n²) in the worst case. The total is O(n³), which is fine for the small n in this lesson. Using new string(c, k) or a StringBuilder would bring it back down to O(n²). Extra space is O(n) for one line.

    Edge cases and common mistakes

    • n = 1. The loop runs once, prints a single star with zero spaces on each side, and updates the variables, which are then never used.
    • Forgetting the right-hand spaces. The lines would be shorter than n characters, and the output wouldn't match.
    • Using i <= n / 2 for the widening branch. The middle line would still widen, and the rhombus would continue growing for one extra line, so it would be too big.
    • Passing a string to the helper. BuildString(2, " ") doesn't compile, because the second parameter is a char and needs single quotes.
    • Updating before printing. If the if/else block ran first, the first line would already be a three-star line, and the output would be shifted by one line.
    • Even n. The problem promises an odd number, because a symmetric pattern with one centre needs one.

    Testing

    • n = 1 prints *.
    • n = 3 prints * , *** and * , each 3 characters wide.
    • n = 5 prints the five lines shown above.
    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free