/* Bai tap on tap
Bai1. In ra man hinh tat ca nhung so nguyen to nho hon 2013
Bai2. Viet ham:
- Nhap vao mot xau ky tu co khong qua 100 ky tu
- In xau
- Dem so ky tu ch co trong xau (ch: nhap tu ban phim)
BTVN: - Chuyen cau truc for -> while -> do/while
- Chuyen if -> while
*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<math.h>
//
char xau[100];
// Kiem tra so nguye to
int TestNT(int a){
int i=2;
while(i<=(int)sqrt(a)){
if (a%i==0) return 0;
i++;
}
return 1;
}
// In so nguye to
void InSNT(){
printf("\n In so nguyen to: \n");
for (int i=1;i<2013;i++)
if (TestNT(i)==1) printf("%d; ",i);
}
//2.
// Nhap xau
void Nhap(){
printf("\n Nhap xau: ");
fflush(stdin);
gets(xau);
}
// In xau
void InXau(){
printf("\n In xau: %s",xau);
}
// Dem ki tu
void Dem(){
int dem=0;
char ch;
printf("\n Nhap ky tu can dem:");
scanf("%c",&ch);
for(int i=0;i<strlen(xau);i++)
if (xau[i]==ch) dem++;
printf("\n So lan xuat hien cua ky tu %c la: %d",ch,dem);
}
int main(){
int k;
printf("\n Chon 1: Bai 1 \n Chon 2: Bai 2");
printf("\n An so: ");
scanf("%d",&k);
switch (k){
case 1:
InSNT();
break;
case 2:
Nhap();
InXau();
Dem();
break;
}
getch();
return 0;
}
0 nhận xét:
Đăng nhận xét