Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Coin

Posted on 1998-02-12
Medium Priority
295 Views
A novice question. I have to ask the user to input float amount between 0.00 and \$100.00. Then printout number of TWENTIES, TENS, FIVES, ONES, quarters, dimes, nickels and pennies. Any help is appreciated. This is the way I approached it: (formatting from float to int is a problem).

main()
{
float amount_uner;
float amount_pennies;
scanf("d", &amount_user);
printf("Enter Dollar amount between \$0.00 and \$100.00"\n);
amount_pennies=amount_user*100;
if (amount_user >100.00 or <0.00)
printf("wrong, Please try inputing your request amount in the form of \$###.## \n");
if (amount_user>=2000)
{
Twenties=amount_user/2000;
Twenties=amount_user%2000;
Tens=  /* continure until we run out of money */

How about the user put in 0.25 or an amount less than \$20.00
0
Question by:catalyst19002800
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3
• 2

LVL 3

Accepted Solution

q2guo earned 200 total points
ID: 1257239
#include <stdio.h>

int main()
{
float bill[8] = {20, 10, 5, 1, 0.25, 0.1, 0.05, 0.01};
int billnum[8] = {0,0,0,0,0,0,0,0};
float amount_user;

printf("Enter Dollar amount between \$0.00 and \$100.00: ");
scanf("%f", &amount_user);

while (amount_user != 0)
{
if (amount_user > 20) {
amount_user -= 20;
billnum[0]++;
}
elseif (amount_user > 10) {
amount_user -= 10;
billnum[1]++;
}
elseif (amount_user > 5) {
amount_user -= 5;
billnum[2]++;
}

elseif (amount_user > 1) {
amount_user -= 1;
billnum[3]++;
}
elseif (amount_user > 0.25) {
amount_user -= 0.25;
billnum[4]++;
}
elseif (amount_user > 0.1) {
amount_user -= 0.1;
billnum[5]++;
}
elseif (amount_user > 0.05) {
amount_user -= 0.05;
billnum[6]++;
}
elseif (amount_user > 0.01) {
amount_user -= 0.01;
billnum[7]++;
}
else {
break;
}
}

printf("There are %d twenty dollar bills.\n", billnum[0]);
printf("There are %d ten dollar bills.\n"   , billnum[1]);
....
printf("There are %d one cents.\n"   , billnum[7]);
}
0

LVL 3

Expert Comment

ID: 1257240
Sorry, all the ifs should read

if (amount_user => 20) {

I forget the equal sign.
0

Author Comment

ID: 1257241
Although the solution is useful but I am not suppose to use arrarys in this problem. Is there alternatives solutions by using nested loops or Case staments: (Thanks so much, You are great. Once I get a chance, I will read more on the details of this site and see what I need to do to support it.
0

LVL 3

Expert Comment

ID: 1257242
I didn't test this program out. so, let me know if it works.

--------------------------------------------------------
#include <stdio.h>

int main()
{
float amount_user;
float i=20;
int a=0 , b=0 , c=0, d=0, e=0, f=0, g=0, h=0;

printf("Enter Dollar amount between \$0.00 and \$100.00: ");
scanf("%f", &amount_user);

while (amount_user != 0)
{
if ((amount_user - i ) >= 0 )  {
switch (amount_user - i) {
case (amount_user - 20) : a++;
case (amount_user - 10) : b++;
case (amount_user - 5) : c++;
case (amount_user - 1) : d++;
case (amount_user - 0.25) : e++;
case (amount_user - 0.1) : f++;
case (amount_user - 0.05) : g++;
case (amount_user - 0.01) : h++;
}

} else  {
switch (amount_user - i) {
case (amount_user - 20) : i = 10;
case (amount_user - 10) : i = 5;
case (amount_user - 5) : i = 1;
case (amount_user - 1) : i = 0.25;
case (amount_user - 0.25) : i = 0.1;
case (amount_user - 0.1) : i = 0.05;
case (amount_user - 0.05) : i = 0.01;
case (amount_user - 0.01) : break;
}
}
}
printf("There are %d twenty dollars.\n" , a);
....
printf("There are %d one cents.\n" , h);
}
0

Author Comment

ID: 1257243
/* Here is a modified version of your suggestion that I think is required. However; it does not work very good. Also I have to get the least number of bills and coins (another words larger denominations) */
/*Thanks*/

#include <stdio.h>

void main()
{
unsigned TWENTIES, TENS, FIVES, ONES, quarters, dimes, nickels, pennies;
float amount_user;

printf("Enter Dollar amount between \$0.00 and \$100.00:");
scanf("%d", &amount_user);
amount_user=amount_user*100;
printf("%d, amount_user\n");

while (amount_user !=0)
{
if(amount_user>=2000)
{
amount_user=amount_user/2000;

}
else if(amount_user>=1000)
{
amount_user-=1000;
TENS++;
}
else if(amount_user>=500)
{
amount_user-=500;
FIVES++;
}
else if(amount_user>=100)
{
amount_user-=100;
ONES++;
}
else if(amount_user>=25)
{
amount_user-=25;
quarters++;
}
else if(amount_user>=10)
{
amount_user-=10;
dimes++;
}
else if(amount_user>=05)
{
amount_user-=5;
nickels++;
}
else if(amount_user>=1)
{
amount_user-=1;
pennies++;
}
else
{
break;
}
}
printf("There are %d TWENTIES\n", TWENTIES);
printf("There are %d TENS\n", TENS);
printf("There are %d FIVES\n", FIVES);
printf("There are %d ONES\n", ONES);
printf("There are %d quarters\n", quarters);
printf("There are %d dimes\n", dimes);
printf("There are %d nickels\n", nickels);
printf("There are %d pennies\n", pennies);
}

0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
###### Suggested Courses
Course of the Month4 days, 8 hours left to enroll