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

Added Easylang
mNo edit summary
(Added Easylang)
 
(8 intermediate revisions by 6 users not shown)
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>
 
Line 345 ⟶ 385:
Igual que la entrada de FreeBASIC.
</pre>
 
 
=={{header|C}}==
<syntaxhighlight lang="c">
#include <stdio.h>
 
#define TOTAL 988
#define Q_VALUES 8
 
int main() {
const int kValues[Q_VALUES] = { 200, 100, 50, 20, 10, 5, 2, 1 };
int t, q, iv;
 
for( t=TOTAL, iv=0; iv<Q_VALUES; t%=kValues[iv], ++iv ) {
q = t/kValues[iv];
printf( "%4d coin%c of %4d\n", q, q!=1?'s':' ', kValues[iv] );
}
 
return 0;
}
</syntaxhighlight>
{{out}}
<pre>
4 coins of 200
1 coin of 100
1 coin of 50
1 coin of 20
1 coin of 10
1 coin of 5
1 coin of 2
1 coin of 1
</pre>
 
 
=={{header|Delphi}}==
Line 422 ⟶ 495:
 
 
 
=={{header|EasyLang}}==
<syntaxhighlight>
sum = 988
coins[] = [ 200 100 50 20 10 5 2 1 ]
#
for coin in coins[]
n = sum div coin
if n > 0
print "coins of " & coin & ": " & n
sum -= n * coin
.
.
</syntaxhighlight>
{{out}}
<pre>
coins of 200: 4
coins of 100: 1
coins of 50: 1
coins of 20: 1
coins of 10: 1
coins of 5: 1
coins of 2: 1
coins of 1: 1
</pre>
 
=={{header|F_Sharp|F#}}==
Line 681 ⟶ 779:
1 * 2
1 * 1</pre>
 
=={{header|J}}==
<syntaxhighlight lang="j">coins=. [ (,: {."1@}:) [: (#: {:)/\. 0 ,. ,
 
1 2 5 10 20 50 100 200 coins 988</syntaxhighlight>
{{out}}
<pre>1 2 5 10 20 50 100 200
1 1 1 1 1 1 1 4</pre>
 
=={{header|JavaScript}}==
Line 869 ⟶ 975:
(1, (2, 1))
(0, (1, 1))
</pre>
 
=={{header|Lua}}==
{{Trans|Wren}}
<syntaxhighlight lang="lua">
do -- find the minimum number of coins needed to make a given value
-- translated from the Wren sample
 
local denoms = { 200, 100, 50, 20, 10, 5, 2, 1 }
local amount = 988;
local coins, remaining = 0, amount
print( "The minimum number of coins needed to make a value of "..amount.." is as follows:" )
for _, denom in pairs( denoms ) do
local n = math.floor( remaining / denom )
if n > 0 then
coins = coins + n
print( string.format( "%6d", denom ).." x "..n )
remaining = remaining % denom
end
if remaining == 0 then break end
end
print( "A total of "..coins.." coins in all." )
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>
 
Line 1,085 ⟶ 1,227:
1 * 2
1 * 1</pre>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="Quackery"> [ ' [ 200 100 50 20 10 5 2 1 ] ] is coins ( --> [ )
 
[ [] swap
coins witheach
[ /mod dip join ]
drop
witheach
[ dup 0 > iff
[ echo say " * "
coins i^ peek echo
cr ]
else drop ] ] is task ( n --> )
 
' [ 988 345 1024 ]
witheach
[ say "To make "
dup echo say ":" cr
task cr ]</syntaxhighlight>
 
{{out}}
 
<pre>To make 988:
4 * 200
1 * 100
1 * 50
1 * 20
1 * 10
1 * 5
1 * 2
1 * 1
 
To make 345:
1 * 200
1 * 100
2 * 20
1 * 5
 
To make 1024:
5 * 200
1 * 20
2 * 2
</pre>
 
=={{header|Raku}}==
Line 1,248 ⟶ 1,435:
</pre>
 
 
=={{header|RPL}}==
{{works with|HP|48G}}
« { 200 100 50 20 10 5 2 1 } { }
→ coinset result
« 1 coinset SIZE '''FOR''' j
coinset j GET
MOD LASTARG / IP
'result' SWAP STO+
'''NEXT'''
DROP result DUP ∑LIST "coins" →TAG
» » '<span style="color:blue">COINS</span>' STO
 
988 <span style="color:blue">COINS</span>
{{out}}
<pre>
2: { 4 1 1 1 1 1 1 1 }
1: coins: 11
</pre>
 
=={{header|Rust}}==
Line 1,326 ⟶ 1,532:
{{libheader|Wren-fmt}}
As there is, apparently, an unlimited supply of coins of each denomination, it follows that any amount can be made up.
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var denoms = [200, 100, 50, 20, 10, 5, 2, 1]
1,983

edits