I.Khái niệm
1.Printf
-Là hàm được sử dụng để xuất dữ liệu cũng như hiện thị dữ liệu đã được định dạng ở đầu ra.
VD: + printf(“control string”,argument list);
+ int a;
printf("nhap vao gia tri a");
2.Scantf
-Là hàm được sử dụng để nhập dữ liệu đã được định dạng tại đầu vào, thường đi liền với hàm printf.
VD: int a
printf("nhap vao gia tri a= ");
scanf("%d", a);
II.Bài tập
***Bài 1***:
1. Use the printf( ) statement and do the following
a) Print out the value of the integer variable sum
b) Print out the text string "Welcome", followed by a new line.
c) Print out the character variable letter
d) Print out the float variable discount
e) Print out the float variable dump using two decimal places
1. Sử dụng hàm printf() và làm những yêu cầu sau:
a) In ra giá trị nguyên của biến sum
b) In ra chuỗi "Welcome", theo một dòng mới
c) In ra biến ký tự "letter"
d) In ra giá trị thực discount
e) In ra giá trị thức dump sử dụng 2 chữ số thập phân
#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 sum=10;
printf("gia tri nguyen của bien = %d",sum);
printf("\nWelcome\n");
printf("bien ki tu: letter");
float discount=3.6;
printf("\nbien thuc thap phan=%0.2f",discount);
float dump=6.03;
printf("\nbien thức có 2 chữ số thập phân=%0.2f",dump);
***Bài 2***:Use the scanf( ) statement and do the following:
a) To read a decimal value from the keyboard, into the integer variable sum
b) To read a float variable into the variable discount_rate
Sử dụng hàm scanf() và làm:
a) Đọc giá trị thập phân từ bàn phím, và in ra biến nguyên "sum"
b) Đọc giá trị thực và in ra biến "discount_rate"
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
#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 sum;
printf("SUM = ");
scanf("%d",&sum);
printf("\ngia tri nguyen la: %d \n",sum);
float discount_rate;
printf("\ndiscount_rate: ");
scanf("%f",&discount_rate);
printf("\ngia tri thuc la: %f",discount_rate);
printf("\n");
system("pause");
return 0;
}
***Bài 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
|
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
#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[]) {
char name[10];
int basic, daper;
float bonper, loandet, salary;
printf("name: ");
scanf("%s",name);
printf("\nbasic = ");
scanf("%d",&basic);
printf("\ndaper = ");
scanf("%d",&daper);
printf("\nbonper = ");
scanf("%f",&bonper);
printf("\nloandet = ");
scanf("%f",&loandet);
salary = basic + basic * daper/100 + bonper * basic/100 - loandet;
printf("\nName \tBasic \tSalary");
return 0;
}
***Bài 4***:Viết chương trình hỏi họ và tên bạn, cuối cùng in ra định dạng tên và họ của bạn
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
#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[]) {
char firstname[10];
char lastname[20];
printf("\nfirstname : ");
scanf("%s", firstname);
printf("\nlastname : ");
scanf("%s", lastname);
printf("\nyour name is : %s%s", firstname, lastname);

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