Course of Raku / Essentials / Variables and data types essentials / Numbers / Exercises / Apple sharing
Solution: Apple sharing
Here is the solution to the problem.
Code
my $N = 3;
my $K = 11;
my $gets = $K div $N;
my $remains = $K % $N;
say "Each person gets $gets apple(s).";
say "There are $remains apple(s) remaining.";🦋 You can find the full code in the file apple-sharing.raku.
Output
With the input numbers 3 and 11, the program prints the following output:
$ raku exercises/numbers/apple-sharing.raku
Each person gets 3 apple(s).
There are 2 apple(s) remaining.Modify the initial value of $N and $K to
model other situations:
$Kis multiple of$N, for example,12and3;$Kis equal to$N;$Kis less than$N.
Comments
In the program, we are using the two operators: div to
perform integer division and % for getting the remainder of
the division. Alternatively, you can make the calculations in the
following manner:
my $gets = $K div $N;
my $remains = $K - $N * $gets;Also note how the variables are interpolated in the string.
Course navigation
← Apple sharing | Sum of numbers →
Translations of this page: English • Deutsch • Español • Italiano • Latviešu • Nederlands • Български • Русский • Українська