Google Answers Logo
View Question
 
Q: C programming exercise answer ( Answered 4 out of 5 stars,   0 Comments )
Question  
Subject: C programming exercise answer
Category: Computers
Asked by: mandragora20-ga
List Price: $50.00
Posted: 08 Sep 2004 17:48 PDT
Expires: 08 Oct 2004 17:48 PDT
Question ID: 398630
I am trying to write a program which lets a user specify a tax
category (Single, head of household, married-joint, and
married-seperate) and a taxable income (single-15% of first $17,850
plus 28% of excess, head of household-15% of first $23,900 plus 28% of
excess, married-joint-15% of first 29,750 plus 28% of excess and
married-seperate-15% of first $14,875 plus 28% of excess) and then
calculates the tax. I am to use a loop so that the user can enter
several tax cases. I do not even know where to begin. PLEASE HELP!
Answer  
Subject: Re: C programming exercise answer
Answered By: leapinglizard-ga on 08 Sep 2004 19:37 PDT
Rated:4 out of 5 stars
 
Dear mandragora20,

Here's a fully commented C program for you to study.



#include<stdlib.h>
#include<stdio.h>
#include<string.h>
/* the above are required headers for standard libraries */

int main() {
    char entry[1000], code;         /* entry is a character buffer for input */
    double income, threshold, tax;  /* needed for calculations */

    printf("=== TAX CALCULATOR ===\n");     /* title */

    while (1) {   /* infinite loop: true until we break out of it */

        printf("--- tax categories ---\n"); /* display the codes */
        printf("s = single\n");
        printf("h = head of household\n");
        printf("m = married-joint\n");
        printf("n = married-separate\n");
        printf("\n");
        printf("Enter one of the above codes, or anything else to quit >");

        fgets(entry, 1000, stdin);          /* read a line of input */
        if (strlen(entry) != 2)   /* if it's not one character plus... */
            break;                /* ... a newline character, we quit */

        code = entry[0];          /* the code is the first character */

        if (!(code == 's' || code == 'S' || code == 'h' || code == 'H'
           || code == 'm' || code == 'M' || code == 'n' || code == 'N'))
            break;    /* if it's not a legal code, we quit */

        printf("Enter your income in dollars, without a comma >");
        fgets(entry, 1000, stdin); /* read another line */
        income = atof(entry);      /* convert into a floating-point number */

        if (code == 's' || code == 'S')   /* check the code (uppercase, too) */
            threshold = 17850.0;   /* set the threshold */

        else if (code == 'h' || code == 'H') /* otherwise, check another code */
            threshold = 23900.0;   /* appropriate threshold here */

        else if (code == 'm' || code == 'N') /* same thing again */
            threshold = 29750.0;

        else          /* if no other code matches, it must be 'n' or 'N' */
            threshold = 14875.0;

        if (income < threshold)    /* are we under the threshold?   */
            tax = 0.15*income;     /* then just apply the base rate   */

        else                       /* otherwise, apply the excess rate, too  */
            tax = 0.15*threshold + 0.28*(income-threshold);

        printf("-----------> You owe $%.2f in tax.\n\n", tax);

    }                        /* while loop returns to beginning */

    printf("\nGoodbye!\n");  /* if we broke out of the loop, we land here */
    return 0;                /* always return zero at the end of main() */
}



If you have any trouble compiling this program, please let me know.
Any questions that aren't answered by the comments, don't hesitate to
ask.

At your service,

leapinglizard
mandragora20-ga rated this answer:4 out of 5 stars
Thanks for this answer, it has helped me a great deal. I will return
in the future if I need help

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy