#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Simple tax program


    Pretty simple program.
    The program runs fine.
    Do you think the program is acceptable considering the parameters set by the instructions?

    Create a small program based on the following

    // encourage the use of user-defined functions
    // (calculates different sales tax based on an item price)

    1. Ask the user to enter the price of an item.
    2. Get the item price from the user.
    3. Calculate the sales tax of the item:
    - 7% if the item costs less than $200.
    - 5% if the item costs $200 or more.
    4. Display the original item price and sales tax amount onto the screen.
    5. Calculate new cost of the item with the sales tax.
    6. Display the total price of the item onto the screen.


    Code:
    #include<stdio.h>
    #include<math.h>
    
    
    double getPrice(void);
    
    int main(void){
    	
    	double price = 0.0;	
    	double sevenP = 0.0;
    	double fiveP = 0.0;
    
    	printf("PLease enter the price of the customers purchase:\t$");
    		price = getPrice();
    	printf("The price is:\t $ %.2lf\n", price);
    	
    		sevenP = price * .07;
    		fiveP = price * .05;
    	
    		if (price < 200){
    	printf("The federal tax is: $ %.2lf\n", sevenP);
    	printf("The price plus the federal tax is: $ %.2lf\n", sevenP + price);
    		}else{
    	printf("The state tax is: $ %.2lf\n", fiveP);
    	printf("The price plus the state tax is: $ %.2lf\n", fiveP + price);
    		}
    	return 0;
    }
    double getPrice(void){
    
    	int value = 0;
    		scanf_s(" %d", &value);
    		return value;
    }
  2. #2
  3. No Profile Picture
    Still Learning
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Montreal, Canada
    Posts
    55
    Rep Power
    40
    It looks ok.
    Subtle efficiency point. You calculate both rates of tax.
    If you move the tax caculation into the if that tests for amount it is
    technically more efficient. Also you would declare one less variable called tax.
  4. #3
  5. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo