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++ 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...

  • 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)...

  • in c++ please tortViewHistory Bookmarks Window Help learn Tybooks.com 3.12. LAB: Exact change functions My Ibrary...

    in c++ please tortViewHistory Bookmarks Window Help learn Tybooks.com 3.12. LAB: Exact change functions My Ibrary > CS 010: Introduction To Computer Science for Science, Mathematics & Engin. 8.12: LAB: Exact change - functions zyBooks catalog Help/FAQ Stephanie R Write a program with total change amount as an integer input that outputs 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...

  • write a PHP program makes change for a given number of cents. The program should ask...

    write a PHP program makes change for a given number of cents. The program should ask the user for the amount of cents and then output the change for specific denominations of ten dollar bills, five dollar bills, one dollar bills, quarters, dimes, nickels, and pennies. Here is a sample program output for an input of 265 cents: Change for 2 dollars and 65 cents : 0 ten dollar bills 0 five dollar bills 2 one dollar bills 2 quarters...

  • Can you please help me with creating this Java Code using the following pseudocode? Make Change C...

    Can you please help me with creating this Java Code using the following pseudocode? Make Change Calculator (100 points + 5 ex.cr.)                                                                                                                                  2019 In this program (closely related to the change calculator done as the prior assignment) you will make “change for a dollar” using the most efficient set of coins possible. In Part A you will give the fewest quarters, dimes, nickels, and pennies possible (i.e., without regard to any ‘limits’ on coin counts), but in Part B you...

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
ADVERTISEMENT