By GokiSoft.com| 20:08 22/09/2023|
Lập Trình C

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

Bài 1:

Viết hàm đặt tên là showInfor() => Hiển thị thông tin cá nhân của bạn gồm tên, tuổi, địa chỉ, email, sđt

Trong hàm main thực hiện gọi hàm showInfor()

Bài 2:

Viết hàm đặt tên là showMessage có 1 tham số truyền vào int msg => Hiển thị dòng Hello msg

Trong hàm main lân lượt gọi 3 lần hàm showMesage với các giá trị khác nhau

Bài 3:

Viết hàm tính giaithua => tham số truyền vào n => trả về kết quả tính giai thừa N

Viết hàm tính tổng => tham số truyền vào m => trả về kết quả tính tổng của 1->m

trong hàm main thực hiện tính giai thừa của 5 và tổng 1-10

so sánh 2 kết quả trên.

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

5

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

hainguyen [T2008A]
hainguyen

2020-09-18 13:40:46



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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
//bai 3
int giaithua(int n){
    int abc = 1, i;
    for(i=1; i<=n; i++){
    	abc*=i;
	}
	return abc;
}
int tong(int m){
	int num = 0, i;
	for(i=1; i<=m; i++){
		num+=i;
	}
	return num;
}	

int main(int argc, char *argv[]) {
	
	int n, m;
	printf("\nNhap n, m: ");
	scanf("%d%d", &n, &m);
	
	if(giaithua(n)>tong(m))    printf("\ngiaithua n > tong m");
	 else if(giaithua(n)<tong(m))    printf("\ngiaithua n < tong m");
	 else printf("\ngiaithua n = trong m");
	
	return 0;
}



hainguyen [T2008A]
hainguyen

2020-09-18 13:10:28



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* run this program using char *argv[]) {
	the console pauser or add your own getch, system("pause") or input loop */
//bai 2
void showMesage(){
	int msg;
	printf("\nNhap msg = ");
	scanf("%d", &msg);
	
	printf("\n%d", msg);
	printf("\nHello msg");
}
int main(int argc, int **argv){
     showMesage(2);
	showMesage(3);
	showMesage(4);
	return 0;
}



Trần Văn Lâm [T2008A]
Trần Văn Lâm

2020-09-18 10:24:53



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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
//BAI 3
int GiaiThua(int n){
	int GT = 1, i;
	for(i = 1; i <=n; i++){
		GT *= i;
	}printf("\nGT 1->n: %d", n, GT);
	
	return GT;
	
}
int Tong(int m){
	int T = 0, i;
	printf("\nNhap so m=");
	scanf("%d", &m);
	for(i = 1; i <= m; i++){
		T += i;
	}printf("\nT 1->m: %d", m, T);
	return T;
}

void main() {
	int n, i, m;
	printf("\nNhap so n=");
	scanf("%d", &n);
	int sum = 1;
	
	for(i = 1; i <= 5; i++){
		sum *= i;
	}printf("\nGiai thua cua 5 :%d", sum);
	
	
	printf("\nNhap so m=");
	scanf("%d", &m);
	int sum1 = 0;
	for(i =1; i <= 10; i++){
		sum1 += i;
	}printf("\nTong tu 1 den 10: %d", sum1);
	
	int GiaiThua = sum;
	int Tong = sum1;

	if(sum >= sum1){
		printf("\nsum >= sum1");
	}else{
		printf("\nsum < sum1");
	}
	
}



hainguyen [T2008A]
hainguyen

2020-09-18 10:09:19



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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
//BAI 1
void showinfor(){
	printf("\nNGUYEN BA HAI "); 
	printf("\nNhap 17 "); 
	printf("\nNhap ASDJHAI ");  
	printf("\nNhap ASDASDF ");  
	printf("\nNhap 111111 ");

}

int main(int argc, char *argv[]) {
//bai 1 
showinfor();

	
	
	return 0;
}



Nguyễn Tiến Đạt [T2008A]
Nguyễn Tiến Đạt

2020-09-18 09:59:39

Bai tap 3


#include<stdio.h>
int giaithuacuan(int n){
	int i,tich=1;
	for(i=1;i<=n;i++)
	tich=tich*i;
	printf("\nGiai thua cua n la %d\n",tich);
	return tich;
}
int tongtu1denm(int m){
	int i,tong=0;
	for(i=1;i<=m;i++)
	tong+=i;
	printf("\nTong cua 1->m la %d\n",tong);
	return tong;
}
int main(){
	int n;
	printf("\nNhap n: ");
	scanf("%d",&n);
	int tich = giaithuacuan(n);
	int m;
	printf("\nNhap m: ");
	scanf("%d",&m);
	int tong=tongtu1denm(m);
	if(tich>tong){
		printf("\nTich lon hon tong");
	}
	else if(tich==tong){
		printf("\nTich bang tong");
	}
	else{
		printf("\nTich be hon tong");
	}
}



vuong huu phu [T2008A]
vuong huu phu

2020-09-18 09:48:26

bai 2


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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
 void showMessage(){
 	int a;
 	printf("\nnhap msg");
 	scanf("%d",&a);
 	
 	printf ("\n Hello %d",a);
}
int main(int argc, char *argv[]) {
showMessage();

showMessage();

showMessage();
	return 0;
}




nguyễn Sử [T2008A]
nguyễn Sử

2020-09-18 09:20:25



	//bai 2
//bat dau code 
void showMessage() {
	
	printf("\nHello msg");
	printf("\nHello msg");
	printf("\nHello msg");

}
	
	int msg;
	
	printf("\nmsg");
	scanf("%d", &msg);
	
	printf("\n%d",msg);
	
	showMessage("Hello msg");
	showMessage("Hello msg");
	showMessage("Hello msg");




	
	return 0;
}



vuong huu phu [T2008A]
vuong huu phu

2020-09-18 09:13:27


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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
 void showInfor(){
 char ten[35], diachi[200], email[150], sdt[20];
	int tuoi;
	
	printf("\n nhap ten: ");
	gets(ten);
	
    fflush(stdin);fflush(stdout);
	printf("\n nhap tuoi ");
	scanf("%d",&tuoi);
	
	fflush(stdin);fflush(stdout);
	printf("\n nhap dia chi ");
	gets(diachi);
	
	fflush(stdin);fflush(stdout);
	printf("\n nhap email ");
	gets(email);
	
	fflush(stdin);fflush(stdout);
	printf("\n nhap so dien thoai ");
	gets(sdt);
	printf("\n%s,%d,%s,%s,%s",ten,tuoi,diachi,email,sdt);
 }
int main(int argc, char *argv[]) {
	showInfor();
	return 0;
}



Nguyễn Tuấn Hùng [T2008A]
Nguyễn Tuấn Hùng

2020-09-18 09:11:20



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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void tong() {
	int sum = 0, i;
	for(i=1; i<=10; i++) {
		sum += i;
	}
	printf("\nTong tu 1->10 = %d", sum);
}

void gt() {
	int gt = 1, i;
	for(i=1; i<=5; i++) {
		gt = gt*i;
	}
	printf("\nGiai thua tu 1->5 = %d", gt);
}


int main(int argc, char *argv[]) {
	tong();
	
	gt();
	
	if(tong >= gt) {
		printf("\nTOng >= gt");
	} else {
		printf("\nTong < gt");
	}
	
	return 0;	
}



An Văn Minh [T2008A]
An Văn Minh

2020-09-18 09:10:34



#include<stdio.h>
#include<stdlib.h>
void showinfor(){	
	printf("\nAn Van Minh");
	printf("\n19 tuoi");
	printf("\nHa Noi");
	printf("\nanvanminh98@gmail.com");
	printf("\n0964124221");
}
int main()
{
	showinfor();
	showinfor();
	return 0;
}