I. Hàm gets()
- Cú pháp:
gets(string);
- Hàm gets cũng giống như hàm scanf được dùng để nhập dữ liệu từ thiết bị nhập chuẩn.
- Sự khác nhau giữa hàm gets và scanf:
+ Hàm scanf khi nhập vào một chuỗi thì nó sẽ kết thúc khi gặp một khoảng trắng, vì vậy hàm scanf rất khó lưu chuỗi mà có chứa khoảng trắng.
+ Hàm gets sẽ chỉ kết thúc khi ta nhấn enter, có nghĩa là hàm gets dễ dàng lưu được một chuỗi mà có chứa khoảng trắng.
II. Hàm puts()
- Cú pháp:
puts(string);
- Hàm puts cũng giống như printf được dùng để xuất dữ liệu ra ngoài màn hình nhưng hàm puts đơn giản hơn
III. Bài tập:
Bài 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,b,c;
puts("Nhap so thu nhat:");
scanf("%d", &a);
puts("Nhap so thu hai:");
scanf("%d", &b);
puts("Nhap so thu ba:");
scanf("%d", &c);
return 0;
}
****************************************************
Bài 2: For the following values, write a program to evaluate the expression
z = a*b+(c/d)-e*f ;
Thứ Bảy, 31 tháng 1, 2015
gets() và puts()
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,b,d,e;
float c,f,z;
a=10; b=7; c=15.75; d=4; e=2; f=5.6;
z=a*b+(c/d)-e*f;
printf("z = %f", z);
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[]) {
float x,y;
printf("Nhap chieu dai HCN:");
scanf("%f", &x);
printf("Nhap chieu rong HCN:");
scanf("%f", &y);
printf("\nChu vi hinh chu nhat la: %f", (x+y)*2);
printf("\nDien tich hinh chu nhat la: %f", x*y);
return 0;
}
****************************************************
Bài 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[]) {
float x,y;
printf("Nhap ban kinh day:");
scanf("%f", &x);
printf("Nhap chieu cao hinh tru:");
scanf("%f", &y);
printf("The tich cua hinh tru la: %f", 3.14*x*x*y);
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 basic_salary, DA, HRA, TA, others, PF,IT, net_salary;
basic_salary = 12000;
DA = basic_salary*12/100;
HRA = 150;
TA = 120;
others = 450;
PF = basic_salary*14/100;
IT = basic_salary*15/100;
net_salary = basic_salary + DA + HRA + others - (PF+IT);
printf("Net salary: %d", net_salary);
return 0;
}
****************************************************
Đăng ký:
Đăng Nhận xét (Atom)





0 nhận xét:
Đăng nhận xét