Question

Write a C program that calculates exact change. In order to receive full credit, please remember...

Write a C program that calculates exact change. In order to receive full credit, please remember that only int arithmetic is exact, so you’ll need to break up your double into two ints, the one before the decimal point and the one after the decimal point. Another point worth mentioning is that the % operator gives the remainder. In other words, when working with int values, 9 / 5 = 1 whereas 9 % 5 = 4. Keep this in mind when you see the sample values shown below.

I have this program and it works for all the examples but when I inter sale price of 25.01 and total payment of $50; the curect answer for this example is 24 dollars, 3 quarters, 2 dimes, 0 nickels and 4 pennies but the code that I have gives me this answer

24 dollars, 3 quarters, 2 dimes, 0 nickels and 3 pennies

Please check my program hopefully we can fix it .

#include <stdio.h>
#include <stdlib.h>


int main() {
  
//Declare variables
float userNUmber;
int change, quarters, dimes, nickels, pennies;

//Prompt and read the amount as an input from the user
//printf("Enter the sales price: ");
float sales;
scanf("%f",&sales);
//printf("Enter the cash paid: ");
float cash;
scanf("%f",&cash);
userNUmber=cash-sales;
//printf("Here's your change: %.2f\n",userNUmber);
//for Calculate the number of quarters

//this part is to Convert float to int data type for change variable
int dollars=userNUmber;
userNUmber=userNUmber-(float)dollars;
change = (userNUmber*100);
quarters = change / 25;

// Calculate remaining change needed
change = change % 25;

//to Calculate the number of dimes
dimes = change / 10;

// to Calculate remaining change needed
change = change % 10;

//to Calculate the number of nickels
nickels = change / 5;

// part for Calculate pennies
change = change%100;
pennies = change %5;

//int penn
//pennies = penn;
//penn = change%5;

//for Display the amount of coins in quarters, dime, nickle and pennies
printf("%d dollars, %d quarters, %d dimes, %d nickels and %d pennies\n",dollars,quarters,dimes,nickels,pennies);


return (0);
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>
#include <stdlib.h>
int main() {
//Declare variables
double userNUmber;
int change, quarters, dimes, nickels, pennies;
//Prompt and read the amount as an input from the user
//printf("Enter the sales price: ");
float sales;
scanf("%f",&sales);
//printf("Enter the cash paid: ");
float cash;
scanf("%f",&cash);
userNUmber=cash-sales;
//printf("Here's your change: %.2f\n",userNUmber);
//for Calculate the number of quarters

//this part is to Convert float to int data type for change variable
int dollars=userNUmber;
userNUmber=userNUmber-(float)dollars;
userNUmber = round(userNUmber *100);
change = userNUmber;
quarters = change / 25;

// Calculate remaining change needed
change = change % 25;

//to Calculate the number of dimes
dimes = change / 10;

// to Calculate remaining change needed
change = change % 10;
//to Calculate the number of nickels
nickels = change / 5;
////// part for Calculate pennies
change = change%100;
pennies = change %5;

//////for Display the amount of coins in quarters, dime, nickle and pennies
printf("%d dollars, %d quarters, %d dimes, %d nickels and %d pennies\n",dollars,quarters,dimes,nickels,pennies);
return (0);
}

OUTPUT :

when you were getting the userNUmber as it is float while subtracting userNUmber from dollar it 98.987 due to floating point precision and while you were converting that directly to int . so change change became 98 instead of 99 what it should be .

So, before assigning userNUmber to change we should do:

userNUmber = round(userNUmber *100);

and then assign it to change after rounding-off.
change = userNUmber;

Add a comment
Know the answer?
Add Answer to:
Write a C program that calculates exact change. In order to receive full credit, please remember...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I made this C program to count change and make a slip saying the exact dollar...

    I made this C program to count change and make a slip saying the exact dollar and change amount given. every time I run the program it says the change given is 477256577 and i'm not sure what I have done wrong? #include<stdio.h> int main(void) { char first, last; int pennies; int nickels; int dimes; int quarters; int loonies; int change; int t_dollars; int t_cents; printf("Type in your 2 initials and press return> "); scanf("%c%c",&first,&last); printf("%c%c please enter in your...

  • (In Python 3) Write a program with total change amount as an integer input, and output...

    (In Python 3) Write a program with total change amount as an integer input, and output the change using the fewest coins, one coin type per line. The coin types are Dollars, Quarters, Dimes, Nickels, and Pennies. Use singular and plural coin names as appropriate, like 1 Penny vs. 2 Pennies. Ex: If the input is: 0 (or less than 0), the output is: No change Ex: If the input is: 45 the output is: 1 Quarter 2 Dimes So...

  • Describe how you would create a Python program that calculates the exact amount of change to...

    Describe how you would create a Python program that calculates the exact amount of change to give a customer in denominations of quarters, dimes, nickels, and pennies. For example, if the change is 79 cents, how many of each coin would be needed to give change?

  • it c++ coding. please write on atom software. 1. Write a program that converts dollars into coins a. Request a dolla...

    it c++ coding. please write on atom software. 1. Write a program that converts dollars into coins a. Request a dollar amount as an integer: $5.34 is input as 534 b. Use a constant variable to represent each coin as a fixed value: const int NICKEL 5; c. Use division and the mod function to calculate the number of each coin. Change Calculator Enter dollar amount (as an integer): $534 The equivalent in coins: 21 Quarters 0 Dimes 1 Nickels...

  • C Program that prompts the user to enter a number that represents a pile of pennies....

    C Program that prompts the user to enter a number that represents a pile of pennies. Then, the program should use that input value to distribute the cents over monetary denominations of dollars, half dollars, quarters, dimes, nickels and pennies. Distribute the pennies over the other denominations with Change function :    prototype: void Change(int *dollars, int *halfDollars, int *quarters, int *dimes, int *nickels, int *pennies); The function takes a logical input value via its pennies parameter that represents the total...

  • C++ Write a program that helps a young student learn to make change. Use the random...

    C++ Write a program that helps a young student learn to make change. Use the random number generator to generate change amounts between1¢ and 99¢. Ask the user how many quarters, dimes, nickels and pennies they need to make that amount. Determine whether the coins specified by the user total the specified amount and give them feedback on their answer including whether they used the minimum or optimum number of coins to produce the amount. Sample output of a program...

  • Construct a program in C++ that exchanges coins for cash like Coinstar that is located in...

    Construct a program in C++ that exchanges coins for cash like Coinstar that is located in several grocery stores. You will prompt the user to provide you with the total number of pennies, nickels, dimes, and quarters, respectively.   You are to determine the total amount of money entered by the user and then output the cash that should be received. For example, if a user enters 50 pennies, 10 dimes, 10 nickels, and 5 quarters, then the user would receive...

  • HELP ( i get these comment for this assignment please help me to fixed Please do...

    HELP ( i get these comment for this assignment please help me to fixed Please do not ask user to enter cents.  Amount due and received should be in dollars such as 2.35 and 5.) Business P2.8Giving change. Implement a program that directs a cashier how to give change. The program has two inputs: the amount due and the amount received from the customer. Display the dollars, quarters, dimes, nickels, and pennies that the customer should receive in return. #include <iostream>...

  • Vending Machine (Python 3) Thinking Outside the box Write a program that asks the user to...

    Vending Machine (Python 3) Thinking Outside the box Write a program that asks the user to enter a bill value (1 = $1 bill, 5 = $5 bill, etc.) and the price of an item they want to buy in pennies and calculate their change amount in dollars, quarters, dimes, nickels, and pennies. Your code should start like this: ## # This program simulates a vending machine that gives change # Define constants PENNIES PER DOLLAR = 100 PENNIES PER...

  • Your program must meet the following specifications: 1. At program start, assume a stock of 10 nickels, 10 dimes, 10...

    Your program must meet the following specifications: 1. At program start, assume a stock of 10 nickels, 10 dimes, 10 quarters, and 10 pennies. 2. Repeatedly prompt the user for a price in the form xX.xx, where X denotes a digit, or to enter q' to quit 3. When a price is entered a. If the price entered is negative, print an error message and start over requesting either a new price or to quit (indicated by entering a 'q)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT