C examples for Operator:Arithmetic Operator
United States Federal Tax Schedule had four categories, and each category had two rates.
Category Tax |
---|
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 |
Married, Separate 15% of first $14,875 plus 28% of excess |
#include <stdio.h> #define SINGLE 1// ww w . j a v a2 s . c om #define HEAD_OF_HOUSEHOLD 2 #define MARRIED_JOINT 3 #define MARRIED_SEPARATE 4 #define EXIT 5 #define RATE_1 0.15f #define RATE_2 0.28f void flush_input_buffer(void); int main(void){ int category; float income, bracket, taxes; while(1){ printf("1) Single 2) Head of Household 3) Married, Joint 4) Married Separate\n"); printf("Enter your tax category (1-4) or 5 to quit: "); scanf("%d", &category); switch (category) { case (SINGLE) : bracket = 17850.0; break; case (HEAD_OF_HOUSEHOLD) : bracket = 23900.0; break; case (MARRIED_JOINT) : bracket = 29750.0; break; case (MARRIED_SEPARATE) : bracket = 14875.0; break; case (EXIT) : printf("Bye.\n"); return 0; // Exit Program default : flush_input_buffer(); printf("Invalid input: please enter an integer between 1 and 5.\n"); continue; } printf("Enter your income: "); while (scanf("%f", &income) != 1 || income < 0) { flush_input_buffer(); printf("Invalid input: please enter a positive number.\n"); printf("Enter your income: "); } if (income > bracket) taxes = RATE_2 * (income - bracket) + RATE_1 * bracket; else taxes = RATE_1 * income; printf("You will owe $%.2f in taxes.\n\n", taxes); } } void flush_input_buffer(void) { while (getchar() != '\n') ; }