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:
std::string strdeclares a string variable namedstr(10, 'a')the constructor of string that creates a string with 10 times the char'a'
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++:
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;
}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 ShortcutsComparison OperatorsString Comparison3Variables Part 2
Type DeclarationNaming ConventionsRecap - Initialize VariablesType Casting Part 1Type Casting Part 26Decision Making
If StatementIf - ElseSwitch StatementConditional OperatorRecap - If ElseNested If - Else