bai tap C
Bai 1. Đọc số
include<stdio.h>
#include<string.h>
main(){
char s[12];
char dv[4][7]={"","nghin","trieu","ty"};
char doc[10][5]={"","mot","hai","ba","bon","nam","sau","bay","tam","chin"};
int i,j,n,so;
printf("nhap vao so can doc: ");
gets(s);
n=strlen(s);
if (n>12){
printf("khong xu ly duoc");
exit(1);
}
for (i=0;i<n;i++){
so=s[i]-48;
if ((so!=1) &&(so!=0)&& (so!=5)) printf("%s ",doc[so]);
else {
if ((so==5) &&((n-i-1)%3==0) &&(s[i-1]!='0')) printf("lam ");
else if ((so==5) &&((n-i-1)%3!=0)) printf("%s ",doc[5]);
else if (so==1){
if ((n-i-1)%3!=1) printf("mot ");
else printf("muoi'");}
else if ((so==0)&& ((n-i-1)%3==2)&& (s[i+1]=='0' || s[i+2]=='0'))
printf("khong tram ");
}
switch ((n-i-1)%3){
case 0 :
if ((s[i]!='0')||(s[i-1]!='0')||(s[i-2]!='0' )) printf("%s ",dv[(n-i-1)/3]);
break;
case 1:
if ((so!=0) && (so!=1)) printf("muoi ");
else if ((so==0)&& (s[i+1]!='0')) printf("linh ");
break;
case 2:
if (so!=0) printf("tram ");
break;
}
}
printf("
");
}
Bài 2. Fibo
#include<stdio.h>
#include<conio.h>
int f(int i)
{
if(i==1||i==2)
return 1;
else
return f(i-1)+f(i-2);
}
int main()
{
unsigned int i;
printf("moi ban nhap i:",i);
scanf("%d",&i);
//f(i);
printf("gia tri cua fan tu thu i la:%d
",f(i));
getch();
}
Bài3. Tổng các số trong 1 số
#include <stdio.h>
char text[100];
int i,t;
void main()
{
t=0;i=0;
do { printf("hay nhap mot so co n chu so : ");
scanf("%s",text);
}
while ((int) text[i] <48 || (int)text[i]>57);
printf("tong cac so la %d",text[0]-48);
for (i=0;i<strlen(text);i++)
{ t += text[i]-48;
}
for (i=1;i<strlen(text);i++)
{
printf("+ %d",text[i]-48);
}
printf("= %d
",t);
}
Bài 4. Thay thế xâu
#include<stdio.h>
#include<string.h>
main()
{
char s1[30],s2[20],s3[20],s4[30],*found;
printf ("nhap xau:"); gets(s1);
printf ("
nhap xau can tim:"); gets(s2);
printf ("
nhap xau thay the:");gets(s3);
while(found=strstr(s1,s2))
{
printf("
ta duoc xau:");
strncpy(s4,s1,found-s1);
s4[found-s1]='\0';
strcat(s4,s3);
strcat(s4,found+strlen(s2));
strcpy(s1,s4);
}
puts(s1);
}
Bài 5. Tổng các ước nguyên tố
#include <stdio.h>
# include <conio.h>
int tong(int x)
{int t;
if (x%10>0) t = (x%10) + tong(x/10);
else if ((x%10 ==0)&((x/10)>0)) t = t+ tong(x/10);
}
void main()
{
int n;
printf("nhao n = ");scanf("%d",&n);
printf("%d",tong(n));
}
Bài 7. Số nguyên tố
#include<stdio.h>
void main()
{
int n,k;
char kt=1;
printf("nhap%d",&n);
for (k=2;k<=sqrt(n);k++)
if (n%k==0) kt=0;
if (kt) printf("%d la snt",n);
else printf("%d ko la snt",n);
}
Baif8. Tổng số tiền
#include<stdio.h>
#include<conio.h>
#include<string.h>
main(){
char s[13];
int i,n,so;
char doc[10][5]={"","mot ","hai ","ba ","bon ","nam ","sau ","bay ","tam ","chin "};
char dv[5][7]={"dong", "nghin ","trieu ","ty "};
printf("Nhap so tien duoi 12 chu so: ");gets(s);
n = strlen(s);
if(n>12){
printf("So tien qua lon, chua phan tich duoc");
printf("
An phim bat ki de thoat");
exit(1);
}
for(i=0;i<n;i++){
so = s[i]-48;
if(so!=1 && so!=0 && so!=5)
printf("%s",doc[so]);
else
if(so==5 && s[i-1]!='0') printf("nam ");
else if(so==5) printf("lam ");
if(so==1)
if((n-1-i)%3!=1)
printf("mot ");
else
printf("muoi ");
else if(so==0 && ((n-1-i)%3 == 2) && (s[i+1]!='0' || s[i+2]!='0')) printf("khong tram ");
switch((n-1-i)%3){
case 0:
if((s[i-1]!='0') | (s[i-2]!='0') | (s[i]!='0'))
printf("%s",dv[(n-1-i)/3]);
break;
case 1:
if(so!=0 && so!=1) printf("muoi ");
if(so==0 && s[i+1]!='0')printf("linh ");
break;
case 2:
if(so!=0)
printf("tram ");
break;
}
}
getch();
}
Bài 9 . Min, max, tổng
#include<stdio.h>
#include<conio.h>
int sum(int[],int);
int max(int[],int);
int min(int[],int);
void main()
{
int (*troham) (int,int);
int a[20],n,i;
printf("Nhap n: "); scanf("%d",&n);
for(i=0;i<n;i++){
printf("a[%d] = ",i); scanf("%d",&a[i]);
}
troham = sum;
printf("Tong cua day so: %d
",troham(a,n));
troham = max;
printf("Max = %d
",troham(a,n));
troham = min;
printf("Min = %d
",troham(a,n));
getch();
}
int sum(int x[20],int n)
{
int i,tong=0;
for(i=0;i<n;i++)
tong+=x[i];
return tong;
}
int max(int x[20],int n)
{
int i,LN=x[0];
for(i=1;i<n;i++)
if(LN<x[i]) LN=x[i];
return LN;
}
int min(int x[20],int n)
{
int i,NN=x[0];
for(i=1;i<n;i++)
if(NN>x[i]) NN=x[i];
return NN;
}
Bài 10. Đếm tiền
#include<stdio.h>
#include<conio.h>
void main()
{
unsigned long int n,n1,dem,tram,chuc,donvi,sosanh;
printf("n=
");scanf("%ld",&n);
sosanh=n;
while (n>0)
{
n1=n;dem=1;
while (n1>=1000)
{
n1=n1/1000;
dem=dem*1000;
}
tram=n1/100;
chuc=(n1-tram*100)/10;
donvi=n1%10;
switch (tram)
{
case 0:if ((sosanh/10)>(n1*dem)) printf("khong tram ");break;
case 1:printf("mot tram ");break;
case 2:printf("hai tram ");break;
case 3:printf("ba tram ");break;
case 4:printf("bon tram ");break;
case 5:printf("nam tram ");break;
case 6:printf("sau tram ");break;
case 7:printf("bay tram ");break;
case 8:printf("tam tram ");break;
case 9:printf("chin tram ");break;
}
switch (chuc)
{
case 0:if (tram!=0)printf("le ");break;
case 1:printf("muoi ");break;
case 2:printf("hai muoi ");break;
case 3:printf("ba muoi ");break;
case 4:printf("bon muoi ");break;
case 5:printf("nam muoi ");break;
case 6:printf("sau muoi ");break;
case 7:printf("bay muoi ");break;
case 8:printf("tam muoi ");break;
case 9:printf("chin muoi ");break;
}
switch (donvi)
{
case 0:printf("");break;
case 1:printf("mot ");break;
case 2:printf("hai ");break;
case 3:printf("ba ");break;
case 4:if (chuc>1) printf("tu ");
else printf("bon ");break;
case 5:if (chuc!=0)printf("lam ");
else printf("nam ");break;
case 6:printf("sau ");break;
case 7:printf("bay ");break;
case 8:printf("tam ");break;
case 9:printf("chin ");break;
}
if (dem==1000000000) printf("ty ");
if (dem==1000000) printf("trieu ");
if (dem==1000) printf("nghin ");
printf("
");
n=n-(n1*dem);
}
printf("Dong hay do-la hoac eu-ro tuy thich @@
");
getch();
}
Bài 11. Đọc số 2 chữ số
/* doc so co 2 chu so*/
#include<stdio.h>
main()
{
int n;
puts("nhap n"); scanf("%d",&n);
int chuc= n/10; int dv = n % 10;
if (chuc==1) printf("muo'i "); if(chuc==2) printf("hai ");
if (chuc==3) printf("ba "); if(chuc==4) printf("bon ");
if (chuc==5) printf("nam "); if(chuc==6) printf("sau ");
if (chuc==7) printf("bay "); if(chuc==8) printf("tam ");
if (chuc==9) printf("chin ");
if (chuc>1)
{
printf("muoi ");
if (dv==1) printf ("mo^'t ");
if (dv==4) printf ("tu ");
if (dv==5) printf ("lam ");
}
else {
if(dv==1) printf("mot");
if((chuc==1)&&(dv==5)) printf("lam ") ;
}
if(dv==2) printf("hai"); if(dv==3) printf("ba");
if(dv==6) printf("sau"); if(dv==7) printf("bay");
if(dv==8) printf("tam"); if(dv==9) printf("chin");
printf("
");
}
Bài 12. đổi tiền Cách 2
#include <stdio.h>
#include <conio.h>
#define max 3
int main()
{
int loaitien[max] = {20,10,5,};
int i,sotien,soto;
printf("nhap so tien: ");
scanf("%d",&sotien);
for (i=0;i<max;i++)
{
soto=sotien/loaitien[i];
printf("%4d to %2d dong
",soto,loaitien[i]);
sotien = sotien%loaitien[i];
}
getch();
}
Bài 13. Max, min
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define n 100
void main()
{
int mang[n][n];
int i, j, imin, jmin, imax, jmax, tong;
for (i = 0; i < 5; i++)
for (j = 0; j < 5; j++)
{
mang[i][j] = rand()%1000;
printf("%5d ", mang[i][j]);
if (j==4)
printf("
");
}
/* yeu cau 2: tim so lon nhat nho nhat */
imin = 0; jmin = 0;
imax = 0; jmax = 0;
for (i =0;i< 5; i++)
for (j = 0; j < 5; j++)
{
if (mang[i][j] > mang[imax][jmax])
{
imax = i;
jmax = j;
}
if (mang[i][j] < mang[imin][jmin])
{
imin = i;
jmin = j;
}
}
printf("So lon nhat cua mang la %d
", mang[imax][jmax]);
printf("So nho nhat cua mang la %d
", mang[imin][jmin]);
/* yeu cau 3: tim tong duong cheo chinh */
tong = 0;
for (i = 0; i < 5; i++)
for (j = 0; j < 5; j++)
if (i == j)
tong = tong + mang[i][j];
printf("Tong duong cheo chinh cua ma tran la: %d
", tong);
getch();
/* yeu cau 4: xap xep theo hang */
for (i = 0; i < 5; i++)
for (j = 0; j < 5; j++)
mang1[i][j] = mang[i][j];
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
for (k = 0; k < 5; k++)
{
if (mang1[i][j]< mang1[i][k])
{
mang1[i][k]+=mang1[i][j];
mang1[i][j]=mang1[i][k]-mang1[i][j];
mang1[i][k]= mang1[i][k]-mang1[i][j];
}}}}
Bài 14. Con trỏ hàm
#include<stdio.h>
#include<conio.h>
int mang(int x,int y);
{
return (x>y ?x:y);
}
main ()
{
int i; int j;
int mang1[5],s,*pm;
pm=mang1;
int min,max(int i,int j);
int tong;
printf("nhap day so:
",k); gets(n);
min=0; max=0;
for (i=0;i<=5;i++)
for(j=i+1;j<=5;j++)
{
if (i<j;i<min;j>max)
{
min=i;
max=j;
}
else if (i>j;i>max;j<min)
{
min=i;
max=i;
}
printf("So lon nhat la %d
", max);
printf("So nho nhat la %d
", min);
tong=max+min;
printf("tong gia tri max,min:%d\=%d\+%d\",tong,max,min);
getch();
{
printf("
m[%d]= ",i);
scanf ("%d",pm+i);
}
for (s=0,i=0;i<5;i++)
s+=*(pm+i);
printf ("
tong=%d",s);
}
Bài 15. Tổng các Ước nguyên dương
#include<stdio.h>
#include<conio.h>
int tong(int n)
{
int i;
int tong = 0;
for (i=2;i<=n;i++)
{
while (n%i==0)
{
tong=tong+i;
n=n/i;
}
}
return tong;
}
void main()
{
int n;
printf ("nhap vao so n:
");
scanf ("%d",&n);
printf ("tong thua so nguyen to cua n la: %d",tong(n));
getch(); }
Bài 16. Thay thế xâu. Cách 2!
#include<stdio.h>
#include<string.h>
main(){
char s1[30],s2[20],s3[20],s4[30],*found;
printf("Nhap xau ban dau: "); gets(s1);
printf("
Nhap vao xau tim kiem: "); gets(s2);
printf("
Nhap xau thay the: "); gets(s3);
while (found=strstr(s1,s2)){
printf("
ta duoc xau: ");
strncpy(s4,s1,found-s1);
s4[found-s1]='\0';
strcat(s4,s3);
strcat(s4,found+strlen(s2));
strcpy(s1,s4);
} puts(s1); }
Bạn đang đọc truyện trên: Truyen247.Pro