Workshop 01
Workshop 01
PART 1:
Program 1:
Hello World!
Program 2: Write a C program to print your name, date of birth. and Address.
Program 3: VIết 1 chương trình hiển thị tổng, hiệu, tích, thương của 2 số 446 và
223 trên màn hình.
Yêu cầu: mỗi biểu thức hiển thị trên một dòng.
Output:
446 / 223 = 2
Program 4: Write a C program to print a block F using hash (#), where the F has
a height of six characters and width of five and four characters.
Program 5: Write a program to create 2 integer variables a and b, assign value
125 to a and 600to b then display the following result as output.
Output:
a+b =725
Program 6: Write a program to declare 2 variable a and b which are real
numbers, assign value 10.5 to a, 7.3 to b and display the following line as
output,
Output:
a/b=1.44
Program 7: Give a rectangle with the length of 7.8 and the width of 3.6. Write a
program to print the area of this rectangle on the screen as below:
Area= 28.0800
Program 8: Write a program to display character ‘d’ on the screen.
Program 9:
Sample
c:22936 ‘A’
23
i:22936 1
16
l:2293 1000
612
f:22936 0.5
08
d:22936 12.809
00
Program 10:
: Enter two numbers. Then, the sum of these two integers is calculated and displayed
on the screen.
Program 11:
: Enter two numbers. Then print swap them.
Program 12:
: Swap Numbers Without Using Temporary Variables (homework)
CONDITIONAL LOGIC
Program 13:
: Write a program to accepts an integer n from the user then check whether n is an
even or odd number.
Program 14:
:Write a program that accepts an integer n from the user then checks the following
conditions:
If two values are not equal to 0, print the following line on the screen:
473
When the code is compiled and executed, it produces the following result:
If you enter:
444
When the code is compiled and executed, it produces the following result:
4
Program 17:
: Write a program to read an integer a then check whether a is in the range [10, 100] or
not.
If a is in the range [10, 100], print the following line on the screen:
7 + 9
When the code is compiled and executed, it produces the following result:
16
LOOP: FOR
Program 20: Write a program that accepts an integer n from the user then displays all the
numbers from 1 to n on the screen.
For example, if you enter 10 from the keyboard, the program will produce the following
result:
1 2 3 4 5 6 7 8 9 10
Program 21: Write a program that accepts two integers a and b from the user and displays
all numbers from a to b on the screen.
For example, if a = 5, b = 9, the screen will display as below:
5 6 7 8 9
Program 22: Write a program that accepts an integer n from the user then displays all
numbers from n to -n (n >= -5) in descending order.
For example, if n = 5, the screen will display as below:
5 4 3 2 1 0 -1 -2 -3 -4 -5
Program 23: Write a program that accepts two integers a and b from the user and displays
the sum of all the numbers from a to b on the screen:
For example, if a = 5, b = 9, the screen will display as below:
35
Because 5 + 6 + 7 + 8 + 9 = 35
Program 24: Write a program that accepts an integer n from the user and displays the
sum of all odd numbers from 0 to n on the screen.
For example, if n = 7, the program will produce the following result:
16
Because 1 + 3 + 5 + 7 = 16
Program 25: Write a program that accepts two integers a and b from the user and prints all
the numbers from a to b, which are divisible by 3:
For example, if a = 1, b = 20, the program will display on the screen as below:
3 6 9 12 15 18
Program 26: Write a program that accepts an integer n from the user and displays the
result of n! on the screen.
For example, if n = 5, the program will display on the screen as below:
120
Because 1 * 2 * 3 * 4 * 5 = 120.
Program 27: Write a program that accepts an integer n from the user and prints the
divisors of n (n > 0) on the screen.
For example, if n = 12, the screen will display as below:
1 2 3 4 6 12
LOOP: WHILE AND DO-WHILE
Program 28:: Write a program that accepts an integer n and prints all even numbers
from n to 100 on the screen.
For example, if n = 90, the program will produce the following result:
90 92 94 96 98 100
Program 29:: Write a progarm that accepts an integer n from the user then prints all
divisors of n on the screen.
For example, if n = 12 , the program produces the following result:
For example, if a = 2, b = 3, the program will produce the following result:
Because 2 * 2 * 2 = 8.
Program 31:: Write a program that accepts two integers a and b from the user then prints
all numbers from a to b, which are divisible by 3 and 5.
For example, if a = 1, b = 50, the program produces the following result:
15 30 45
Program 32:: Fill in the blank (...) to complete the program that prints all numbers
from 1 to 50 on the screen.
Program 33:: Fill in the blank (...) to complete the program that prints all odd numbers
from 1 to 100.
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 num2 char op
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)
Based on taxable income, the employee has to pay his/her income tax with levels
pre-defined in the following table:
Write a program which will compute income tax of a people using the following
interface:
Case 1:
Case 1:
Program 3 (1 mark)
Program 4 (1 mark)
Program 5: (2 marks)
Program 6: (1 marks)
Related Each character will be stored as its ASCII code with value 0..255
knowledge
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