indian coins
import java.util.*;
public class IndianCoins {
public static void main(String[] args) {
Integer coins[] = { 1, 2, 5, 10, 20, 50, 100, 500, 2000 };
// AS THE ORDER GIVEN IS IN ASCENDING ORDER ,WE REVERSE TO GET THE DESCENDING
// ORDER
Arrays.sort(coins, Comparator.reverseOrder());
int count = 0;
System.out.println("Enter amount:");
Scanner sc = new Scanner(System.in);
int amount = sc.nextInt();
ArrayList<Integer> ans = new ArrayList<>();
for (int i = 0; i < coins.length; i++) {
if (coins[i] <= amount) {
while (coins[i] <= amount) {
count++;
ans.add(coins[i]);
amount -= coins[i];
}
}
}
System.out.println("Minimum No.of coins required: " + count);
for (int i = 0; i < ans.size(); i++) {
System.out.print(ans.get(i) + " ");
}
System.out.println();
}
}
Comments
Post a Comment