By GokiSoft.com| 15:48 08/12/2021|
Lập Trình C

[Video] Bài tập ôn luyện Function - Lập trình C - C2110I

Bài tập ôn luyện Function - Lập trình C


#bt1902.c


#include <stdio.h>
#include <stdlib.h>

//Bai 1 - implement
void showInfo() {
	printf("\nHo ten: TRAN VAN A");
	printf("\nTuoi: 20");
	printf("\nDia chi: Ha Noi");
	printf("\nEmail: tranvana@gamil.com");
	printf("\nSDT: 123456789");
}

//Bai 2 - implement
void showMessage(int msg) {
	printf("\nHello %d", msg);
}

//Bai 3 - implement
int giaithua(int n) {
	//n! = 1 * 2 * 3 * ... * n
	int result = 1;
	int i;
	
	for(i=1;i<=n;i++) {
		result *= i;
	}
	
	return result;
}

int tinhtong(int m) {
	int result = 0, i;
	for(i=1;i<=m;i++) {
		result += i;
	}
	
	return result;
}


#bt1902.h


//Bai 1
void showInfo();

//Bai 2
void showMessage(int msg);

//Bai 3
int giaithua(int n);
int tinhtong(int m);


#main.c


#include <stdio.h>
#include <stdlib.h>
#include "bt1902.h"

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
	//Bai 1
	printf("=========== BAI 1 ====================");
	showInfo();
	
	//Bai 2
	printf("=========== BAI 2 ====================");
	showMessage(1);
	showMessage(10);
	showMessage(16);
	
	//Bai 3
	printf("=========== BAI 3 ====================");
	int gt = giaithua(5);
	int tong = tinhtong(10);
	
	printf("\n5! = %d, sum(10) = %d", gt, tong);
	if(gt > tong) {
		printf("\n5! > sum(10)");
	} else if(gt == tong) {
		printf("\n5! = sum(10)");
	} else {
		printf("\n5! < sum(10)");
	}
	
	return 0;
}


Tags:

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)