Solved

Coin

Posted on 1998-02-12
5
291 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
[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
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Want to delete all my personal data 13 156
Line meaning 9 97
smtp c source code 7 132
Synchronization using condition variables to avoid locking in C 8 104
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

734 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