II.Bài Tập
1. Write a C program that accepts a number and square the number with the help of a function.
To do this,
a. Declare a function.
b. Accept the number.
c. Pass the number to the function and return the square of that number
Function prototype: int cal_square(int number);
2. Write a C program to find the area and perimeter of a circle.
Function prototype: float areaCircle(float radius);
Function prototype: float perimeterCircle(float radius);
Using the library: “Math.h” => PI = 3.14
3. Write a C program to calculate the factorial of an integer
Function prototype: long factorial(int number);
4. Write a program with a function that takes two int parameters, adds them together, then returns the sum. The program should ask the user for two numbers, then call the function with the numbers as arguments, and tell the user the sum.
Function prototype: int calSum(int number1, int number2);
Bài Làm : Bài 1:
#include <stdio.h>
#include <stdlib.h>/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int S(int a);
int main(int argc, char *argv[]) {
int x =4;
int sub(x);
printf("S=%d",S(x));
return 0;
}
int S(int a){
int S=a*a;
return S;
}
Bài 2
#include <stdio.h>
#include <stdlib.h>
#define PI 3.14
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
float C(float x);
float S(float x);
int main(int argc, char *argv[]) {
float R;
printf("Nhap Ban Kinh R =");
scanf("%f",&R);
float a = C(R);
float b =S(R);
printf("Chu Vi Hinh Tron = %f",a);
printf("\nDien tich Hinh Tron = %f",b);
return 0;
}
float S(float x){
float S=PI*x*x;
return S;
}
float C(float x){
float C=2*PI*x;
return C;
}
Bài 3
#include <stdlib.h>
#define PI 3.14
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
float C(float x);
float S(float x);
int main(int argc, char *argv[]) {
float R;
printf("Nhap Ban Kinh R =");
scanf("%f",&R);
float a = C(R);
float b =S(R);
printf("Chu Vi Hinh Tron = %f",a);
printf("\nDien tich Hinh Tron = %f",b);
return 0;
}
float S(float x){
float S=PI*x*x;
return S;
}
float C(float x){
float C=2*PI*x;
return C;
}
Bài 3
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int GiaiThua(int T);
int main(int argc, char *argv[]) {
int S;
printf("Nhap Mot So :");
scanf("%d",&S);
int C = GiaiThua(S);
printf("%d ! = %d",S,C);
return 0;
}
int GiaiThua(int T){
int a=1;
int b=1;
for(;a<=T;a++){
b=b*a;
}
return b;
}
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int GiaiThua(int T);
int main(int argc, char *argv[]) {
int S;
printf("Nhap Mot So :");
scanf("%d",&S);
int C = GiaiThua(S);
printf("%d ! = %d",S,C);
return 0;
}
int GiaiThua(int T){
int a=1;
int b=1;
for(;a<=T;a++){
b=b*a;
}
return b;
}
Bài 4
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int Tong(int ,int );
int main(int argc, char *argv[]) {
int s;
printf("Nhap So :");
scanf("%d",&s);
int f;
printf("Nhap So :");
scanf("%d",&f);
printf("Tong = %d",Tong(s,f));
return 0;
}
int Tong(int s, int f){
int t;
t= s+f;
return t;
}
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int Tong(int ,int );
int main(int argc, char *argv[]) {
int s;
printf("Nhap So :");
scanf("%d",&s);
int f;
printf("Nhap So :");
scanf("%d",&f);
printf("Tong = %d",Tong(s,f));
return 0;
}
int Tong(int s, int f){
int t;
t= s+f;
return t;
}


















