621 views
4 votes
Write a c program with total change amount a an integer input, and output the change uing the fewet coin, one coin type per line. The coin type are Dollar, Quarter, Dime, Nickel, and Pennie. Ue ingular and plural coin name a appropriate, like 1 Penny v. 2 Pennie

1 Answer

2 votes

#include <stdio.h>

int main() {

int change; // variable to store the total change amount

int coin_values[] = {100, 25, 10, 5, 1}; // array to store the coin values (in cents)

const char *coin_names[] = {"Dollar", "Quarter", "Dime", "Nickel", "Penny"}; // array to store the coin names

int coin_counts[5]; // array to store the number of each coin type

printf("Enter the total change amount: ");

scanf("%d", &change);

// Loop through the coin_values array to determine the number of each coin type

for (int i = 0; i < 5; i++) {

coin_counts[i] = change / coin_values[i]; // integer division to determine the number of coins

change = change % coin_values[i]; // modulo to determine the remaining change

}

// Loop through the coin_counts array to print the number of each coin type

for (int i = 0; i < 5; i++) {

if (coin_counts[i] == 1) { // if the number of coins is 1, print the singular coin name

printf("%d %s\\", coin_counts[i], coin_names[i]);

} else if (coin_counts[i] > 1) { // if the number of coins is greater than 1, print the plural coin name

printf("%d %ss\\", coin_counts[i], coin_names[i]);

}

}

return 0;

}

User Gimboland
by
7.5k points