Solved

Coin

Posted on 1998-02-12
5
284 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

Free book by J.Peter Bruzzese, Microsoft MVP

Are you using Office 365? Trying to set up email signatures but you’re struggling with transport rules and connectors? Let renowned Microsoft MVP J.Peter Bruzzese show you how in this exclusive e-book on Office 365 email signatures. Better yet, it’s free!

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

867 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now