C++ Online Compiler
Example: Income Tax Calculator in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Income Tax Calculator #include <iostream> #include <iomanip> // For std::fixed and std::setprecision int main() { double annualIncome; double taxAmount = 0.0; // Step 1: Get annual income from the user std::cout << "Enter your annual income: $"; std::cin >> annualIncome; // Step 2: Define hypothetical tax slabs and calculate tax // Slab 1: Up to $10,000 - 0% tax // Slab 2: $10,001 to $50,000 - 10% on amount above $10,000 // Slab 3: Above $50,000 - 20% on amount above $50,000 if (annualIncome <= 10000) { taxAmount = 0; // No tax for income up to $10,000 } else if (annualIncome <= 50000) { // Income is between $10,001 and $50,000 // Tax 10% on the amount over $10,000 taxAmount = (annualIncome - 10000) * 0.10; } else { // Income is above $50,000 // Calculate tax for the first $50,000 // Tax for income between $10,001 and $50,000 ($40,000 range) taxAmount = (50000 - 10000) * 0.10; // Tax on the 10% slab: $40,000 * 0.10 = $4,000 // Add tax for income above $50,000 at 20% taxAmount += (annualIncome - 50000) * 0.20; } // Step 3: Display the calculated income tax std::cout << std::fixed << std::setprecision(2); // Format output to 2 decimal places std::cout << "Based on your income of $" << annualIncome << ", your estimated income tax is: $" << taxAmount << std::endl; return 0; }
Output
Clear
ADVERTISEMENTS