-
Notifications
You must be signed in to change notification settings - Fork 2
/
Cash.c
95 lines (81 loc) · 2.16 KB
/
Cash.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <cs50.h>
#include <stdio.h>
int get_cents(void);
int calculate_quarters(int cents);
int calculate_dimes(int cents);
int calculate_nickels(int cents);
int calculate_pennies(int cents);
int main(void)
{
//ask how much change the customer is owed:
int cents = get_cents();
//Calculate the no. of quarters to be given to the customer:
int quarters = calculate_quarters(cents);
cents = cents - quarters * 25;
//Calculate the no. of dimes to be given to the customer:
int dimes = calculate_dimes(cents);
cents = cents - dimes * 10;
//Calculate the no. of nickels to be given to the customer:
int nickels = calculate_nickels(cents);
cents = cents - nickels * 5;
//Calculate the no. of pennies to be given to the customer:
int pennies = calculate_pennies(cents);
cents = cents - pennies * 1;
//To find the sum total no. of coins:
int coins = quarters + dimes + nickels + pennies;
//To print the total no. of coins:
printf("Total number of coins: %i\n", coins);
//To print the total no. of each of types of coins:
printf(" Quarters : %i\n Dimes : %i\n Nickels : %i\n Pennies : %i\n", quarters, dimes, nickels, pennies);
}
int get_cents(void)
{
//Get the amount of change owed to the customer:
int change
do
{
change = get_int("Change owed for? ");
}
while (change < 0);
return change; //Returns the value in cents
}
int calculate_quarters(int cents)
{
//Divide cents by highest possible value of quarters
int q;
for (q=0; cents >= 25; q++)
{
cents = cents - 25;
}
return q; //q value returns the number of highest quarters
}
int calculate_dimes(int cents)
{
//Divide cents by highest possible value of dimes
int d;
for (d=0; cents >= 10; d++)
{
cents = cents - 10;
}
return d; //d value returns the no. of dimes
}
int calculate_nickels(int cents)
{
//Divide cents by highest possible value of nickels
int n;
for (n=0; cents >= 5; n++)
{
cents = cents - 5;
}
return n; //n value return the no. of nickels
}
int calculate_pennies(int cents)
{
//Divide cents by highest possible number of pennies
int p;
for (p=0; cents >= 1; p++)
{
cents = cents - 1;
}
return p; //p value gives the highest value of no. of pennies
}