[Bài tập]:Day 6:Exercise fuction

Bài 1: Write a C program that accepts a number and square the number with the help of a function.
int square(int x);
           
int square(int x){
            int c=x*x;
            return c;
            }
           

int main(int argc, char *argv[]) {
            int a,b;
           
            printf("Xin moi nhap so : ");
            scanf("%d",&a);
           
            int c=square(a);
            printf("\nLuy thua cua %d la %d",a,c);

            return 0;
Bài 2: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

float area(int x);
float pri(int y);

float area(int x){
            float s=2*3.14*x*x;
            return s;
}
float pri(int y){
            float c=3.14*y*y;
            return c;
}

int main(int argc, char *argv[]) {
            int a;
            printf("Xin moi nhap ban kinh: ");
            scanf("%d",&a);
            float S=area(a);
            printf("\nDien tich hinh tron la: %.2f",S);
            float C=pri(a);
            printf("\nChu vi hinh tron la: %.2f",C);
Bài 3:3. Write a C program to calculate the factorial of an integer
Function prototype:  long factorial(int number);

long factorial(int number);

long factorial(int number){
            int x;
            int fac=1;
            for(x=1;x<=number;x++){
                        fac=fac*x;
            }
            return fac;
}

int main(int argc, char *argv[]) {
            int i,z;
           
            printf("Xin moi nhap so: ");
            scanf("%d",&i);
            z=factorial(i);
            printf("Giai thua cua %d la %d",i,z);
           
            return 0;

Nhận xét