Find minimum number of coins that make a given value: Difference between revisions

Added Algol 68
(→‎J: without side effects)
(Added Algol 68)
Line 84:
1 x 2
1 x 1
</pre>
 
=={{header|ALGOL 68}}==
{{Trans|Wren}}
<syntaxhighlight lang="algol68">
BEGIN # find the minimum number of coins needed to make a given value #
# translated from the Wren sample #
 
[]INT denoms = ( 200, 100, 50, 20, 10, 5, 2, 1 );
INT coins := 0;
INT amount = 988;
INT remaining := amount;
print( ( "The minimum number of coins needed to make a value of " ) );
print( ( whole( amount, 0 ), " is as follows:", newline ) );
FOR d pos FROM LWB denoms TO UPB denoms
WHILE INT denom = denoms[ d pos ];
INT n = remaining OVER denom;
IF n > 0 THEN
coins +:= n;
print( (" ", whole( denom, -3 ), " x ", whole( n, 0 ), newline ) );
remaining MODAB denom
FI;
remaining > 0
DO SKIP OD;
print( ( newline, "A total of ", whole( coins, 0 ), " coins in all.", newline ) )
END
</syntaxhighlight>
{{out}}
<pre>
The minimum number of coins needed to make a value of 988 is as follows:
200 x 4
100 x 1
50 x 1
20 x 1
10 x 1
5 x 1
2 x 1
1 x 1
 
A total of 11 coins in all.
</pre>
 
3,032

edits