{ }codelper
for looprepeating stringsnested loopsP0007

Star triangle

Given n, print i stars (*) on the i-th line (1 ≤ i ≤ n).

Input
1 ≤ n ≤ 50
Output
n lines
Example 1 · stdin
3
stdout
*
**
***
main.py
Input (stdin)

When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).

$ Ready

Open this on a computer to write and run code. On mobile you can read the problem and the solutions.

In other languagesShow solutions by language · 8 languages

Repeating a character differs by language: Python "*" * i, JavaScript/Java "*".repeat(i), C# new string('*', i), C++ std::string(i, '*').
C has none, so it uses a nested loop.
Craykon grows the line with + "*".

Pythonmain.py · 3 lines
n = int(input())for i in range(1, n + 1):    print("*" * i)
JavaScriptmain.js · 2 lines
const n = Number(require("fs").readFileSync(0, "utf8"));for (let i = 1; i <= n; i++) console.log("*".repeat(i));
TypeScriptmain.ts · 3 lines
import { readFileSync } from "node:fs";const n: number = Number(readFileSync(0, "utf8"));for (let i = 1; i <= n; i++) console.log("*".repeat(i));
JavaMain.java · 8 lines
import java.util.Scanner; public class Main {    public static void main(String[] args) {        int n = new Scanner(System.in).nextInt();        for (int i = 1; i <= n; i++) System.out.println("*".repeat(i));    }}
C#Main.cs · 10 lines
using System; class Program{    static void Main()    {        int n = int.Parse(Console.ReadLine());        for (int i = 1; i <= n; i++) Console.WriteLine(new string('*', i));    }}
C++main.cpp · 8 lines
#include <iostream>#include <string> int main() {    int n;    std::cin >> n;    for (int i = 1; i <= n; i++) std::cout << std::string(i, '*') << '\n';}
Cmain.c · 11 lines
#include <stdio.h> int main(void) {    int n;    scanf("%d", &n);    for (int i = 1; i <= n; i++) {        for (int j = 0; j < i; j++) putchar('*');        putchar('\n');    }    return 0;}
Craykonmain.cray · 7 lines
n = Console.ReadLine().ToNumber();line = "";for (i = 1; i <= n; i++){    line = line + "*";    Console.WriteLine(line);}