# Why is this java code producing wrong results?

**URL:** https://forum.kirupa.com/t/why-is-this-java-code-producing-wrong-results/668195
**Category:** programming
**Created:** [July 12, 2024, 8:21am UTC](https://forum.kirupa.com/t/why-is-this-java-code-producing-wrong-results/668195 "2024-07-12T08:21:23Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![oslon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/oslon/32/24631_2.png) [@oslon](https://forum.kirupa.com/u/oslon)
#### Post date: [July 12, 2024, 8:21am UTC](https://forum.kirupa.com/t/why-is-this-java-code-producing-wrong-results/668195/1 "2024-07-12T08:21:23Z")

</div>

```auto
...
        if (filingStatus == 0) {
            //compute tax for single
            if (taxableIncome <= 8350) {
                tax = taxableIncome * 10 / 100;
            } else if (taxableIncome <= 33950) {
                tax = (taxableIncome - 8350) * 0.15 + 8350 * 0.10;
            } else if (taxableIncome <= 82250) {
                tax = 8350 * 0.1 + (33950 - 8350) * 0.15 + (taxableIncome - 33951) * 0.25;

            } else if (taxableIncome <= 171550) {
                tax = 8350 * 0.1 + (33950 - 8350) * 0.15 + (82250 - 33951) * 0.25 + (taxableIncome - 82251) * 0.28;

            } else if (taxableIncome <= 372950) {
                tax = 8350 * 0.1 + (33950 - 8350) * 0.15 + (82250 - 33951) * 0.25 + (171550 - 82251) * 0.28 + (taxableIncome - 171551) * 0.33;

            } else {
                tax = 8350 * 0.1 + (33950 - 8350) * 0.15 + (82250 - 33951) * 0.25 + (171550 - 82251) * 0.28 + (372950 - 171551) * 0.33 + 0.35 * (taxableIncome - 372951);

            }

        }
        System.out.println("tax is " + tax);

    }
}

```

Output

```auto
$ javac Egone.java && java Egone.java
Enter taxable income
400000
Enter the filing status 0-single, 1-married filing jointly, 2-married filing separately, 3-head of household
0
tax is 117682.29

```

```auto
        if (filingStatus == 0) {
            //compute tax for single
            if (taxableIncome <= 8350) {
                tax = taxableIncome * 10 / 100;
            } else if (taxableIncome <= 33950) {
                tax = (taxableIncome - 8350) * 15 / 100 + 8350 * 10 / 100;
            } else if (taxableIncome <= 82250) {
                tax = 8350 * 10 / 100 + (33950 - 8350) * 15 / 100 + (taxableIncome - 33951) * 25 / 100;

            } else if (taxableIncome <= 171550) {
                tax = 8350 * 10 / 100 + (33950 - 8350) * 15 / 100 + (82250 - 33951) * 25 / 100 + (taxableIncome - 82251) * 28 / 100;

            } else if (taxableIncome <= 372950) {
                tax = 8350 * 10 / 100 + (33950 - 8350) * 15 / 100 + (82250 - 33951) * 25 / 100 + (171550 - 82251) * 28 / 100 + (taxableIncome - 171551) * 33 / 100;

            } else {
                tax = 8350 * 10 / 100 + (33950 - 8350) * 15 / 100 + (82250 - 33951) * 25 / 100 + (171550 - 82251) * 28 / 100 + (372950 - 171551) * 33 / 100 + (taxableIncome - 372951) * 35 / 100;

            }

        }
        System.out.println("tax is " + tax);

    }
}

```

Output

```auto
$ javac Egone.java && java Egone.java
Enter taxable income
400000
Enter the filing status 0-single, 1-married filing jointly, 2-married filing separately, 3-head of household
0
tax is 117680.15

```

# But this doesn’t work

```auto
...
        if (filingStatus == 0) {
            //compute tax for single
            if (taxableIncome <= 8350) {
                tax = 10 / 100 * taxableIncome;
            } else if (taxableIncome <= 33950) {
                tax = 15 / 100 * (taxableIncome - 8350) + 10 / 100 * 8350;
            } else if (taxableIncome <= 82250) {
                tax = 8350 * 10 / 100 + (33950 - 8350) * 15 / 100 + (taxableIncome - 33951) * 25 / 100;

            } else if (taxableIncome <= 171550) {
                tax = 10 / 100 * 8350 + 15 / 100 * (33950 - 8350) + 25 / 100 * (82250 - 33951) + 28 / 100 * (taxableIncome - 82251);

            } else if (taxableIncome <= 372950) {
                tax = 10 / 100 * 8350 + 15 / 100 * (33950 - 8350) + 25 / 100 * (82250 - 33951) + 28 / 100 * (171550 - 82251) + 33 / 100 * (taxableIncome - 171551);

            } else {
                tax = 10 / 100 * 8350 + 15 / 100 * (33950 - 8350) + 25 / 100 * (82250 - 33951) + 28 / 100 * (171550 - 82251) + 33 / 100 * (372950 - 171551) + 35 / 100 * (taxableIncome - 372951);
            }

        }
        System.out.println("tax is " + tax);

    }
}

```

This is producing wrong results, why?

---

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [July 12, 2024, 10:04pm UTC](https://forum.kirupa.com/t/why-is-this-java-code-producing-wrong-results/668195/2 "2024-07-12T22:04:26Z")

</div>

Can you be a bit more specific? Are you seeing errors? What isn’t working?

---

<div class="post-metadata">

### Author: ![sinvalfelisberto](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sinvalfelisberto/32/26648_2.png) [@sinvalfelisberto](https://forum.kirupa.com/u/sinvalfelisberto)
#### Post date: [July 26, 2024, 8:36pm UTC](https://forum.kirupa.com/t/why-is-this-java-code-producing-wrong-results/668195/3 "2024-07-26T20:36:42Z")

</div>

Maybe the result is not what he expects… it seems it’s correct, without errors.
