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:
new stringcreates a string of characters- the char
'a'indicates what character we want to repeat 10indicates how many times to repeat the character
Challenge
EasyEach 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
nfrom user - 1 <=
n< 1000
Tips
- Try starting from the small triangles
- Check the hint if you are stuck ;)
nrepresents 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 repeat10- 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());
}
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorIncrement/DecrementPost Increment/DecrementArithmetic Shortcuts5Operators Part 2
Comparison OperatorsLogical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3