Today we are going to build an Income Tax Calculator Project in Java. Taxation systems can be complex! The amount of tax payable will vary depending on which slab your total income lies. Look at this table to make things clear.

Up to 3 LPA(lakhs per annum), no tax is required. All incomes above 3 LPA are subjected to a total of 3 % Education plus Higher Education Cess on the total tax. The total tax varies with the slab as per the given table.

### Java Income Tax Calculator Project

```
public class IncomeTaxCalculator {
static double calculateTax(double ti) {
double total_tax = 0;
double total_cess = 0;
if(ti > 300000) {
double amt=0,tax1;
if((ti - 300000) > 200000)
amt = 200000;
else {
amt = ti - 300000;
}
tax1 = (0.1 * amt);
total_tax += tax1;
System.out.println("Tax Payable for slab 3,00,000-5,00,000: "
+ tax1);
}
if(ti > 500000) {
double amt,tax2;
if((ti - 500000) > 500000)
amt = 500000;
else {
amt = ti - 500000;
}
tax2 = 0.2 * amt;
total_tax += tax2;
System.out.println("Tax Payable for slab 5,00,000-10,00,000: "
+ tax2);
}
if(ti > 1000000){
double tax3;
tax3 = 0.3 * (ti - 1000000);
total_tax += tax3;
System.out.println("Tax Payable for slab 10,00,000-above: "
+ tax3);
}
total_cess = 0.03 * total_tax;
System.out.println("Total cess = 3% of income tax = "+total_cess);
return total_tax + total_cess;
}
public static void main(String[] args) {
System.out.println("total tax: "+calculateTax(2000000));
}
}
```

At first glance, the entire code might seem overwhelming for beginners. But the overall look tells us that there are three conditional blocks to check for the three taxable slabs: 3 lakh – 5 lakh, 5 lakh – 10 lakh, and 10 lakh – above. For total income (ti) less than 3 lakh no tax is levied upon.

In each block, we use variables tax1, tax2, and tax3 to calculate that tax amount for each slab.

If the total income lies in the 2nd or 3rd slab, we need to calculate the applicable tax for the previous slabs too. For instance, if the total income is 20,00,000. Then we must calculate tax for all three slabs. The total tax amount for 1st slab will be 20,000(10% of 2,00,000). Now you understand why 20,000 has been added with the income tax rate for the slab 5,00,000 – 10,00,000.

The above code can be reduced to many fewer lines with proper conditions and if-else block. I have expanded the code purposefully to make the logic straight forward. Here’s the simpler version of the above code:

### Optimized Solution

```
import java.util.Scanner;
public class IncomeTaxCalculator2 {
static void calculateTax(double ti) {
double tax=0,cess=0;
if(ti > 300000 && ti < 500000)
tax = (ti-30000)*0.1;
else if(ti > 500000 && ti < 1000000)
tax = 20000 + (ti-50000)*0.2;
else if(ti > 1000000)
tax = 120000 + (ti-1000000)*0.3;
cess = tax * 0.03;
System.out.println("Total tax: "+tax+" Total cess: "+cess);
System.out.println("Tax payable:"+(tax+cess));
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Total Income: ");
double total_income = sc.nextDouble();
calculateTax(total_income);
sc.close();
}
}
```

#### OUTPUT

In the above code, we accept console-based user input for the total income. Then we call our calculateTax() method with the provided value. In this case, 20,00,000. Check that the total tax for both the program is same. Therefore, as a Programmer, we shall readily jump into the latter example. But the first code will help you understand how the taxation system works in India.