Thread: price + sales tax calculator

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    1

    price + sales tax calculator

    I am trying to make a simple program that allow you to enter the value of an item, then adds on a sales tax of 5.6 percent and outputs in the following format:"Item price of $10.00 with sales tax is 10 dollars and 56 cents".

    I have made a program so far that calculates the sales tax, adds it to the item price and then gives a resulting floating point number. However, I do not know how to display the result in the format above. Here is what I have written so far. Any help would be great!

    Code:
    #include <stdio.h>
    #define TAXRATE .056
    int main(void)
    {
           float item, tax, total_cost;
           
           printf("Enter the value of your item\n");
           scanf(" %f", &item);
           tax = item * TAXRATE;
           total_cost = item + tax;
     
           printf("Your item of $%.2f with sales tax is %.2f", item, total_cost);
    
           getchar();
           return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Possible way; note, I normally use the math floor function instead; not sure this will work.

    Untested code.
    Code:
    int dollar_cost;
    
    dollar_cost = (int)total_cost; // needs tested with value above x.50 to confirm it does not round up (in the K&R C is was not always the same)
    printf("Your item of $%.2f with sales tax is %.2f dollars %d", item, total_cost, dollar_cost);
    Tim S.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    One solution involves "typecasting," or changing floats to integers. While there are probably better ways of accomplishing this, here is one that juggles the floating point result and typecasts it into integers to get the desired values:

    Code:
    #include <stdio.h>
    #define TAXRATE .056
    int main(void)
    {
           float item, tax, total_cost;
           int dollars,cents;
    
           printf("Enter the value of your item\n");
           scanf(" %f", &item);
           tax = item * TAXRATE;
           total_cost = item + tax;
    
           // truncate value to whole number (remove "cents") by typecasting to an integer
           dollars = (int)total_cost;
           // subtract "dollars" from total cost, multiply "cents" by 100 to get this value
           //   as an integer, and typecast it to an integer
           cents = (int)((total_cost - dollars)*100);
    
           printf("Your item of $%.2f with sales tax is %.2f", item, total_cost);
    
           // desired output
           printf("\nItem is %d dollars and %d cents\n",dollars,cents);
    
           getchar();
           return 0;
    Last edited by Matticus; 07-14-2011 at 01:44 PM.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by beginnerc View Post
    I am trying to make a simple program that allow you to enter the value of an item, then adds on a sales tax of 5.6 percent and outputs in the following format:"Item price of $10.00 with sales tax is 10 dollars and 56 cents".

    I have made a program so far that calculates the sales tax, adds it to the item price and then gives a resulting floating point number. However, I do not know how to display the result in the format above. Here is what I have written so far. Any help would be great!

    Code:
    #include <stdio.h>
    #define TAXRATE .056
    int main(void)
    {
           float item, tax, total_cost;
           
           printf("Enter the value of your item\n");
           scanf(" %f", &item);
           tax = item * TAXRATE;
           total_cost = item + tax;
     
           printf("Your item of $%.2f with sales tax is %.2f", item, total_cost);
    
           getchar();
           return 0;
    }
    Here's a little hint for you... Floating point math is not exact math. When working with money, it is often best to work in pennies using integers rather than in dollars using floating points.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sales tax
    By Mark_Guy in forum C Programming
    Replies: 0
    Last Post: 10-22-2008, 08:47 AM
  2. Two-dimensional array Sales
    By mikeprogram in forum C++ Programming
    Replies: 7
    Last Post: 12-04-2005, 10:32 PM
  3. Understanding the traveling sales algorithm
    By Axel in forum C Programming
    Replies: 22
    Last Post: 10-22-2005, 10:48 AM
  4. donut sales take a dive
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 07-03-2004, 01:34 PM

Tags for this Thread