Menu
Coddy logo textTech

Number Pattern

Part of the Fundamentals section of Coddy's C# journey — lesson 67 of 69.

When creating patterns like pyramids, we often need to create strings with repeated characters. Here's a useful technique to create such strings:

string str = new string('a', 10);

str will hold "aaaaaaaaaa"

Let's break down how this works: 

  1. new string creates a string of characters
  2. the char 'a' indicates what character we want to repeat
  3. 10 indicates how many times to repeat the character
challenge icon

Challenge

Easy

Each test case has one input - an odd whole number.
Your task is to print n - pyramid using *, here are some examples:

1 - pyramid

*

5 - pyramid

*
***
*****

7 - pyramid

*
***
*****
*******

Input

  • odd integer n from user
  • 1 <= n < 1000

Tips

  • Try starting from the small triangles
  • Check the hint if you are stuck ;)

n represents the number of  * in the bottom row

Cheat sheet

To create strings with repeated characters in C#, use the string constructor:

string str = new string('a', 10);

This creates a string containing the character 'a' repeated 10 times, resulting in "aaaaaaaaaa".

The syntax breakdown:

  • new string - creates a string of characters
  • 'a' - the character to repeat
  • 10 - how many times to repeat the character

Try it yourself

using System;

public class Program {
    public static void Main(string[] args) {
        int n = int.Parse(Console.ReadLine());
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals