Skip to content

Commit ddf9898

Browse files
Exchange Money Calculator
1 parent 41778a9 commit ddf9898

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import java.util.*;
2+
3+
public class Exchange {
4+
public static void main(String[] args) {
5+
6+
Scanner sc = new Scanner(System.in);
7+
System.out.print("Enter the amount for Exchange: ");
8+
int V = sc.nextInt();
9+
exchanger(V);
10+
11+
sc.close();
12+
13+
}
14+
15+
public static void exchanger(int V) {
16+
int[] coins = { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 };
17+
int[] minCoins = new int[V + 1];
18+
int[] lastCoin = new int[V + 1];
19+
20+
for (int i = 1; i <= V; i++) {
21+
minCoins[i] = Integer.MAX_VALUE;
22+
for (int j = 0; j < coins.length; j++) {
23+
if (coins[j] <= i && minCoins[i - coins[j]] + 1 < minCoins[i]) {
24+
minCoins[i] = minCoins[i - coins[j]] + 1;
25+
lastCoin[i] = coins[j];
26+
}
27+
}
28+
}
29+
30+
System.out.println("Minimum number of coins: " + minCoins[V]);
31+
System.out.print("Coins: ");
32+
while (V > 0) {
33+
System.out.print(lastCoin[V] + " ");
34+
V -= lastCoin[V];
35+
}
36+
System.out.println();
37+
}
38+
}

0 commit comments

Comments
 (0)