Home > Blockchain >  Tax bracket calculator self assignment
Tax bracket calculator self assignment

Time:11-09

My assignment was to create a tax calculator in whatever way possible initially with paying tax excluding tax brackets. How would I adjust this code to use Loops instead or how could I make it more efficient?

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

int main()
{
    double earnings;    //user income
    double tax;         //sum of total tax

    printf("=====================<><>TAX CALCULATOR (INC TAX BRACKETS)<><>=====================\n How much have you earned: \n");
    scanf("%lf", &earnings);
    double tax1 = earnings -12000;      //seperating each tax bracket
    double tax2 = earnings - 20000;
    double tax3 = earnings - 40000;
    double tax4;
    double tax5;
    if (earnings >= 12000 && earnings <=19999){
        tax = (tax1*0.1);
        printf("\nYou've made: %0.2f", earnings - tax);
        printf("\nPaying: %0.2f in tax", tax);
    }
    else if(earnings >= 20000 && earnings <=39999){
        //printf("\n============tax2: %0.2f============\n",tax2);
        //printf("\n============Tax2: %0.2f============\n",tax2*0.15);
        tax = (tax1*0.1   tax2*0.15);
        printf("\nYou've made: %0.2f", earnings - tax);
        printf("\nPaying: %0.2f in tax", tax);
    }
    else if(earnings >= 40000){
        tax = (tax1*0.1   tax2*0.15   tax3*0.2);
        //printf("\n============tax3: %0.2f============\n",tax3);
        //printf("\n============Tax3: %0.2f============\n",tax3*0.2);
        printf("\nYou've made: %0.2f", earnings - tax);
        printf("\nPaying: %0.2f in tax", tax);
    }
    else{
            tax = 0;
            printf("You made %0.2f", earnings);
            printf("Paying: %0.2f", tax);
    }

    return 0;
}

CodePudding user response:

You can simplify your conditions, like:

    if (earnings < 12000) {
    } else if (earnings < 20000) {
    } else if (earnings < 40000) {
    } else { // earnings >= 40000 
    }

You can also reduce the "sometimes not needed" pre-calculations, merge consecutive calls to printf and avoid duplicating them everywhere, and replace run-time calculations with compile-time calculations, like:

    const double constTax1 = (20000 - 12000) * 0.1;
    const double constTax2 = (40000 - 20000) * 0.15   constTax1;
    double tax = 0.0;

    if (earnings < 12000) {
        // Do nothing!
    } else if (earnings < 20000) {
        tax = (earnings - 12000) * 0.1;
    } else if (earnings < 40000) {
        tax = (earnings - 20000) * 0.15   constTax1;
    } else { // earnings >= 40000 
        tax = (earnings - 20000) * 0.2   constTax2;
    }
    printf("\nYou've made: %0.2f\nPaying: %0.2f in tax", earnings - tax, tax);

Fortunately, it's likely that the compiler would've done most of these optimizations itself. The biggest performance gain would be from merging the calls to printf() (to reduce/avoid function call overhead).

Note that the compiler is typically prevented from optimizing calls to printf() properly because it's in a different compilation unit (even if you use link-time optimisation it's likely that printf() is in a dynamically linked library).

CodePudding user response:

You could do something like the following to avoid writing the same numbers multiple times and to make it easier to add additional tax brackets. (This almost certainly won't make it any faster, but I do think it could potentially be more maintainable.)

const int nb = 3;
const double taxrate[nb] = {0.1, 0.15, 0.2};
const double taxcutoff[nb] = {12000, 20000, 40000};
double tax = 0;

for (int i = 0; i < nb && earnings > taxcutoff[i]; i  ) {
 tax  = (earnings - taxcutoff[i]) * taxrate[i];
}
printf("\nYou've made: %0.2f", earnings - tax);
printf("\nPaying: %0.2f in tax", tax);

I believe the above is equivalent to your original code, but this is not how real tax brackets work. It is more likely you want something like:

const int nb = 3;
const double taxrate[nb] = {0.1, 0.15, 0.2};
const double taxcutoff[nb] = {12000, 20000, 40000};
double bracketearnings;
double tax = 0;

for (int i = 0; i < nb && earnings > taxcutoff[i]; i  ) {
 if (i == nb-1 || earnings < taxcutoff[i 1]) {
    bracketearnings = earnings - taxcutoff[i];
 }
 else {
    bracketearnings = taxcutoff[i 1] - taxcutoff[i];
 }
 tax  = bracketearnings * taxrate[i];
}
printf("\nYou've made: %0.2f", earnings - tax);
printf("\nPaying: %0.2f in tax", tax);
  • Related