Thứ Bảy, 31 tháng 1, 2015

Bài tập ngày 3.1

I.Lý thuyết
1 . Hàm Gets
- Gets nằm trong thư viện : #include <stdio.h>
- Gets dùng để nhập dữ liệu.
- Cú pháp :
            Gets ("Chuỗi định dạng");
2.Hàm Puts
- Puts nằm trong thư viện : #include <stdio.h>
-Puts dùng để xuất dữ liệu.
-Cú pháp :
               Puts("Chuỗi định dạng");
+chuỗi định dạng là mảng khai báo được định dạng.
II. Bài tập

1.   Write a program to accept and add three numbers.
Bài Làm




#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;
    printf("So thu nhat :");
    scanf("%d",&a);
    printf("So thu hai :");
    scanf("%d",&b);
    printf("So thu ba :");
    scanf("%d",&c);
    printf("Ket thuc.");
    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       
Bài Làm


#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 a,b,c,d,e,f,z;
    printf("Nhap a =");
    scanf("%f",&a);
        printf("Nhap b =");
    scanf("%f",&b);
        printf("Nhap c =");
    scanf("%f",&c);
        printf("Nhap d =");
    scanf("%f",&d);
        printf("Nhap e =");
    scanf("%f",&e);
        printf("Nhap f =");
    scanf("%f",&f);
    printf("z = a*b+(c/d)-e*f= %f ",z = a*b+(c/d)-e*f );
    printf("\n\nKet thuc.");
    return 0;
Bài 3
Write a program to evaluate the area and perimeter of the rectangle.
Bài làm
 #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[]) {
printf("Dien tich hinh chu nhat :");
int a,b,S,D;
printf("\nChieu dai a =");
scanf("%d",&a);
printf("\nChieu rong b =");
scanf("%d",&b);
printf("\n\nDien tich hinh chu nhat la :");
printf("\nS=a*b=%d",S=a*b);
printf("\n\nChu vi hinh chu nhat la :");
printf("\n\nD=(a+b)*2=%d",D=(a+b)*2);
printf("\n\nKet thuc.");
return 0;
Bài 4:  Write a program to evaluate the volume of a cylinder.
Bài làm

#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,h,V;

printf("Nhap ban kinh r =");
scanf("%d",&r);
printf("Nhap chieu cao h =");
scanf("%d",&h);
printf("The tich cua mot xi lanh la :");
printf("\n\nV=h*3.14*r*2=%d",V=h*3.14*r*2);
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)
Bài Làm

#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[]) {
printf("Muc Luong Cua Nhan Vien :");
int a,b,c,d,e,f,g,h;
printf("\nSalary =$");
scanf("%d",&a);
printf("DA=(Salary*12%)/100%= $%d",b=(a*12)/100);
printf("\nHRA =");
scanf("%d",&c);
printf("\nTA=");
scanf("%d",&d);
printf("\nOther =");
scanf("%d",&e);
printf("\n\nPF=(Salary*14%)/100%=$%d",f=(a*14)/100);

printf("\n\nIT=(Salary*15%)/100%=$%d",g=(a*15)/100);

h=a+b+c+d+e-(f+g);
printf("\n\n\nNet Salary =Basic Salary + DA+HRA+TA+Other-(PF+IT)= %d",h);
return 0;
}

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

Đăng nhận xét