Skip to content

Commit 6a27bcb

Browse files
committed
Create greedy.c
1 parent c5d970e commit 6a27bcb

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

pset1/greedy.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <stdio.h>
2+
#include <cs50.h>
3+
#include <math.h>
4+
5+
int main(void)
6+
{
7+
//Variables declaration
8+
float input = 0;
9+
printf("O hai! ");
10+
11+
//Ask user for input
12+
do
13+
{
14+
printf("How much change is owned?\n");
15+
input = GetFloat();
16+
}while (input < 0);
17+
18+
19+
//Process input
20+
int count = 0; //to count how many coins
21+
22+
//Process dollars
23+
input = input * 100;
24+
int cinput = (int) round(input);
25+
26+
// Process cents
27+
do
28+
{
29+
if (cinput >= 25) cinput -= 25;
30+
else if (cinput < 25 && cinput >= 10) cinput -= 10;
31+
else if(cinput < 10 && cinput >= 5) cinput -= 5;
32+
else if (cinput < 5 && cinput > 0) { cinput -= 1; };
33+
count++;
34+
35+
}while(cinput > 0);
36+
37+
// Print number of coins
38+
printf("%d\n", count);
39+
}

0 commit comments

Comments
 (0)