C++ Online Compiler
Example: Regional Taxation with If-Else If in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Regional Taxation with If-Else If #include <iostream> #include <string> #include <iomanip> // For std::fixed and std::setprecision double calculateTaxIfElse(double amount, const std::string& region) { double taxRate = 0.0; // Default tax rate if (region == "North") { taxRate = 0.05; // 5% tax } else if (region == "South") { taxRate = 0.07; // 7% tax } else if (region == "East") { taxRate = 0.06; // 6% tax } else if (region == "West") { taxRate = 0.08; // 8% tax } else { std::cout << "Warning: Unknown region '" << region << "'. Applying default tax (0%)." << std::endl; taxRate = 0.0; } return amount * taxRate; } int main() { double itemAmount = 100.0; // Step 1: Calculate tax for North region std::string region1 = "North"; double tax1 = calculateTaxIfElse(itemAmount, region1); double totalCost1 = itemAmount + tax1; std::cout << std::fixed << std::setprecision(2); // Format output to 2 decimal places std::cout << "Item: $" << itemAmount << ", Region: " << region1 << ", Tax: $" << tax1 << ", Total: $" << totalCost1 << std::endl; // Step 2: Calculate tax for South region std::string region2 = "South"; double tax2 = calculateTaxIfElse(itemAmount, region2); double totalCost2 = itemAmount + tax2; std::cout << "Item: $" << itemAmount << ", Region: " << region2 << ", Tax: $" << tax2 << ", Total: $" << totalCost2 << std::endl; // Step 3: Calculate tax for an unknown region std::string region3 = "Central"; double tax3 = calculateTaxIfElse(itemAmount, region3); double totalCost3 = itemAmount + tax3; std::cout << "Item: $" << itemAmount << ", Region: " << region3 << ", Tax: $" << tax3 << ", Total: $" << totalCost3 << std::endl; return 0; }
Output
Clear
ADVERTISEMENTS