Solved

Coin

Posted on 1998-02-12
5
288 Views
Last Modified: 2010-04-02
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
Comment
Question by:catalyst19002800
  • 3
  • 2
5 Comments
 
LVL 3

Accepted Solution

by:
q2guo earned 50 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

by:q2guo
ID: 1257240
Sorry, all the ifs should read

if (amount_user => 20) {

I forget the equal sign.
0
 

Author Comment

by:catalyst19002800
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

by:q2guo
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

by:catalyst19002800
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops 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.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question