forked from randerson112358/C-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgreedy_algorithm.c
More file actions
66 lines (54 loc) · 1.53 KB
/
Copy pathgreedy_algorithm.c
File metadata and controls
66 lines (54 loc) · 1.53 KB
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
56
57
58
59
60
61
62
63
64
65
66
/**
This programs uses the greedy algorithm to give the least
amount of change back.
Written in the C programming language
By: (randerson112358)
*/
# include <stdio.h>
int main(void)
{
int changeOwed;
int check;
char invisibleChar;
int count = 0;
int numQ=0, numD=0, numN=0, numP=0;
/***Run this loop until the user inputs a number and it is greater than or equal to 0***/
do{
printf("How much change is owed (in cents)?\n");
check = scanf("%d", &changeOwed); // returns 1 if the input is a number and returns 0 otherwise
//Get's rid of any extra invisible characters if the input is a char/String
do{
scanf("%c",&invisibleChar);
}while(invisibleChar != '\n');
}while(check == 0 || !(changeOwed >=0 ));
int c = changeOwed;// The variable c was only used to shorten my typing
//Continue to do this loop until
while(c > 0){
//Use as many quarters needed
while(c >= 25){
count ++;
numQ++;
c = c - 25;
}
//Use as many dimes needed
while(c >= 10){
count ++;
numD++;
c = c - 10;
}
//Use as many nickels needed
while(c >= 5){
count ++;
numN++;
c = c - 5;
}
//Use as many pennies needed
while(c >= 1){
count ++;
numP++;
c = c - 1;
}
}
printf("Quarters: %d, Dimes: %d, Nickels: %d, Pennies: %d\nNumber of coins used= %d\n\n", numQ, numD, numN, numP, count);
system("pause"); //Comment this out if you are not using windows operating system
}