Program to Calculate Income Tax

Write a program to calculate the total income tax paid in old and new tax slabs by taking the annual salary from the user.

Old Tax Slab:

Annual Salary (Rs.)

Income Tax Rates

0 to 250000

Nil

250000 to 500000

5%

500000 to 1000000

20%

Greater than 1000000

30%

Note: The olda Tax slab has rebate if the annual salary is <= 500000.

New Tax Slab:

Annual Salary (Rs.)

Income Tax Rates

0 to 250000

Nil

250000 to 500000

5%

500000 to 750000

10%

750000 to 1000000

15%

1000000 to 1250000

20%

1250000 to 1500000

25%

Greater than 1500000

30%

Note: New Tax slab has rebate if the annual salary <= 700000.

Approach: To solve the problem, follow the below idea:

The problem can be solved by calculating the total tax according to the input salary. The Income Tax can be calculated according to the old as well as the new tax slabs.

Below is the implementation of the approach:

C++
#include <iostream>
using namespace std;

double getOldTax(double salary)
{
    // Lies under tax rebate limit
    if (salary <= 500000)
        return 0;
    if (salary <= 1000000)
        return (salary - 500000) * 0.2 + (250000 * 0.05);
    return (salary - 1000000) * 0.3 + (500000 * 0.2)
           + (250000 * 0.05);
}

double getNewTax(double salary)
{
    // lies under tax rebate limit
    if (salary <= 700000)
        return 0;
    if (salary <= 750000)
        return (250000 * 0.05) + (salary - 500000) * 0.1;
    if (salary <= 1000000)
        return (250000 * 0.05) + (250000 * 0.10)
               + (salary - 750000) * 0.15;
    if (salary <= 1250000)
        return (250000 * 0.05) + (250000 * 0.10)
               + (250000 * 0.15) + (salary - 1000000) * 0.2;
    if (salary <= 1500000)
        return (250000 * 0.05) + (250000 * 0.10)
               + (250000 * 0.15) + (250000 * 0.2)
               + (salary - 1250000) * 0.25;
    return (250000 * 0.05) + (250000 * 0.10)
           + (250000 * 0.15) + (250000 * 0.2)
           + (250000 * 0.25) + (salary - 1500000) * 0.3;
}

int main()
{
    double salary = 1000000;
    cout << "Tax using Old Tax Slab Rates: "
         << getOldTax(salary) << endl;
    cout << "Tax using New Tax Slab Rates: "
         << getNewTax(salary) << endl;
    return 0;
}
Java
public class TaxCalculator {

    public static double getOldTax(double salary)
    {
        // Lies under tax rebate limit
        if (salary <= 500000) {
            return 0;
        }
        if (salary <= 1000000) {
            return (salary - 500000) * 0.2
                + (250000 * 0.05);
        }
        return (salary - 1000000) * 0.3 + (500000 * 0.2)
            + (250000 * 0.05);
    }

    public static double getNewTax(double salary)
    {
        // Lies under tax rebate limit
        if (salary <= 700000) {
            return 0;
        }
        if (salary <= 750000) {
            return (250000 * 0.05)
                + (salary - 500000) * 0.1;
        }
        if (salary <= 1000000) {
            return (250000 * 0.05) + (250000 * 0.10)
                + (salary - 750000) * 0.15;
        }
        if (salary <= 1250000) {
            return (250000 * 0.05) + (250000 * 0.10)
                + (250000 * 0.15)
                + (salary - 1000000) * 0.2;
        }
        if (salary <= 1500000) {
            return (250000 * 0.05) + (250000 * 0.10)
                + (250000 * 0.15) + (250000 * 0.2)
                + (salary - 1250000) * 0.25;
        }
        return (250000 * 0.05) + (250000 * 0.10)
            + (250000 * 0.15) + (250000 * 0.2)
            + (250000 * 0.25) + (salary - 1500000) * 0.3;
    }

    public static void main(String[] args)
    {
        double salary = 1000000;
        System.out.println("Tax using Old Tax Slab Rates: "
                           + getOldTax(salary));
        System.out.println("Tax using New Tax Slab Rates: "
                           + getNewTax(salary));
    }
}
Python3
def get_old_tax(salary):
    # Lies under tax rebate limit
    if salary <= 500000:
        return 0
    if salary <= 1000000:
        return (salary - 500000) * 0.2 + (250000 * 0.05)
    return (salary - 1000000) * 0.3 + (500000 * 0.2) + (250000 * 0.05)


def get_new_tax(salary):
    # lies under tax rebate limit
    if salary <= 700000:
        return 0
    if salary <= 750000:
        return (250000 * 0.05) + (salary - 500000) * 0.1
    if salary <= 1000000:
        return (250000 * 0.05) + (250000 * 0.10) + (salary - 750000) * 0.15
    if salary <= 1250000:
        return (250000 * 0.05) + (250000 * 0.10) + (250000 * 0.15) + (salary - 1000000) * 0.2
    if salary <= 1500000:
        return (250000 * 0.05) + (250000 * 0.10) + (250000 * 0.15) + (250000 * 0.2) + (salary - 1250000) * 0.25
    return (250000 * 0.05) + (250000 * 0.10) + (250000 * 0.15) + (250000 * 0.2) + (250000 * 0.25) + (salary - 1500000) * 0.3


def main():
    salary = 1000000
    print("Tax using Old Tax Slab Rates:", get_old_tax(salary))
    print("Tax using New Tax Slab Rates:", get_new_tax(salary))


if __name__ == "__main__":
    main()
JavaScript
// Function to calculate tax using old tax slab rates
function getOldTax(salary) {
    // Lies under tax rebate limit
    if (salary <= 500000) {
        return 0;
    }
    if (salary <= 1000000) {
        return (salary - 500000) * 0.2 + (250000 * 0.05);
    }
    return (salary - 1000000) * 0.3 + (500000 * 0.2) + (250000 * 0.05);
}

// Function to calculate tax using new tax slab rates
function getNewTax(salary) {
    // lies under tax rebate limit
    if (salary <= 700000) {
        return 0;
    }
    if (salary <= 750000) {
        return (250000 * 0.05) + (salary - 500000) * 0.1;
    }
    if (salary <= 1000000) {
        return (250000 * 0.05) + (250000 * 0.10) + (salary - 750000) * 0.15;
    }
    if (salary <= 1250000) {
        return (250000 * 0.05) + (250000 * 0.10) + (250000 * 0.15) + (salary - 1000000) * 0.2;
    }
    if (salary <= 1500000) {
        return (250000 * 0.05) + (250000 * 0.10) + (250000 * 0.15) + (250000 * 0.2) + (salary - 1250000) * 0.25;
    }
    return (250000 * 0.05) + (250000 * 0.10) + (250000 * 0.15) + (250000 * 0.2) + (250000 * 0.25) + (salary - 1500000) * 0.3;
}

// Main function to test the tax calculation
function main() {
    let salary = 1000000;
    console.log("Tax using Old Tax Slab Rates:", getOldTax(salary));
    console.log("Tax using New Tax Slab Rates:", getNewTax(salary));
}

// Run the main function
main();

Output
Tax using Old Tax Slab Rates: 112500
Tax using New Tax Slab Rates: 75000

Time Complexity: O(1)
Auxiliary Space: O(1)