Question 1 - Tax Bill
Write a program to calculate a persons income tax bill for the year. The basic rate of tax is 20% and everyone has a tax allowance of £3,500 on which they pay no tax. If they earn more than £16,000 then the rate of tax increases to 25%, if they earn more than £25,000 then the rate of tax is 30%.
The user should be able to enter the yearly salary and the program should output the total tax for that year. The program should use the if construct.
Test Case Output
NOTE: In this example, I have assumed that if for example a person earns £18,000 then they pay 25% tax on all their earnings apart from their tax allowance. This is the simplest way of implementing this problem, you could implement it a more complex manner using 25% of tax for all their earnings over £16000 and 20% on all their earnings below £16000.
Simple results:
Salary Tax
2000 0
10000 1300
16000 2500
18000 3625
25000 5375
30000 7950
Note that on the border cases I have used 25% for £25,000 and 20% for £16,000
Questions 2 & 3
The point I was trying to illustrate with these is to be careful about using the correct syntax && is a LOGICAL AND and a single & is a the bitwise AND operator.
i = 10;
j = 0;
if (i && j) {
printf("One\n");
}else{
printf("Two\n");
}
In this case it is testing if i is TRUE AND if j is TRUE. Remembering that a value is TRUE if it is non zero, then i is TRUE but j is FALSE. Therefore the whole expression is false, and the text Two would be printed on the screen.
If j was made equal to 5, then it would now be TRUE and hence the whole expression is true and the text One will be displayed.
The statement
if (i && j)
could be written as
if (i != 0 && j !=0)
In the next part
i=10; j=0; if (i & j) { printf("One\n"); }else{ printf("Two\n"); }the bitwise AND of 10 and 0 is evaluated, this produces a value of 0, which is FALSE and hence the text Two is written on the screen.
Even if j is equal to 5, the bitwise ANd still evaluates to 0, and the results is still FALSE and Two is written to the screen.
Therefore - always be careful to use the correct syntax in your expressions!
Back to Main Page for Lecture 3 |