Course of Raku / Essentials / Positional data types / Exercises / Square and cube

Solution: Square and cube

Code 1

A straightforward solution is to use the ** operator.

my $n = @*ARGS[0];
say $n ** 2;
say $n ** 3;

🦋 Find the program in the file square-and-cube.raku.

Code 2

A slightly more stylish solution is to use Unicode superscripts.

my $n = @*ARGS[0];
say $n²;
say $n³;

🦋 Find the program in the file square-and-cube-2.raku.

Output

Run the program a few times and test it with different numbers.

$ raku exercises/positionals/square-and-cube.raku 5
25
125

Next exercise

💪 Traffic lights

Course navigation

Typed variables / Allomorphs   |   Associative data types

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