#include<stdio.h>
#include<conio.h>
main()
{
int n,tong = 0;
printf("nhap vao mot so nguyen: ");
scanf("%d",&n);
int i=n%10;
printf("tong cac chu so cua %d la:",n);
while(i!=0)
{
i=n%10;
tong=tong+i;
n=n/10;}
printf("%d",tong);
getch();
}
2. Chuỗi palidrom
#include<stdio.h>
#include<conio.h>
main()
{ char n[20];
printf("nhap vao mot chuoi: ");
scanf("%ch",&n);
if(palidrom(n)==1) printf("chuoi la palidrom");
else printf("chuoi khong phai la palidrom");
getch();
}
int palidrom(char n[20])
{
int i;
int l=strlen(n);
for(i=0;i<=l/2;i++)
{
if(n[i]!=n[l-i-1]) return 0;
else return 1;
}}
3. Vay tiền ngân hàng
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c;
printf("thu nhap hang nam la: ");
scanf("%d",&a);
printf("tai san cua khach hang: ");
scanf("%d",&b);
printf("so no cua khach hang: ");
scanf("%d",&c);
if(((a>=18000000)||(b>=100000000))&&(c<=36000000))
printf("cho vay");
else
printf("khong cho vay");
getch();
}
4. Giai thừa và tổng giai thừa
#include<conio.h>
#include<stdio.h>
float giai_thua(float n)
{
if(n==0) return 1;
else return(n*giai_thua(n-1));
}
float tong_giai_thua(float n)
{
if(n<=0) return 1;
else
{
if(n==1) return 1;
else return 1/giai_thua(n) +tong_giai_thua(n-1);
}
}
int main()
{
float n;
printf("\nNhap n:");
scanf("%f",&n);
printf("%.1f!=%.3f",n,giai_thua(n));
printf("\nTong giai thua la:%.2f",tong_giai_thua(n));
getch();
}
5. Mảng hai chiều
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#define max 100
void nhap(int a[], int n)
{
for(int i=1; i<=n; i++)
{
printf("Nhap pt a[%d]: ",i);
scanf("%d", &a[i]);
}
}
void ghifile(int a[],int n)
{
FILE *f;
f=fopen("bcdonline.text","wt");
fprintf(f,"%d",n);
for(int i=1;i<=n;i++)
fprintf(f,"%3d",a[i]);
fclose(f);
}
int main()
{
nhap();
ghifile();
getch();
}
0 comments:
Post a Comment