Question 3 - Electricity Bill
Write a program to calculate my electricity bill. The electricity units are char ged at two different rates, the off peak units are 2.2p per unit and the peak units are 5.6p per unit. The standing charge is a flat rate of £8.50 per bill and VAT is add ed to the total bill at 8%. Write a program which reads in the current and past meter readings for the peak and off peak meters and then proceeds to calculate the total bill. The output should be neatly formatted on the screen and the programs should make use of constants.
Test Case Output
Enter the present peak meter reading: 100 Enter the past peak meter reading: 0 Enter the present off peak meter reading: 100 Enter the past off peak meter reading: 0 Cost of peak units 5.60 Cost of off peak units 2.20 Standing charge 8.50 Total VAT added 1.30 Total Bill 17.60
Question 4 - Temperature Conversion
Write a program which converts a user specified temperature from fahrenheit to degrees celcius.
Test Case
For an input temperature of 100 fahrenheit, the output value should be 37.777778.
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!