So I'm making a change program! Exciting!
I've only learned how to use loops... And if else statements.
So I used a lot of if/else statements in my code and was hoping there's a more efficient way to do this. I'm supposed to print out the change breakdown of the given amount. like so...
34.67 entered
1 20.00
1 10.00
0 5.00
4 1.00
2 .25
1 .10
1 .05
2 .01
I just am trying to understand loops better without having to use so many if/else statements. I don't want to get stuck into a habit of just using that all the time... Here's my code...
Code:
#include <stdio.h>
int main (void)
{
//Declare Variables
int twenty,ten,five,one, dime, nickle, penny;
double dollar_amt, cents;
//Have User Enter An Amount
printf("Please enter an amount: ");
scanf("%lf", &dollar_amt);
//Check if user has correct amount entered, if not have them re-enter it
while (dollar_amt > 100.00 || dollar_amt < 0)
{
printf("Please input an amount no more than 100.00: ");
scanf("%lf", &dollar_amt);
}
//Print the amount entered
printf("The amount entered is %2.2lf\n", dollar_amt);
twenty = 20;
if (dollar_amt > 20)
{
(int)dollar_amt % twenty;
}
else
{
printf("\n");
}
ten = 10;
if (dollar_amt > 10)
{
(int)dollar_amt % ten;
}
else
{
printf("\n");
}
five = 5;
if (dollar_amt > 5)
{
(int)dollar_amt % five;
}
else
{
printf("\n");
}
one = 1;
if (dollar_amt > 1)
{
(int)dollar_amt % 1;
}
printf("\n");
//End Program
return 0;
}