Assignment 2 - Flow of Control (while - loop)

1.

Write a C program to output the first n non-negative integers.

Sample Output
n = 6
0
1
2
3
4
5

   
2.

Write a C program to input the mark of a student. The mark should lie in between 0 and 100 inclusively.

Sample Output
Enter the mark = -2
Invalid input, please re-enter

Enter the mark = 101
Invalid input, please re-enter

Enter the mark = 83
The mark you have entered is 83

   
3.

Write a C program to sum up a sequence of integers.

Sample Output
Number 1 = 15
Continue (yes=1/no=0)? 1

Number 2 = 22
Continue (yes=1/no=0)? 1

Number 3 = 8
Continue (yes=1/no=0)? 1

Number 4 = 24
Continue (yes=1/no=0)? 0

Sum = 69

   
4. Write a C program to solve the quadratic equation:

    where
  => for

Sample Output

Solving quadratic equation: ax^2+bx+c=0
a = 0
a can't be equal to 0
a = 1
b = 2
c = 7
No real root

Continue (yes=1/no=0)? 1

Solving quadratic equation: ax^2+bx+c=0
a = 1
b = -1
c = -2
There are two real roots.
They are -1 and 2.

Continue (yes=1/no=0)? 1

Solving quadratic equation: ax^2+bx+c=0
a = 1
b = -6
c = 9
There is an equal root.
It is 3.

Continue (yes=1/no=0)? 0
   
5.

Write a C program to input a sequence of positive numbers and then generate the corresponding statistics. The end of data is indicated by the number 0.

Sample Output
number 1 = 38
number 2 = -12
positive number only, please re-enter.
number 2 = 92
number 3 = 61
number 4 = 17
number 5 = 80
number 6 = 45
number 7 = 0

Count = 6
Sum = 333
Average = 55.50
Largest = 92
Smallest = 17

   
6. In a certain country, the allowance given to taxpayers in the calculation of net chargeable income is shown in table 1. The tax to be paid is calculated by using table 2. (N.B. Net chargeable income = annual income - total allowance)

Family members Allowance   Net chargeable income Rate
(i) the taxpayer $30000   On the first $20000 10%
(ii) the taxpayer's spouse $30000   On the next $20000 20%
(iii) each child (up to a maximum of 5 children) $10000   Remainder 30%
(iv) each dependent parent $15000      

The tax paid should not be greater than 25% of the total annual income. Write a C program to generate the following sample output. (C program)

Sample Output

Name? Cheung Wai Yee
Total annual income? -1000
Total annual income? 120000
Marital status (1=Single, 2=Married)? 3
Marital status (1=Single, 2=Married)? 1
Number of dependent parents? -2
Number of dependent parents? 0
Tax for Cheung_Wai_Yee is $21000

Any more (yes=1/no=0)? 1

Name? So Siu Ming
Total annual income? 10000000
Marital status (1=Single, 2=Married)? 2
Number of children? 5
Number of dependent parents? 4
Tax for So_Siu_Ming is $2500000

Any more (yes=1/no=0)? 1

Name? Wong Ching Man
Total annual income? 110000
Marital status (1=Single, 2=Married)? 2
Number of children? 1
Number of dependent parents? 1
Tax for Wong_Ching_Man is $3000

Any more (yes=1/no=0)? 1

Name? Lo Yuen Kam
Total annual income? 110000
Marital status (1=Single, 2=Married)? 2
Number of children? 0
Number of dependent parents? 0
Tax for Lo_Yuen_Kam is $9000

Any more (yes=1/no=0)? 0