Menu
Coddy logo textTech

Flat Asterisk Pyramid

Part of the Fundamentals section of Coddy's R journey — lesson 76 of 78.

challenge icon

Challenge

Easy

Read a single integer n from input and print a left-aligned asterisk pyramid with n rows.

Each row i (from 1 to n) should contain exactly i asterisks. Use the strrep() function to repeat the "*" character and cat() to print each row on a new line.

For example, if the input is:

5

The output should be:

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

If the input is:

3

The output should be:

*
**
***

Try it yourself

# Read input
con <- file("stdin", "r")
n <- as.integer(suppressWarnings(readLines(con, n = 1)))

# TODO: Write your code below
# Use a loop to print n rows
# Each row i should contain i asterisks
# Use strrep() to repeat "*" and cat() to print each row

All lessons in Fundamentals