CÁc bài tập C căn bản P4
---------------------------------
/* Bai tap 1_93 - In tat ca cac uoc so cua so n */
#include <stdio.h>
#include <conio.h>
void main()
{
int n, i;
printf("Cho gia tri N = ");
scanf("%d", &n);
printf("Cac uoc so cua %d la :\n", n);
for (i=1; i<n; i++)
if ((n % i) == 0)
printf("%5d", i);
getch();
}
----------------------------
#include <stdio.h>
unsigned USCLN (unsigned n, unsigned m)
{
while (n != 0 && m != 0)
if (n>m)
n -= m;
else
m -= n;
if (n == 0)
return m;
else
return n;
}
unsigned BSCNN (unsigned n, unsigned m)
hang[nline] = (char *)malloc(strlen(s));
strcpy(hang[nline], s+1);
nline++;
}
do {
clrscr();
i = line;
while (i<line+26 && i<nline)
printf("%s", hang[i++]);
c = getch();
if (c == 0)
{
c = getch();
switch(c)
{
case 72 : if (line > 0)
line--;
break;
case 80 : if (line+25 < nline)
line++;
break;
}
}
} while (c != 27);
fclose(fp);
}
}
---------------------------
/* Bai tap 9_5 - Tim so tu trong chuoi */
#include <stdio.h>
getch();
}
--------------------------------------
--------------------------------
#include <stdio.h>
void main()
{
char s[100];
int i;
printf("\nNhap vao mot chuoi : ");
gets(s);
i = 0;
while (s[i] != 0)
{
if (isalpha(s[i]))
if (s[i] < 97)
s[i] += 32;
else
s[i] -= 32;
i++;
}
printf("\nChuoi bien thanh : %s", s);
getch();
}
---------------------------------------
/* Bai tap 5_3 - Tim phuong an doi tien */
#include <stdio.h>
#define TONGSOTIEN 300000
void main()
{
}
------------------------------------------
/* Bai tap 1_87 - Demo ve hop dung conio */
#include <conio.h>
#include <stdlib.h>
#include <dos.h>
char doublebox[] ="ÉÍ»ºÈ¼";
char singlebox[] ="ÚÄ¿³ÀÙ";
void swap(int *x, int *y)
{
int temp;
temp = *y;
*y = *x;
*x = temp;
}
void drawbox(int x1, int y1, int x2, int y2, int type)
{
char *boxtype;
int i;
if (type == 2)
boxtype = doublebox;
else
boxtype = singlebox;
gotoxy(x1,y1);
cprintf("%c", boxtype[0]);
for (i=x1 + 1; i < x2; i++)
cprintf("%c", boxtype[1]);
cprintf("%c", boxtype[2]);
for (i=y1+1; i<y2; i++)
{
do {
x2 = random(80);
} while (x1 == x2);
y1 = random(25);
do {
y2 = random(25);
} while (y1 == y2);
type = random(2) + 1;
color = random(256);
drawbox_color(x1,y1,x2,y2,type,color);
delay(100);
}
}
-----------------------------------------
#include <stdio.h>
void main()
{
double fahrenheit, celsius;
printf("\nNhap gia tri do Fahrenheit : ");
scanf("%lf", &fahrenheit);
if (fahrenheit != 32.0)
{
celsius = 5.0 * (fahrenheit-32.0) / 9.0 ;
printf("Do Celsius tuong ung = %5.2lf", celsius);
}
getch();
}
----------------------------------------
/* Bai tap 1_21 - In day so Fibonanci */
#include <stdio.h>
if ((fp2 = fopen(filename2, "w+")) == NULL)
printf("\nKhong the tao tap tin %s", filename2);
else
{
do {
printf("\nHo ten (ENTER de ket thuc) : ");
gets(s);
if (strlen(s) > 0)
{
strcat(s, "\n");
fputs(s, fp1);
printf("\nTuoi : ");
gets(s);
strcat(s, "\n");
fputs(s, fp1);
printf("\nDia chi : ");
gets(s);
strcat(s, "\n");
fputs(s, fp1);
}
} while (strlen(s) > 0);
fclose(fp1);
fp1 = fopen(filename1, "r");
while (fgets(s, 100, fp1) != NULL)
{
fgets(s1, 100, fp1);
fgets(s2, 100, fp1);
s[strlen(s)-1] = 0;
s1[strlen(s1)-1] = 0;
fprintf(fp2, "%s %s %s", s, s1, s2);
}
---------------------------------------
/* Bai tap 3_1 - Ham tinh N! */
#include <stdio.h>
unsigned long giaithua(int n)
{
unsigned long ketqua = 1;
int i;
for (i=2; i<=n; i++)
ketqua *= i;
return ketqua;
}
void main()
{
int n;
printf("\nNhap vao gia tri N : ");
scanf("%d", &n);
printf("%d! = %lu", n, giaithua(n));
getch();
}
----------------------------------
#include <stdio.h>
void main()
{
int a[10], i;
printf("\nNhap vao 10 phan tu nguyen cua day : ");
for (i=0; i<10; i++)
scanf("%d", &a[i]);
i = 1;
while (a[i] <= a[i-1] && i<10)
break;
if (j >= i)
nguyento[k++] = i;
}
for (n=0; n<k; n++)
for (i=0; i<k; i++)
for (j=0; j<k; j++)
for (l=0; l<k; l++)
if (nguyento[i] + nguyento[j] + nguyento[l] == nguyento[n])
printf("\n%ld = %ld + %ld + %ld", nguyento[n], nguyento[i],
nguyento[j], nguyento[l]);
getch();
}
-----------------------------------------
/* Bai tap 1_20 - Tinh n!! */
#include <stdio.h>
void main()
{
int n, start, i;
unsigned long gthua = 1;
printf("\nNhap gia tri N : ");
scanf("%d", &n);
if (n%2 == 0)
start = 2;
else
start = 1;
for (i=start; i<=n; i = i+2)
gthua *= i;
printf("\n%d!! = %ld", n, gthua);
getch();