hungprochuong1c
1. VIẾT HÀM S = 1 + ..... + (2*N + 1)
/* tinh tong day 1+3+5+....+(2*n+1) */
#include<conio.h>
#include<stdio.h>
//================================
int tong(int n)
{int i,s;
for(i=1,s=0;i<2*n+1;i+=2)/* hoac for(i=1,s=0;i<=s;i++)s=s+2*i+1; */
s=s+i;
return s;
}
//================================
void main()
{
clrscr();
int n,s;
char ch;
tiep: printf("
nhap n=");
scanf("%d",&n);
if (n<=0){printf ("
moi nhap lai n");goto tiep;}
s=tong(n);
printf (" tong cua dayla:%d",s);
printf ("
ban co muon tiep tuc c/k");
ch=getch();
if((ch=='c')||(ch=='C')) goto tiep;
else printf ("
cam on va tam biet");
}
2. VIẾT HÀM TÍNH GIAI THỪA
#include<conio.h>
#include<stdio.h>
//==============================
unsigned long giaithua(int n)
{
int i;
unsigned long gt;
for (i=1,gt=1;i<=n;i++)
gt=gt*i;
return gt;
}
/*hoac su dung de quy :
long giaithua (int n)
{
iF (n==0) return 1;
return n*giaithua(n-1);
}
*/
//================================
void main()
{
clrscr();
int n;
char ch;
tiep: printf("
nhap n moi=");
scanf("%d",&n);
if (n<0){printf ("
moi nhap lai n");goto tiep;}
printf("
%d!=%lu",n,giaithua(n));
printf ("
ban co muon tiep tuc c/k");
ch=getch();
if((ch=='c')||(ch=='C')) goto tiep;
else printf ("
cam on va tam biet");
}
3. VIẾT HÀM ĐỔI THÀNH XÂU NHỊ PHÂN
#include<stdio.h>
#include<conio.h>
void chuyen (int n)
{
int i=0,j,a[50];
while (n!=0)
{
a[i]=n%2;
n=n/2;
i++;
}
j=i-1;
for(i=j;i>=0;i--)
printf("%d",a[i]);
}
void main ()
{
clrscr();
int n;
char ch;
printf("\t\t\t Chuyen doi thap phan sang nhi phan");
tiep:printf("
Ban hay nhap so can chuyen doi: ");
scanf("%d",&n);
if (n<0) goto tiep;
printf("
day nhi phan cua %d la:",n);
chuyen (n);
printf("
ban co muon tiep tuc c/k");
ch=getch();
if ((ch=='c')||(ch=='C')) goto tiep;
else printf("
cam on va tam biet");
}
4. VIẾT HÀM S = 1 +...... + 2*N
/* ham tinh tong 2+4+6+..........+2*n*/
#include<conio.h>
#include<stdio.h>
//------------------------------------------
int tong (int n)
{
int i,s;
for(i=1,s=0;i<=n;i++)
s=s+2*i;
return s;
}
//============================================
void main()
{ clrscr();
int n,s;
char ch;
tiep: printf("nhap n=");
scanf("%d",&n);
if (n<=0){printf ("moi nhap lai n");goto tiep;}
s=tong(n);
printf("tong cua day la:%d",s);
printf ("
ban co muon tiep tuc c/k");
ch=getch();
if((ch=='c')||(ch=='C')) goto tiep;
else printf ("
cam on va tam biet");
}
5. BIẾN THÀNH XÂU HEXA
/*bai ni sua tap*/
#include <stdio.h>
void main()
{
unsigned number;
char hex[] = "0123456789ABCDEF";
printf("
Nhap vao mot gia tri nguyen duong 16 bit : ");
scanf("%u", &number);
printf("Gia tri Hex tuong ung = %c%c%c%c",
hex[number/0x1000], hex[(number/0x100)%0x10],
hex[(number/0x10)%0x10], hex[number%0x10]);
getch();
}
6. S = 1^3 + .... + N^3
/* tinh tong s = 1^3 + 2^3 + ... + n^3 */
#include<conio.h>
#include<stdio.h>
//=============================================
int tong(int n)
{
int i,s;
for(i=1,s=0;i<=n;i++)
s=s+i*i*i;
return s;
}
//============================================
main()
{
clrscr();
int n,s;
char ch;
tiep: printf("nhap n=");
scanf("%d",&n);
if (n<=0){printf ("
moi nhap lai n");goto tiep;}
s=tong(n);
printf("tong cua chuoi la:%d",s);
ch=getch();
if((ch=='c')||(ch=='C')) goto tiep;
else printf ("
cam on va tam biet");
return 0;
}
7. TÍNH NGUYÊN TỐ CỦA MỘT SỐ NGUYÊN DƯƠNG
#include<conio.h>
#include<stdio.h>
int nguyento(int n)
{
int i;
for(i=2;i<=n/2;i++)
if(n%i==0)return 0;
return 1;
}
main()
{
clrscr();
int n;
char ch;
tiep: printf("nhap n=");
scanf("%d",&n);
if(n<=1)goto tiep;
if(nguyento(n)==1)
printf("%d la so nguyen to",n);
else
printf("%d khong la so nguyen to",n);
ch=getch();
if((ch=='c')||(ch=='C')) goto tiep;
else printf ("
cam on va tam biet");
return 0;
}
8. S = 1*2*3 + ... + N*(N+1)*(N+3)
/* ham tinh tong 1*2*3+2*3*4+.........n*(n+1)*(n+2),*/
#include<conio.h>
#include<stdio.h>
//=========================================
int tong(int n)
{int s,i;
for (i=1,s=0;i<=n;i++)
s=s+i*(i+1)*(i+2);
return s;
}
//=======================================
main()
{
clrscr();
int n,s;
char ch;
tiep: printf("nhap n=");
scanf("%d",&n);
if (n<=0){printf ("moi nhap lai n");goto tiep;}
s=tong(n);
printf("tong cua day la:%d",s);
printf ("
ban co muon tiep tuc c/k");
ch=getch();
if((ch=='c')||(ch=='C')) goto tiep;
else printf ("
cam on va tam biet");
return 0;
}
9. TÍNH HOÀN THIỆN
#include<conio.h>
#include<stdio.h>
int sht (int n)
{
int i,s;
s=0;
for (i=1;i<n-1;i++)
if (n%i==0)
s=s+i;
if (n==s)return 1;
return 0;
}
void main()
{
int n;
char ch;
tiep:printf("
moi nhap n=");
scanf ("%d",&n);
if (sht(n)==1)
printf("
%d la so hoan thien ",n);
else
printf("
%d la so khong hoan thien ",n);
printf ("
ban co muon tiep tuc c/k");
ch=getch();
if((ch=='c')||(ch=='C')) goto tiep;
else printf ("
cam on va tam biet");
}
10. TÌM SỐ BÉ NHẤT
#include<stdio.h>
#include<conio.h>
float min (int a,int b,int c)
{
float min;
min=a;
if (min>b) min=b;
if (min>c) min=c;
return min;
}
main()
{
clrscr();
float a,b,c;
char ch;
tiep:printf("
nhap a,b,c =");
scanf("%f%f%f",&a,&b,&c);
printf("
so nho nhat trong 3 so la:%5.1f",min(a,b,c));
printf("
co muon tiep tuc khong c/k");
ch=getch();
if((ch=='c')||(ch=='C')) goto tiep;
else printf("
tam biet");
return 0;
}
11. VIẾT HÀM GIẢI PT BẬC 2
include<conio.h>
#include<stdio.h>
#include<math.h>
void ptb2(float a,float b,float c)
{
float x1,x2,delta;
if (a==0)
if (b==0)
if (c==0)
printf(" pt co vo so nghiem");
else
printf(" pt vo nghiem");
else
printf(" pt co nghiem la %5.2f",-c/b);
else
{
delta=(b*b-4*a*c);
if (delta<0)
printf("pt vo nghiem");
else
if(delta==0)
printf(" pt co nghiem la x=%5.2f",-b/2/a);
else
{
x1=(-b+sqrt(delta))/2/a;
x2=(-b-sqrt(delta))/2/a;
printf("pt co hai ngiem x1=%5.2f,x2=%5.2f",x1,x2);
}
}
}
main()
{
clrscr();
float a,b,c;
char ch;
tiep:printf ("
nhap he so a,b,c=");
scanf("%f%f%f",&a,&b,&c);
ptb2 (a,b,c);
printf ("
ban co muon tiep tuc c/k");
ch=getch();
if((ch=='c')||(ch=='C')) goto tiep;
else printf ("
cam on va tam biet");
return 0;
}
12. 3 CẠNH TAM GIÁC
#include<conio.h>
#include<stdio.h>
//--------------------------
void kiemtra (int a,int b,int c)
{
if((a+b>c)&&(a+c>b)&&(b+c>a))
printf("abc la canh cua tam giac");
else
printf("abc khog phai la canh cua tam giac");
}
main()
{clrscr();
float a,b,c;
char ch;
tiep:printf("
nhap vao a,b,c");
scanf("%f%f%f",&a,&b,&c);
kiemtra (a,b,c);
printf ("
ban co muon tiep tuc c/k");
ch=getch();
if((ch=='c')||(ch=='C')) goto tiep;
else printf ("
cam on va tam biet");
return 0;
}
13. ĐẾM TỪ TRONG XÂU
#include<conio.h>
#include<stdio.h>
#include<string.h>
int dem (char a[])
{
int i,s=1;
for (i=0;i<strlen(a);i++)
if (a[i]==' ')
s=s+1;
return s;
}
void main()
{
clrscr();
char s[100],ch;
tiep:printf("
nhap xau can kiem tra: ");
gets (s);
printf("
so tu cua xau vua nhap la:%2.1d",dem(s));
printf("
ban co muong tiep tuc c/k");
ch=getch();
if((ch=='c')||(ch=='C')) goto tiep;
else printf ("
cam on va tam biet");
}
14. TÍNH SỐ FIBONAXI THỨ N
#include<conio.h>
#include<stdio.h>
int fibonaxi(int n)
{
if((n==1)||(n==2)) return 1;
return (fibonaxi(n-2)+fibonaxi(n-1));
}
void main()
{
clrscr();
int n;
char ch;
tiep:printf("
nhap n=");
scanf("%d",&n);
if (n<=0) goto tiep;
printf("
so fibonaxi thu %2.1d la:%2.1d",n,fibonaxi(n));
printf ("
ban co muon tiep tuc c/k");
ch=getch();
if((ch=='c')||(ch=='C')) goto tiep;
else printf ("
cam on va tam biet");
}
15. ĐỔI CHỖ GIÁ TRỊ HAI BIẾN THỰC CHO NHAU
#include<conio.h>
#include<stdio.h>
void doicho(float *a,float *b)
{
float tg;
tg=*a;*a=*b;*b=tg;
}
main()
{
float x,y;
printf("nhap x,y=");
scanf("%f%f",&x,&y);
printf("
gia tri x,y truoc khi doi la:%5.2f,%5.2f",x,y);
doicho(&x,&y);
printf("
gia tri x,y sau khi doi la:%5.2f,%5.2f",x,y);
getch();
}
16. TÌM UCLN
#include<conio.h>
#include<stdio.h>
int uocchung (int a,int b)
{if(a==b) return a;
if(a>b) return uocchung(a-b,b);
return uocchung(a,b-a);
}
main()
{
clrscr();
int a,b;
nhap:printf("
nhap a,b=");
scanf("%d%d",&a,&b);
if((a<0)||(b<0)) goto nhap;
printf("
uoc chung lon nhat la:%d",uocchung(a,b));
getch();
}
17. TÍNH ĐỘ DÀI XÂU KÍ TỰ
#include<conio.h>
#include<stdio.h>
int dem(char a[])
{int i=0;
while (a[i]!=NULL)i++;
return i;
}
void main(void)
{
clrscr();
char s[20];
int t;
printf("
nhap chuoi:");
gets(s);
t=dem(s);
printf("
do dai xau s la :%d",t);
getch();
}
18. ĐẾM CHỮ SỐ IN HOA
#include<conio.h>
#include<stdio.h>
int demhoa (char a[])
{
int i=0,n=0;
while(a[i]!=NULL)
{
if((a[i]>='A')&&(a[i]<='Z'))
n++;
i++;
}
return n;
}
void main(void)
{
clrscr();
char s[20],ch;
int t;
tiep:printf("
nhap chuoi:");
gets(s);
t=demhoa(s);
printf("
so chu hoa la :%d",t);
printf("
ban co muon tiep tuc c/k");
ch=getch();
if ((ch=='c')||(ch=='C')) goto tiep;
else printf("
cam on va tam biet");
}
19. ĐỔI CHỮ IN HOA THÀNH CHỮ IN THƯỜNG
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <conio.h>
void hoa(char *a)
{
for(int i=0;i<strlen(a);i++)
a[i]=toupper(a[i]);
}
void thuong(char *a)
{ hfghgfhfhfhfgh
for(int i=0;i<strlen(a);i++)
a[i]=tolower(a[i]);
}
main(void)
{
char s[200];
printf("Nhap vao 1 chuoi :");
fflush(stdin);
gets(s);
hoa(s);
printf("Chuoi viet hoa : %s",s);
thuong(s);
printf("
Chuoi viet thuong :%s",s);
getch();
}
20. PT BẬC NHẤT
#include<conio.h>
#include<stdio.h>
#include<math.h>
void ptb1(float a,float b)
{
if(a==0)
if(b==0)
printf("phuong trinh vo so ngiem");
else
printf("phuong trinh vo nghiem");
else
printf("phuogn trinh con nghiem x la:%5.2f",-b/2/a);
}
//-------------------------------------------
main()
{
clrscr();
float a,b;
char ch;
tiep:printf("nhap vap a,b=");
scanf("%f%f",&a,&b);
ptb1(a,b);
printf("
co muon tiep tuc khong c/k");
ch=getch();
if((ch=='c')||(ch=='C')) goto tiep;
else printf("
tam biet");
return 0;
}
Bạn đang đọc truyện trên: Truyen247.Pro