Wednesday, September 10, 2014

[LBC] Day 3 - 5: More condition

1.  Declare two variables x and y. Assign values to these variables. Number x should be printed only if it is less than 2000 or greater than 3000, and number y should be printed only if it is between 100 and 500.

2.  Write a program to show your computer’s capabilities. The user types in a letter of the alphabet and your program should display the corresponding language or package available. Some sample input ans output is given below :
                           Input                                  Output
                           A or a                                Ada
                           B or b                                 Basic
                           C or c                                 COBOL                           
Using the ‘switch’ statement to choose and display the appropriate message. Use the
default label to display a message if the input does not match any of the above         
letters.




3.  Accept values in three variables and print the highest value.

* Cú pháp lệnh switch ... case:

switch (Biểu thức điều kiện){
case giá trị 1: Khối lệnh; break;
case giá trị 2: Khối lệnh; break;
.
.
case giá trị n: Khối lệnh; break;
default: Khối lệnh;
}

Chú ý:

- Các giá trị 1,2..n là các giá trị trả về của Biểu thức điều kiện, tương ứng với giá trị trả về của biểu thức điều kiện thì các khối lệnh tại các case sẽ được thực hiện, sau khi khối lệnh được thực hiện thì tại mỗi case sẽ có thêm dòng break; để thoát khỏi lệnh switch.
- Ngoài lệnh break thì người ta còn dùng lệnh continue để bỏ qua một vòng lặp hoặc case chứa nó.
- Các khối lệnh phía sau các case không cần phải có cặp ngoặc {}.

[LBC] Day 3 - 4: Excercise more IF

1.  Write a program that accepts two numbers a and b and checks whether or not a is divisible by b.

2.  Write a program to accept 2 numbers and tell whether the product of the two numbers is equal to or greater than 1000.

3.  Write a program to accept 2 numbers. Calculate the difference between the two values. If the difference is equal to any of the values entered, then display the following message :
Difference is equal to value <number of value entered>
If the difference is not equal to any of the values entered, display the following message:
Difference is not equal to any of the values entered

4.  Montek company gives allowances to its employees depending on their grade as follows :
  Grade                                 Allowance
  A                                        300
  B                                        250
     Others                                100       
Calculate the salary at the end of the month.   (Accept Salary and Grade from the user )

5.  Write a program to evaluate the Grade of a student for the following constraints
If marks >75 – grade A
If 60< marks < 75 – grade B
If 45<marks<60 – grade C
If 35<marks<45 - grade D

If marks < 35 – grade E

* Note:
- Khi ta khai báo: 
char chuoi[10];
nó chỉ lưu trữ được 9 ký tự thôi vì ký tự thứ 10 là ký tự NULL ('\O')
- Khi ta dùng hàm scanf() để nhập ký tự thì các ký tự nhập vào sẽ được nhận sau khi người nhập có khoảng trắng, enter, \n, \t... còn các ký tự khác sau đó sẽ được lưu trong bộ nhớ đệm, để giải quyết vấn đề này ta dùng hàm fflush(stdin) sau hàm scanf() để clear bộ nhớ đệm.
Trong bài 4, ta có thể dùng hàm getch() để nhận giá trị nhập vào và hàm putchar() để in giá trị nhập vào ra màn hình.
Hàm getch(): là hàm dùng để lấy ký tự từ bàn phím nhưng không in ra màn hình. Thường dùng get() để dừng màn hình lại xem kết quả, trong bài này là ta tận dụng chức năng của nó để giải quyết vấn đề bộ nhớ đệm của scanf() mà không dùng fflush(stdin).

[LBC] Day 3 - 3: Excercise IF

1.

2.

3.

* Cú pháp lệnh else ... if:
if (Biểu thức điều kiện 1){
            Câu lệnh;
}else if (Biểu thức điều kiện 2){
            Câu lệnh;
.
.
.
}else if (Biểu thức điều kiện n){
            Câu lệnh;
}else{
            Câu lệnh;
}

Chú ý:
- Trong bài 3 ta sử dụng cấu trúc else... if lồng vào nhau để trả về kết quả cho biến answer. Ta cũng có thể sử dụng cấu trúc lệnh switch...case để thực hiện bài toán này.

[LBC] Day 3 - 2: Excercise with ball (if)


* Cú pháp lệnh if:

if (Biểu thức điều kiện) {
                Các lệnh;
}

Nếu Biểu thức điều kiện trả về giá trị TRUE thì Các lệnh sẽ được thực thi ngược lại nếu trả về giá trị FALSE thì sẽ thoát lệnh if.

* Cú pháp lệnh if...else:

if (Biểu thức điều kiện){
                Các lệnh;
}else{
                Các lệnh;
}

Nếu Biểu thức điều kiện trả về giá trị TRUE thì Các lệnh sẽ được thực thi ngược lại nếu trả về giá trị FALSE thì sẽ thực hiện Các lệnh sau else.

Chú ý:
- Sau cặp ngoặc () không có dấu ";" , nếu sau nó chỉ có một lệnh thì có thể bỏ cặp ngoặc {}.
- Sau cặp ngoặc {} không có dấu ";" vì nó là khối lệnh bao gồm các lệnh, chỉ có các câu lệnh riêng rẽ mới kết thúc bằng dấu ";"
               

[LBC] Day 3 - 1: Exercise gets()/puts()

1.   Write a program to accept and add three numbers.

2.  For the following values, write a program to evaluate the expression

z = a*b+(c/d)-e*f ;

  a=10
  b=7
  c=15.75
  d=4
  e=2
  f=5.6      

3. Write a program to evaluate the area and perimeter of the rectangle.

4.  Write a program to evaluate the volume of a cylinder.

5.  Write a program to evaluate the net salary of an employee given the following constraints
Basic salary : $ 12000
DA : 12% of Basic salary
HRA : $150
TA : $120
Others : $450
Tax cuts – a) PF :14% of Basic salary and b) IT: 15% of Basic salary
Net Salary = Basic Salary + DA + HRA + TA + Others – (PF + IT)


Note:


Tuesday, September 9, 2014

[LBC] Day 2 - 3: Using dev-C++ to write some program

1. Use the printf( )  statement and do the following
Print out the value of the integer variable sum
Print out the text string "Welcome", followed by a new line.
Print out the character variable letter
Print out the float variable discount
Print out the float variable dump using two decimal places



2. Use the scanf( )  statement and do the following:
To read a decimal value from the keyboard, into the integer variable sum
To read a float variable into the variable discount_rate
Print the ASCII values of the characters ‘A’ and ‘b’



3. Write a program which takes name, basic , daper ( ie, percentage of D.A), bonper (ie, percentage bonus) and loandet ( loan amount to be debited) for an employee. Calculate the salary using the following relation:
salary = basic + basic * daper /100 +bonper                 
        *  basic/100 - loandet
Data is :
 
name
basic
daper
bonper
loandet
MARK
2500
55
33.33
250.00
Calculate salary and then print the result under the following headings.
(Salary to be printed to the nearest dollar.)
        Name                                    Basic                        Salary

4. Write a program that asks for your first name and last name, and then prints the names in the format last name, first name.

*Note:

- Trong khai báo chuỗi có 2 cách:
+ char chuoi[100] = "I love you";
+ char *chuoi = "I love you";

- Mã ASCII của một character chính là số thứ tự tương ứng của nó trong hệ thập phân.

- Các kiểu dữ liệu dẫn xuất (derived data type - không phải các kiểu dữ liệu cơ bản như int, float, double, char. vd: kiểu "chuỗi", struct, object...) trong hàm scanf() không phải thêm dấu "&" vì chính tên của các biến dữ liệu kiểu đó đã tham chiếu đến địa chỉ của nó rồi.

Đọc thêm về C tại:
http://quangchien.wordpress.com/2011/11/14/l%E1%BA%ADp-trinh-khoa-h%E1%BB%8Dc-b%E1%BA%B1ng-ngon-ng%E1%BB%AF-c-bai-gi%E1%BA%A3ng-c%E1%BB%A7a-richard-fitzpatrick/