Subject: PRF192- PFC
Workshop 02
File name: No.FullName.RAR that includes 7 programs file:
No.FullName_P1.c, No.FullName_P2.c, …. No.FullName_P7.c
With: No is your number in List Students, FullName is your fullname, P1…P7 is your
programs)
Submit in Slot 6 on Edunext
Deadline: 12:00 Sunday, 05/02/2023
Objectives:
Practicing skills at analyzing and implementing simple programs
Contents: 7 programs
Program 1 2 3 4 5 6 7
Mark 2 2 1 1 2 1 1
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”
}
Implement this program.
Program 2 (2 marks) – Yearly Personal Income Tax
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= 9000$/month
Alimony (tiền cấp dưỡng) for each his/her dependent pd= 3600$/month/dependent
With n dependents, Yearly tax-free income: tf = 12*(pa + n*pd)
Taxable income (thu nhập chịu thuế)
ti = income – tf
(If ti<=0 then income tax = 0)
Based on taxable income, the employee has to pay his/her income tax with levels
pre-defined in the following table:
Level Taxable Income Income tax
1 Less than or equal to 5.000 5%
2 From 5001 to 10.000 10%
3 From 10.001 to 18.000 15%
4 Over 18.000 20%
Write a program which will compute income tax of a people using the following
interface:
Case 1:
Your income of this year: 240.000 // nhập vào
Number of dependent: 4// nhập vào
Tax-free income: 280.800
Taxable income: 0
Income tax: 0
Case 2:
Your income of this year: 440.000
Number of dependent:4
Tax-free income: 280.800=12*(9000+4*3600)
Taxable income: 159.200=440.000-12*(9000+4*3600)
Income tax: 30.190
Program 3 (1 mark)
Objectives Practice loop statements
Related knowledge None
Problem Write a C program that will print out sum of integers inputted
from the keyboard until the value 0 is inputted.
Analysis Suggested algorithm (logical order of verbs)
Nouns: Begin
sum → int S; S=0;
Accepted integral value → Do {
int x Accept x;
If (x != 0) S = S + x;
}
While (x!=0);
Print out S;
End
Program 4 (1 mark)
Objectives Practice loops statement
Related knowledge None
Problem Write a C program that will carry out some times: accept two integers,
swap these values, print them out to the monitor. The program will
terminate when the value of 0 is inputted.
Analysis Suggested algorithm (logical order of verbs)
Nouns: Begin
2 integers → int x, y; Do {
Accept x, y;
int t= x; /* t: temporary variable */
x= y;
y= t;
Print out x, y;
}
While ( x!=0 && y!=0);
End
Program 5: (2 marks)
Related knowledge Use the function getchar() – stdio.h, to input a character,
the function toupper(ch) to convert a character to
uppercase - ctype.h
ASCII code of the ENTER key: ‘\n’
Problem Write a C program that will:
- permit user inputting a string of characters. The
input operation will terminate if the ENTER key is
stroked.
- print out the number of vowels, number of
consonants, and number of others to the monitor.
Analysis Suggested algorithm (logical order of verbs)
Nouns: Begin
inputted character Do {
→ char ch Accept ch; /* ch= getchar(); */
Number of vowels Convert ch to its uppercase /* ch= toupper(ch); */
→ int nVowels =0; If ( ch>=’A’ and ch <=’Z’) {
Number of consonants switch (ch) {
→ int consonants =0; case ‘A’ :
Number of other characters case ‘E’ :
→ int nOthers =0; case ‘I’ :
case ‘O’ :
case ‘U’ : nVowels ++; break;
default: nConsonants++;
}
}
else nOthers = nOthers++;
} While ( ch != ‘\n’);
Print out nVowels;
Print out nConsonants;
Print out nOthers;
End
Program 6: (1 marks)
Related knowledge Each character will be stored as its ASCII code with value 0..255
Problem Write a C program that will print out the ASCII code table.
Analysis Suggested algorithm (logical order of verbs)
ASCII code Begin
→ int code For each code = 0 to 255
{ Print out (“%c : %d, %o, %X\n”, code, code, code, code);
If (code !=0 && code %20==0) getchar(); /* code page of 20 lines */
}
End.
Program 7: (1 marks)
Problem Write a C program that will accept two characters then
print out ASCII code difference between them and
characters between them including code values in
decimal, octal, hexadecimal expansions in ascending
order.
Analysis Suggested algorithm (logical order of verbs)
2 character Begin
→ char c1, c2 Accept c1 ;;
Difference Accept c2;
➔ → int d; If (c1 > c2 )
Character for swapping { t = c1; c1 = c2; c2= t;
operation }
➔ → char t d = c2 – c1;
Character for looping Print out d;
➔ Char c For each c from c1 to c2
{ Print out (“%c : %d, %o, %X\n”, c, c, c, c);
}
End.
END