关于C语言使用do while循环实现求质数

#include<iostream>
#include<cmath>
using namespace std;
int main(){
    int i,j,k,flag;
    i=2;
    do{
        flag=1;
        k=sqrt(i);
        j=2;
        do{
            if(i%j==0){
                flag=0;
                break;
            }
            j++;
        }while(j<=k);
        if(flag)
        cout<<i<<"是质数."<<endl;
        i++;
    }while(i<=100);
    return 0;
}

1. 打印100以内的质数 ```c #include <stdio.h> int main() { int i, j, flag; for (i = 2; i <= 100; i++) { flag = 1; for (j = 2; j <= i / 2; j++) { if (i % j == 0) { flag = 0; break; } } if (flag == 1) { printf("%d ", i); } } return 0; } ``` 2. 使用while循环在/project目录下通过随机小写10个字母加固定字符串project批量创建10个html文件。例如:coaolvajcq_project.html ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> int main() { char filename[30]; int i = 1; srand(time(NULL)); while (i <= 10) { sprintf(filename, "/project/%c%c%c%c%c%c%c%c%c%c_project.html", rand() % 26 + 'a', rand() % 26 + 'a', rand() % 26 + 'a', rand() % 26 + 'a', rand() % 26 + 'a', rand() % 26 + 'a', rand() % 26 + 'a', rand() % 26 + 'a', rand() % 26 + 'a', rand() % 26 + 'a'); FILE *fp = fopen(filename, "w"); if (fp != NULL) { fclose(fp); i++; } } return 0; } ``` 3. 将以上文件名中的project全部改成task (用for循环实现),并且html改成大写HTML ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> int main() { char filename[30]; int i = 1; srand(time(NULL)); while (i <= 10) { sprintf(filename, "/project/%c%c%c%c%c%c%c%c%c%c_project.html", rand() % 26 + 'a', rand() % 26 + 'a', rand() % 26 + 'a', rand() % 26 + 'a', rand() % 26 + 'a', rand() % 26 + 'a', rand() % 26 + 'a', rand() % 26 + 'a', rand() % 26 + 'a', rand() % 26 + 'a'); FILE *fp = fopen(filename, "w"); if (fp != NULL) { fclose(fp); i++; } } for (i = 1; i <= 10; i++) { sprintf(filename, "/project/%c%c%c%c%c%c%c%c%c%c_project.html", rand() % 26 + 'a', rand() % 26 + 'a', rand() % 26 + 'a', rand() % 26 + 'a', rand() % 26 + 'a', rand() % 26 + 'a', rand() % 26 + 'a', rand() % 26 + 'a', rand() % 26 + 'a', rand() % 26 + 'a'); char *pos = strstr(filename, "_project.html"); if (pos != NULL) { memcpy(pos, "_task.HTML", 10); } rename(filename, "/project/newname"); } return 0; } ``` 4. bash for循环打印下面这句话中字母数不大于6的单词real estate price increases in shanghai retreated by nearly 7% last year ```bash for word in real estate price in by 7% last; do echo $word; done ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值