C Online Compiler
Example: Regional Taxation with If-Else If in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Regional Taxation with If-Else If #include <stdio.h> #include <string.h> // Required for strcmp() // Function to get tax rate based on region name float getTaxRate(const char* region) { // Step 1: Compare input region with known regions using strcmp() // strcmp returns 0 if strings are equal if (strcmp(region, "North") == 0) { return 0.10; // 10% tax rate } else if (strcmp(region, "South") == 0) { return 0.075; // 7.5% tax rate } else if (strcmp(region, "East") == 0) { return 0.12; // 12% tax rate } else if (strcmp(region, "West") == 0) { return 0.08; // 8% tax rate } else { return 0.05; // Default tax rate for unknown regions (e.g., 5%) } } int main() { char regionName[50]; float itemPrice; float taxRate; float finalPrice; // Step 2: Prompt user for region and item price printf("Enter the region (North, South, East, West): "); scanf("%s", regionName); printf("Enter the item price: $"); scanf("%f", &itemPrice); // Step 3: Get the tax rate for the specified region taxRate = getTaxRate(regionName); // Step 4: Calculate final price finalPrice = itemPrice * (1 + taxRate); // Step 5: Display results printf("\nRegion: %s\n", regionName); 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