C Online Compiler
Example: Regional Taxation with Switch Statement in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Regional Taxation with Switch Statement #include <stdio.h> // Function to get tax rate based on region code float getTaxRateByCode(char regionCode) { // Step 1: Use a switch statement to check the region code switch (regionCode) { case 'N': // North region code return 0.10; // 10% tax rate case 'S': // South region code return 0.075; // 7.5% tax rate case 'E': // East region code return 0.12; // 12% tax rate case 'W': // West region code return 0.08; // 8% tax rate default: return 0.05; // Default tax rate for unknown codes } } int main() { char regionInputCode; float itemPrice; float taxRate; float finalPrice; // Step 2: Prompt user for region code and item price printf("Enter the region code (N for North, S for South, E for East, W for West): "); scanf(" %c", ®ionInputCode); // Note the space before %c to consume newline printf("Enter the item price: $"); scanf("%f", &itemPrice); // Step 3: Get the tax rate for the specified region code taxRate = getTaxRateByCode(regionInputCode); // Step 4: Calculate final price finalPrice = itemPrice * (1 + taxRate); // Step 5: Display results printf("\nRegion Code: %c\n", regionInputCode); printf("Item Price: $%.2f\n", itemPrice); printf("Tax Rate: %.2f%%\n", taxRate * 100); printf("Final Price (including tax): $%.2f\n", finalPrice); return 0; }
Output
Clear
ADVERTISEMENTS