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.
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![To Khuong Loc [C2002L]](https://www.gravatar.com/avatar/494297780288abb9b16b5520549f6fbe.jpg?s=80&d=mm&r=g)
To Khuong Loc
2020-10-24 04:04:28
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void showInfo(char ten[], int tuoi, char diaChi[], char email[], char sdt[]) {
printf("\n ten: %s", ten);
printf("\n tuoi: %d ", tuoi);
printf("\n dia chi: %s ", diaChi);
printf("\n email: %s ", email);
printf("\n sdt: %s ", sdt);
}
void helloMes(char mes[]) {
printf("\nhello %s" , mes);
}
int tinhGiaiThua(int n) {
int kq = 1, i;
for(i = 0; i < n; i++) {
kq *= i + 1;
}
printf("\nket qua giai thua %d! la %d", n, kq);
return kq;
}
int tinhTong(int start, int end) {
int tong = 0, i;
for(i = start; i <= end; i++) {
tong += i;
}
printf("\ntong tu %d den %d la %d", start, end, tong);
return tong;
}
int main(int argc, char *argv[]) {
showInfo("loc", 24, "Ha Noi", "abc@abc.com", "+84343423423423");
helloMes("nguyen van a");
helloMes("nguyen van b");
helloMes("nguyen van c");
int giaiThua = tinhGiaiThua(5);
int tong = tinhTong(1, 10);
if(giaiThua > tong) {
printf("\ngiai thua lon hon tong");
} else if (giaiThua == tong) {
printf("\ngiai thua bang tong");
} else {
printf("\ngiai thua nho hon tong");
}
return 0;
}
![Trần Thị Khánh Huyền [T2008A]](https://www.gravatar.com/avatar/554e115833778e4294a01aebe228f3d6.jpg?s=80&d=mm&r=g)
Trần Thị Khánh Huyền
2020-09-25 09:55:01
#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("\nTen cua ban");
printf("\nTuoi cua ban");
printf("\nDia chi cua ban");
printf("\nEmail cua ban");
printf("\nSdt cua ban");
int main(int argc, char *argv[]) {
showInfor();
return 0;
}*/
void showMessage(){
int msg;
printf("\nHello msg: "); scanf("%d",&msg);
}
int main(int argc, char *argv[]) {
showMessage();
showMessage();
showMessage();
return 0;
}
![Nguyễn đình quân [T2008A]](https://www.gravatar.com/avatar/46aca6afcfe99fdb28357afb847d8a0c.jpg?s=80&d=mm&r=g)
Nguyễn đình quân
2020-09-24 07:50:04
#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 name[35], address[200], email[150], phone[20];
int age;
printf("\n\nNhap ten:");
fflush(stdin);fflush(stdout);
gets(name);
printf("\n\nNhap dia chi:");
fflush(stdin);fflush(stdout);
gets(address);
printf("\n\nNhap email:");
fflush(stdin);fflush(stdout);
gets(email);
printf("\n\nNhap phone:");
fflush(stdin);fflush(stdout);
gets(phone);
printf("\n\nNhap age:");
scanf("%d", &age);
printf("\n%s, %d, %s, %s, %s", name, age, email, phone, address);
}
int main(int argc, char *argv[]) {
//Khai bao bien cua chuong trinh
ShowInfor();
return 0;
}
![Bùi Văn Mạnh [T2008A]](https://www.gravatar.com/avatar/17e9c94870c94e61c9203ee31dccf01c.jpg?s=80&d=mm&r=g)
Bùi Văn Mạnh
2020-09-21 03:31:13
#include<stdio.h>
#include <stdlib.h>
void showmes ()
{
printf ("HELLO MESSSS");
}
int main (){
showmes ();
printf ("\nshowmess(30)");
printf ("\nshowmess(2)");
printf ("\nshowmess(4)");
return 0;
}
![Bùi Văn Mạnh [T2008A]](https://www.gravatar.com/avatar/17e9c94870c94e61c9203ee31dccf01c.jpg?s=80&d=mm&r=g)
Bùi Văn Mạnh
2020-09-21 03:21:31
#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 name[35], address[200], email[150], phone[20];
int age;
printf("\n\nNhap ten:");
fflush(stdin);fflush(stdout);
gets(name);
printf("\n\nNhap dia chi:");
fflush(stdin);fflush(stdout);
gets(address);
printf("\n\nNhap email:");
fflush(stdin);fflush(stdout);
gets(email);
printf("\n\nNhap phone:");
fflush(stdin);fflush(stdout);
gets(phone);
printf("\n\nNhap age:");
scanf("%d", &age);
printf("\n%s, %d, %s, %s, %s", name, age, email, phone, address);
}
int main(int argc, char *argv[]) {
//Khai bao bien cua chuong trinh
ShowInfor();
return 0;
}
![Triệu Văn Lăng [T2008A]](https://www.gravatar.com/avatar/1348e3562c6492c26f796cb1f45982a1.jpg?s=80&d=mm&r=g)
Triệu Văn Lăng
2020-09-20 12:47:24
bai 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 n) {
int gt=1, i;
printf("\nnhap n: "); scanf("%d",&n);
if(n<0) {
printf("\nmoi nhap lai n ");
scanf("%d",&n);
}
for(i=1;i<=n;i++){
gt=gt*i;
}
printf("\nket qua giai thua cua %d la %d",n, gt);
return gt;
}
int tinhtong(int m){
int s=0, t;
printf("\nnhap m: "); scanf("%d",&m);
for(t=0;t<=m;t++) {
s+=t;
}
printf("\ntong tu 1-%d la: %d",m, s);
return s;
}
int main(int argc, char *argv[]) {
giaithua(5);
tinhtong(10);
int gt5=giaithua(5);
int tong10=tinhtong(10);
if(gt5>=tong10) {
printf("\ngiai thua cua 5 lon hon tong tu 1-10");
} else {
printf("\ngiai thua cua 5 nho hon tong tu 1-10");
}
return 0;
}
![Nguyễn Anh Vũ [T2008A]](https://www.gravatar.com/avatar/8863d24ed74b396082becbc4db8331fd.jpg?s=80&d=mm&r=g)
Nguyễn Anh Vũ
2020-09-20 09:10:58
#include<stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
//bat dau code;
//bai 3;
int giaithua(int n){
int i=1,x=1;
while(i<=n){
x = x*i;
i++;
}printf("\nGiai thua cua %d: %d",n,x);
return x;
}
int tong(int a){
int sum = 0, b;
for(b=1;b<=a;b++) {
sum += b;
}
printf("\nTong cua 1->%d: %d", a, sum);
return sum;
int main(){
int Giaithua5=giaithuacuaN(5);
int Tongcua10=tong(10);
if(Giaithua5>=Tongcua10){
printf("\nGiai thua5 lon hon tong tu 1>10");
}else{
printf("\nGiai thua 5 nho hon tong tu 1>10");
}
//ket thuc code;
return 0;
}
![Nguyễn Anh Vũ [T2008A]](https://www.gravatar.com/avatar/8863d24ed74b396082becbc4db8331fd.jpg?s=80&d=mm&r=g)
Nguyễn Anh Vũ
2020-09-20 08:30:33
#include<stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
//bat dau code;
//bai 2;
void showmes ()
{
printf ("HELLO MESSSS");
}
int main (){
showmes ();
printf ("\nshowmess(20)");
printf ("\nshowmess(2)");
printf ("\nshowmess(4)");
}
//ket thuc code;
return 0;
}
![Nguyễn Anh Vũ [T2008A]](https://www.gravatar.com/avatar/8863d24ed74b396082becbc4db8331fd.jpg?s=80&d=mm&r=g)
Nguyễn Anh Vũ
2020-09-20 08:30:32
#include<stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
//bat dau code;
//bai 2;
void showmes ()
{
printf ("HELLO MESSSS");
}
int main (){
showmes ();
printf ("\nshowmess(20)");
printf ("\nshowmess(2)");
printf ("\nshowmess(4)");
}
//ket thuc code;
return 0;
}
![Nguyễn Anh Vũ [T2008A]](https://www.gravatar.com/avatar/8863d24ed74b396082becbc4db8331fd.jpg?s=80&d=mm&r=g)
Nguyễn Anh Vũ
2020-09-20 08:10:28
//bat dau code;
//bai 1
int showinfo;
char ten[10], tuoi[10], diachi[50], email[30], sdt[10];
printf("\nhap ten: ");
scanf("%d, &ten");
gets(ten);
printf("\nhap tuoi");
scanf("%d, &tuoi");
gets(tuoi);
printf("\nhap dia chi");
scanf("%d, &dia chi");
gets(diachi);
printf("\email");
scanf("%d, &email");
gets(email);
printf("sdt");
scanf("%d, &sdt");
gets(sdt);
printf("\nTen: %s", ten);
printf("\nTuoi: %d", tuoi);
printf("\nDiachi: &s", diachi);
printf("\nEmail: %s", email);
printf("\nSdt: %s", sdt);
//ket thuc code;
return 0;
}