forked from randerson112358/C-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgreedy.c
55 lines (41 loc) · 1.12 KB
/
greedy.c
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
/**
Description: This program uses a greedy algorithm to give the least amount of change back.
*/
#include <stdio.h>
//Define the function
void greedyAlgorithm(int c);
int main(void){
int changeOwed;
printf("How much change is owed (in cents) ? ");// Ask user input
scanf("%d", &changeOwed); //Get user input
greedyAlgorithm(changeOwed);// Run the greedy algorithm
system("pause");//Comment out if you are not using Windows OS
}
void greedyAlgorithm(int c){
int numQ=0, numD=0, numN=0, numP =0, count =0;
//Gets all of the possible number of quarters ($0.25) as change
while(c >= 25){
numQ++;
c = c - 25;
}
//Gets all of the possible number of dimes ($0.10) as change
while(c >= 10){
numD++;
c = c - 10;
}
//Gets all of the possible number of nickels ($0.05) as change
while(c >= 5){
numN++;
c = c - 5;
}
//Gets all of the possible number of pennies ($0.01) as change
while(c >= 1){
numP++;
c = c - 1;
}
printf("Please give the user the following: \n");
printf("%d Quarters \n", numQ);
printf("%d Dimes \n", numD);
printf("%d Nickels\n", numN);
printf("%d Pennies \n", numP);
}