1. 打印100~200 之间的素数 2. 输出乘法口诀表 3. 判断1000年---2000年之间的闰年

来源:互联网 发布:工作照软件 编辑:程序博客网 时间:2024/05/19 18:48

1. 打印1000~2000 之间的素数

方法1:

#include <stdio.h>void test(){    int i = 0;    int j = 0;    int count = 0;    for(i=100; i<=200; i++)    {        for(j=2; j<i; j++)            if(i%j == 0)                break;            if(i == j)                printf("%d ",i);                count++;    }    printf("\ncount = %d\n",count);}int main(){  test();  return 0;}

方法二:

#include <stdio.h>#include <math.h>void test(){    int i = 0;    int j = 0;    int count = 0;    for(i=101; i<=200; i+=2)//从101这个奇数开始,因为偶数都不是素数    {        for(j=2; j<=sqrt(i); j++)//减少循环次数        {            if(i%j == 0)                break;        }        if(j>sqrt(i))        {            count++;            printf("%d ", i);        }    }    printf("\ncount = %d\n", count);}int main(){  test();  return 0;}

运行结果:
这里写图片描述
方法二:

2. 输出乘法口诀表

代码实现:

#include <stdio.h>void test(){    int i = 0;    int j = 0;    for(i=1; i<= 9; i++)    {        for(j=1; j<=i; j++)        {            printf("%d*%d=%2d ",i,j,i*j);        }        printf("\n");    }}int main(){  test();  return 0;}

运行结果:
这里写图片描述

3. 判断1000年—2000年之间的闰年

代码实现:

#include <stdio.h>void test(){    int year = 0;    int count = 0;    for(year=1000; year<=2000; year++)    {        if(((year%4 == 0)&&(year%100 != 0))||(year%400 == 0))        //判断是否为闰年 如果满足 整除4并且不整除100  或者 整除400,则该年为闰年       {           printf("%d ",year);           count++;       }    }    printf("\ncount = %d\n",count);}int main(){    test();    return 0;}

运行结果:
这里写图片描述

阅读全文
0 0