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)
An Văn Minh
2020-09-19 10:09:00
#include<stdio.h>
#include<math.h>
int giaithuacuaN(int n){
int i=1;
int tich=1;
for(int i=1;i<=n;i++){
tich=tich*i;
printf("\nGiai thua cua n=%d", tich);
}
return tich;
}
int tong(int m){
int i=1;
int sum=0;
for(i=1;i<=m;i++){
sum=sum+i;
printf("\nTong tu 1>m=%d", 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");
}
return 0;
}
vuong huu phu
2020-09-19 09:46:49
bai3
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int i;
int g[2]{1,1} ;
void giaithua(int n){
for(i=1;i<=n;i++){
g[0]=g[0]*i;}
printf("\n giai thua = %d ",g[0]);
}
void tong(int m){
for(i=0;i<=m;i++){
g[1]=g[1]+i;}
printf("\n tong = %d ",g[1]);
}
int main(int argc, char *argv[]) {
giaithua(5);
tong(10);
if (g[0]>g[1]){
printf("\n gt lon hon tong");
}else if(g[0]<g[1]){
printf("\n gt nho hon tong");
}else {
printf("\n gt bang tong");
}
return 0;
}
An Văn Minh
2020-09-19 09:45:32
#include<stdio.h>
#include<stdlib.h>
int showMesage(int msg){
if(msg==3){
printf("\nHello msg");
}else
return msg;
}
int main(){
showMesage(1);
showMesage(2);
showMesage(3);
return 0;
}
Do Trung Duc
2020-09-19 09:33:23
//Bai 2
#include <stdio.h>
#include <stdlib.h>
void showMessage (int msg){
printf("\nHello %d", msg);
}
int main(int argc, char *argv[]) {
showMessage(1);
showMessage(2);
showMessage(3);
return 0;
}
Do Trung Duc
2020-09-19 09:31:38
//Bai 3:
#include <stdio.h>
#include <stdlib.h>
int tinhgiaithua(int n){
int giaithua = 1;
int i;
for(i=1; i<=n; i++){
giaithua = giaithua * i;
}
printf("\nGiai thua cua %d = %d", n , giaithua);
return giaithua;
}
int tinhtong(int m){
int tong = 0;
int i;
for(i=0; i<=m; i++){
tong = tong + i;
}
printf("\nTong tu 1 - %d = %d", m, tong);
return tong;
}
int main(int argc, char *argv[]) {
int G,T;
G= tinhgiaithua(5);
T= tinhtong(10);
if(T>=G){
printf("\nTong tu 1 - 10 >= Giai thua cua 5");
} else{
printf("\nTong tu 1 - 10 < Giai thua cua 5");
}
return 0;
}
Do Trung Duc
2020-09-19 08:52:22
#include <stdio.h>
#include <stdlib.h>
void ShowInfor(){
printf ("\n1.Ten: Do Trung Duc\n2.Tuoi: 29\n3.Dia chi: Ha Noi\n4.Email:trungducchk50@gmail.com\n5.Phone: 0985246267");
}
int main(int argc, char *argv[]) {
ShowInfor();
}
Đức Sơn
2020-09-19 07:06:07
#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() {
char ten[20], tuoi[10], diachi[50], email[50], phone[20];
fflush(stdin); fflush(stdout);
printf("\nNhap ten:");
gets(ten);
fflush(stdin); fflush(stdout);
printf("\nNhap tuoi:");
gets(tuoi);
fflush(stdin); fflush(stdout);
printf("\nNhap dia chi:");
gets(diachi);
fflush(stdin); fflush(stdout);
printf("\nNhap email:");
gets(diachi);
fflush(stdin); fflush(stdout);
printf("\nNhap phone:");
gets(phone);
}
int main(int argc, char *argv[]) {
printf("\nHien thi thong tin ca nhan:");
showInfor();
return 0;
}
Triệu Văn Lăng
2020-09-19 04:57:52
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 mgs) {
int chay=0, i;
for(i=0;i<=mgs;i++) {
chay+=i;
}
printf("\nHello mgs %d",mgs,chay);
}
int main(int argc, char *argv[]) {
showMessage(6);
showMessage(10);
showMessage(20);
return 0;
}
Triệu Văn Lăng
2020-09-19 04:40:49
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() {
printf("\nHo ten: Trieu Van Lang");
printf("\nTuoi: 19");
printf("\nDia chi: lang son");
printf("\nEmail: langtvth2008033@fpt.edu.vn");
printf("\nSdt: 0976xxx256");
}
int main(int argc, char *argv[]) {
showInfor();
showInfor();
return 0;
}
Trần Văn Lâm
2020-09-18 15:13:47
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
//BAI 2
void showMessage(){
int msg;
printf("\nNhap msg=");
scanf("%d", &msg);
printf("\nHello msg");
}
int main(int argc, char *argv[]) {
showMessage(2);
showMessage(4);
showMessage(6);
return 0;
}