Lab2
Lab2
Workshop 02
Objectives:
Practicing skills at analyzing and implementing simple programs
Contents: 5 programs
Program 1 2 3 4 5
Mark 2 2 2 2 2
Program 1 ( 2 marks)
Write a program that allows user inputting a simple expression containing one of four
operators +, -, *, / then the result is printed out to the monitor. Input format: num1
operator num2,
An example of user interface
Enter an expression (+ - * /): 4*5
Result: 20
Sample Analysis
Content Implementation
Nouns Expression, double num1, num2
format num1 operator char op
num2 double result
result
Verbs Begin
Accept num1, op, num2 scanf( “%lf%c%lf”, &num1, &op, &num2)
Calculate result switch (op)
Print out result { case ‘+’ : result = num1 + num2;
End print out result;
break;
case ‘-’ : result = num1 - num2;
print out result;
break;
case ‘*’ : result = num1 * num2;
print out result;
break;
case ‘/’ : if ( num2==0)
print out “Divide by 0
“
else
{ result = num1 /
num2;
print out result;
}
break;
default: print out “Op is not
supported”
}
Suppose that:
In Viet Nam, each people has to pay for his/her yearly personal income tax as the
following description:
Rules:
Tax-free income:
Personal pending amount (tiền nuôi bản thân) pa= 9 000 000$/month
Alimony (tiền cấp dưỡng) for each his/her dependent pd= 3 600
000$/month/dependent
With n dependents, Yearly tax-free income: tf = 12*(pa + n*pd)
Write a program which will compute income tax of a people using the following
interface:
Case 1:
Case 2:
Program 4: (2 marks)
Program 5: (2 marks)
Write a C program that asks the user to enter the height of a triangle (an integer greater
than 1 if wrong enter again) and then prints right triangle, equilateral triangle with the
specified height. The triangle should be made of symbol (*) and should look something
like this:
Input n: 5
*
* *
* * *
* * * *
* * * * *
*
* *
* * *
* * * *
* * * * *