-
Notifications
You must be signed in to change notification settings - Fork 0
/
CurrencyPayment.java
55 lines (46 loc) · 1.77 KB
/
CurrencyPayment.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.util.*;
public class CurrencyPayment {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input the size of currency denominations
System.out.print("Enter the size of currency denominations: ");
int size = scanner.nextInt();
// Input the currency denominations values
System.out.println("Enter the currency denominations value:");
int[] denominations = new int[size];
for (int i = 0; i < size; i++) {
denominations[i] = scanner.nextInt();
}
// Input the amount to pay
System.out.print("Enter the amount you want to pay: ");
int amount = scanner.nextInt();
// Sort the denominations in descending order
Arrays.sort(denominations);
reverseArray(denominations);
// Calculate the minimum number of notes
int[] notesCount = new int[size];
for (int i = 0; i < size; i++) {
notesCount[i] = amount / denominations[i];
amount %= denominations[i];
}
// Output the payment approach
System.out.println("Your payment approach in order to give the minimum number of notes will be:");
for (int i = 0; i < size; i++) {
if (notesCount[i] > 0) {
System.out.println(denominations[i] + ":" + notesCount[i]);
}
}
}
// Helper method to reverse an array
private static void reverseArray(int[] arr) {
int start = 0;
int end = arr.length - 1;
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
}