Menu
Coddy logo textTech

Number Pattern

Part of the Fundamentals section of Coddy's C++ journey — lesson 72 of 74.

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

std::string str(10, 'a');

In this example, str will hold 10 occurrences of a: "aaaaaaaaaa"

Let's break down how this works: 

  1. std::string str declares a string variable named str
  2. (10, 'a') the constructor of string that creates a string with 10 times the char 'a'
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++:

std::string str(10, 'a');

This creates a string with 10 occurrences of the character 'a': "aaaaaaaaaa"

The constructor takes two parameters:

  • Number of repetitions (10)
  • Character to repeat ('a')

Try it yourself

#include <iostream>

int main() {
    int n;
    std::cin >> n;
    // Write your code below
    
    return 0;
}
quiz iconTest yourself

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

All lessons in Fundamentals