Course of Raku / Essentials / Numbers / Exercises / Sum of numbers

Solution: Sum of numbers

Here is the solution to the problem.

Code

my $a = prompt 'Enter number A: ';
my $b = prompt 'Enter number B: ';
my $c = prompt 'Enter number C: ';

my $sum = $a + $b + $c;

say "The sum of numbers is $sum.";

🦋 You can find the full code in the file sum-of-numbers.raku.

Output

Run the program a few times and confirm it works with the numbers of different kinds.

$ raku exercises/numbers/sum-of-numbers.raku
Enter number A: 1.2
Enter number B: -3.4
Enter number C: 45
The sum of the numbers is 42.8.

Comments

Notice that we are directly using the value returned by the prompt routine. That may cause problems if the user enters a string that does not represent a value. We can make the program better in the future after we learn about exceptions.

Next exercise

💪 Class desks

Course navigation

Strings / String length   |   Boolean type