2018.9.16练习

版权声明:Zhining https://blog.csdn.net/weixin_43214609/article/details/82728143

1.100到200之间的素数

素数就是除了1和它本身不可被别的数所整除。
那么就可以用两个for循环来实现,一个作为所要判断的数,一个做为要除的数,从2开始且不等于它本身。
定义count,表示能整除的个数,count=0时,避免重复输出。
具体代码如下:

#include <stdio.h>
#include <stdlib.h>


int main() {
int i, j, count;
	for (i = 101; i <= 200; i++)
	{
		count = 0;
		for (j = 2; j < i; j++)
			if (i%j == 0)
			{
				count++; break;
			}
		if (count == 0)
			printf("素数有:%d\n",i);
	}
			
	system("pause");
	return 0;

}

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


2.乘法口诀表

具体代码如下:

#include <stdio.h>
#include <stdlib.h>

int main() {

    //乘法口诀表
	int i, j;
	for (i = 1; i <= 9; i++)
	{for (j = 1; j <= i; j++)
		printf("%d*%d=%d ", i, j, i*j);
		printf("\n");
		}
	system("pause");
	return 0;
	}

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


3.判断1000年到2000年的闰年

可以被400整除***或者***可以被4整除但不能被100整除的。
具体代码如下:

#include <stdio.h>
#include <stdlib.h>

int main() {
  int i;
	for (i = 1000; i <= 2000; i++) 
	{ 
		if (i % 400 == 0 || (i % 100 !=0 && i % 4 == 0))
			printf("%d年是闰年。\n", i);
	}
	system("pause");
	return 0;
}

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


从最简单开始一点点努力!

猜你喜欢

转载自blog.csdn.net/weixin_43214609/article/details/82728143