Hey guys, im new to the forums, and sort of new to programming in c. Im currently in my second semester of college so I already have some programming knowledge. Our assignment is this:
Write a program in C that will allow a user to enter an amount of money, then display the least number of coins and the value of each coin required to make up that amount. The twist to this program is the introduction of a new coin – the jonnie (value = $1.26). Therefore, the coins available for your calculations are: Twonies, Loonies, Jonnies, Quarters, Dimes, Nickels and Pennies. You can assume the amount of money user enters will be less than $100.00.
User enters $4.53. Your output should be:
The least number of coins required to make up $4.53 is 4. The coins needed are:
1 Toonie $2.00
2 Jonnies $2.52
1 Penny $0.01
$4.53
I'm wondering if i'm close. I'm stuck on what to do for the output. This is what i have so far. Any input on making this shorter is greatly appreciated. We can only use #include <stdio.h>
[tag]
Code:
//Start
#include <stdio.h>
int main()
{
//Get user input for amount of money
//Input
int i;
//Store input from user
printf("Please enter an amount to find the least number of coins to\nmake up your value: ");
scanf("%.2d",i);
//Coin values avail for use
int t;
int j;
int l;
int q;
int d;
int n;
int p;
int total_coins;
//Calc the least amount of coins for an input < $100.00
for (i = 0; i < 100;)
{
if (i >= 2)
{
t = i/2;
j = i % t / 1.26;
l = i % j / 1;
q = i % l / 0.25;
d = i % q / 0.1;
n = i % d / 0.05;
p = i % n / 0.01;
total_coins = t + j + l + q + d + n + p;
getchar();
}
else if ((i < 2) && (i >= 1.26))
{
j = i % t / 1.26;
l = i % j / 1;
q = i % l / 0.25;
d = i % q / 0.1;
n = i % d / 0.05;
p = i % n / 0.01;
total_coins = j + l + q + d + n + p;
getchar();
}
else if ((i < 1.26) && (i >= 1.00))
{
l = i % j / 1;
q = i % l / 0.25;
d = i % q / 0.1;
n = i % d / 0.05;
p = i % n / 0.01;
total_coins = l + q + d + n + p;
getchar();
}
else if ((i < 1.00) && (i >= 0.25))
{
q = i % l / 0.25;
d = i % q / 0.1;
n = i % d / 0.05;
p = i % n / 0.01;
total_coins = q + d + n + p;
getchar();
}
else if (( i < 0.25) && (i >= 0.10))
{
d = i % q / 0.1;
n = i % d / 0.05;
p = i % n / 0.01;
total_coins = d + n + p;
getchar();
}
else if ((i < 0.10) && (i >= 0.05))
{
n = i % d / 0.05;
p = i % n / 0.01;
total_coins = n + p;
getchar();
}
else if ((i < 0.05) && (i >= 0.01))
{
total_coins = i / 0.01;
getchar();
}
}
return 0;
}
[/tag]