I.Lý thuyết
-Hàm gets() là một trong những hàm cơ bản nhất, được dùng để đọc và nhập vào một chuỗi ký tự (string) bao gồm cả ký hiệu đặc biệt như \n hay khoảng trắng (phím space), và hàm này được kết thúc bằng phím Enter.
-Hàm puts() cũng là những hàm cơ bản, dùng để in và xuất ra một chuỗi ký tự, đồng thời hàm này thêm vào kí hiệu \n một cách tự động, và sẽ không in ra kí hiệu kết thúc chuỗi.
II.Bài tập
**Bài 1**: 1. Write a program to accept and add three numbers.
#include <stdio.h> #include <stdlib.h> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char *argv[]) { int a; int b; int c; printf("nhap vao gia tri a = "); scanf("%d", &a); printf("nhap vao gia tri b = "); scanf("%d", &b); printf("nhap vao gia tri c = "); scanf("%d", &c); printf("Tong cua ba so = %d", a + b + c); return 0; }
**Bài 2**: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
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int a = 10;
int b = 7;
float c = 15.75;
int d = 4;
int e = 2;
float f = 5.6;
float z;
printf("Gia tri cua z = %f", z = a*b+(c/d)-e*f );
return 0;
}
**Bài 3**: Write a program to evaluate the area and perimeter of the rectangle.
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int a;
int b;
printf("Nhap vao chieu dai a = ");
scanf("%d", &a);
printf("Nhap vao chieu rong b = ");
scanf("%d", &b);
printf("Dien tich hinh chu nhat = %d", a*b);
printf("\nChu vi hinh chu nhat = %d", (a+b)*2);
return 0;
}
**Bài 4**:4. Write a program to evaluate the volume of a cylinder.
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int r;
int h;
float pi = 3.14;
printf("Nhap vao ban kinh duong tron day r = ");
scanf("%d", &r);
printf("Nhap vao chieu cao h = ");
scanf("%d", &h);
printf("The tich hinh tru = %f", r*r*pi*h);
return 0;
}
**Bài 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)
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int BS = 12000;
int DA = BS*12/100;
int HRA = 150;
int TA = 120;
int Other = 450;
int PF = BS*14/100;
int IT = BS*15/100;
printf("\nYour net salary = %d $ ", BS+DA+HRA+TA+Other-(PF+IT) );
return 0;
}





Không có nhận xét nào:
Đăng nhận xét