Skip to content
ExamplesEspañolWorkspace →

Drawing Shapes

Draws a square and a triangle with asterisks.

Level
Intermediate
Topic:
Loops
Concepts:
Input & output
Para
Open in Codoritmo

Run, change and debug this example in your browser.

What you'll learn

Two nested Para loops walk through rows and columns. Escribir Sin Saltar keeps the asterisks on one line, and Escribir "" moves to the next one.

Code

shapes.psc · 19 lines
Algoritmo DibujarFiguras    Definir n, fila, columna Como Entero    Escribir "Tamaño de las figuras:"    Leer n    Escribir "Cuadrado:"    Para fila <- 1 Hasta n Hacer        Para columna <- 1 Hasta n Hacer            Escribir Sin Saltar "* "        FinPara        Escribir ""    FinPara    Escribir "Triángulo:"    Para fila <- 1 Hasta n Hacer        Para columna <- 1 Hasta fila Hacer            Escribir Sin Saltar "* "        FinPara        Escribir ""    FinParaFinAlgoritmo

Flowchart

Each block in the flowchart matches one instruction in the code.

Algoritmo DibujarFigurasAlgoritmo DibujarFigurasAlgoritmo DibujarFigurasDefinir n, fila, columna Como EnteroDefinir n, fila, columna ComoEnteroEscribir "Tamaño de las figuras:"Escribir "Tamaño de lasfiguras:"Leer nLeer nEscribir "Cuadrado:"Escribir "Cuadrado:"Para fila <- 1 Hasta n HacerPara fila <- 1 Hasta nPara columna <- 1 Hasta n HacerPara columna <- 1 Hasta nEscribir Sin Saltar "* "Escribir Sin Saltar "* "Escribir ""Escribir ""Escribir "Triángulo:"Escribir "Triángulo:"Para fila <- 1 Hasta n HacerPara fila <- 1 Hasta nPara columna <- 1 Hasta fila HacerPara columna <- 1 Hasta filaEscribir Sin Saltar "* "Escribir Sin Saltar "* "Escribir ""Escribir ""FinAlgoritmoFinAlgoritmo

Sample run

This is what the program shows when it runs. The answers typed by the user are highlighted.

Tamaño de las figuras:User answer: 4Cuadrado:* * * * * * * * * * * * * * * * Triángulo:* * * * * * * * * * 

Challenge

Draw an upside-down triangle: n asterisks on the first row and one on the last.

Show hint

Make the outer Para count backwards with Con Paso -1.

Open in Codoritmo
  • Times Table

    Prints the multiplication table for the number you choose.

    Beginner
  • Store Inventory

    Records the stock of each product and flags what to restock.

    Intermediate
  • Bubble Sort

    Sorts random numbers by swapping neighbors, and stops as soon as they're in order.

    Intermediate