|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
|
|
|
PP: Tax Exercise
So, we can now start writing some useful programs. Let us take some practical application. We want to compute the income tax payable by a salaried employee. For now, let us make some assumptions, so that the world fits in what little we know of programming.For computing tax, we need to compute the total income first. For salaried employees, the core element of income is one"s basic pay. Using the concept of variables, let us define this as a variable, since we may need to change this for different persons, and we may want to use this figure for computing various other aspects of his income and tax. So,
�� basic_pay = 8000
This is monthly basic pay. For tax purpose, we need annual figures. So:
� annual_basic = basic_pay * 12
You can see that we dont write 8000*12, but basic_pay * 12. So, when we change the basic pay, the computation of annual_basic will not become wrong.
Another major component of income is the "dearness allowance", generally called DA. DA is a percentage of basic. This percentage will vary from time to time, based on some government notification. So, it is useful to make this explicit, to allow easy change.
� DA_percentage = 83
Now the DA can be computed.
� annual_DA = basic_pay * 12 * DA_percentage/100.
In a similar way, we can define house rent allowance (HRA).
� annual_HRA = basic_pay * 12 * 30/100
HRA is 30% of the basic; not something that changes often, and hence we have opted to keep it a part of the ex-pression. Let us assume there is no other income. So, we have the total income.
�total_income = annual_basic + annual_DA + annual_HRA
In the simple case, the tax is a simple fraction of the income, say 20%. So, tax is:
� income_tax� = total_income * 0.20
Note that 0.20 is 20/100 - we can write it either way. In one case, we make the computer do this computation; in the other, we do it ourselves. Here, the difference is not important for two reasons. First the ex-pression is so simple, the cost of computation is low for us and also for the computer. So, it perhaps does not matter, who computes it. Second, both the forms are adequately intuitive - so there is no loss of readability. Whenever, you have a choice of such a decision, apply both these criteria. For example, if you have to multiple something with PI/7, it is better to write it as pi/7, instead of 0.448. The latter form is not intuitive and a reader will wonder what this number is! Even you may not remember after a while. So, it is generally a good idea to give most of these 'magic numbers', a relevant name and use a variable of that name in your program.
Now, here is the consolidated program, which you can replace 'YOUR PROGRAM' in our template with. We have added the relevant declarations for the variables we have introduced.
int basic_pay, annual_basic; float DA_percentage, annual_DA, annual_HRA; float total_income; basic_pay = 8000; DA_percentage = 83; annual_basic = basic_pay * 12; annual_DA = basic_pay * 12 * DA_percentage/100; annual_HRA = basic_pay * 12 * 30/100; total_income = annual_basic + annual_DA + annual_HRA income_tax� = total_income * 0.20 System.out.print("Total income is "); System.out.println(total_income); System.out.print("Tax payable is "); System.out.println(income_tax);
Try it out with different basic_pay and other parameters. Can you modify this program by adding Rs 800 as a fixed conveyance allowance per month, and Rs 10000 as an annual allowance.
We will follow this example in the next section to introduce conditionals.
|
|
| | |
|
|
|
|
|
|
|