Course of Raku / Essentials / Loops / Exercises / Squares and cubes in a loop

Solution: Squares and cubes in a loop

Code

Here is the code of the solution. The for loop is using a range that spans from -5 to 5.

for -5 .. 5 -> $n {
    say "$n\t{$n²}\t{$n³}";
}

🦋 Find the program in the file squares-and-cubes-loop.raku.

Example

Run the program and check the result.

$ raku exercises/loops/squares-and-cubes-loop.raku
-5	25	-125
-4	16	-64
-3	9	-27
-2	4	-8
-1	1	-1
0	0	0
1	1	1
2	4	8
3	9	27
4	16	64
5	25	125

Next exercise

💪 Factorial

Course navigation

Conditional checks / Ternary operator   |   Data type conversion

Translations of this page: EnglishDeutschEspañolItalianoLatviešuNederlandsБългарскиРусскийУкраїнська