C++实现1.打印100~200之间的素数;2.打印9*9乘法口诀;3.打印1000到2000年之间的所有闰年.

/////////////////////////////
1.打印100~200之间的素数
////////////////////////////
步骤:1.判断素数
2.循环比较100~200之间的数字
#include<stdio.h>
#include<stdlib.h>
int IsPrime(int x){
int num = 2;
//任何数都能整除1,故应从二开始
while (num < x){
//%含义是求模(求余数)
if (x % num == 0){
//x被num整除,说明x不是素数
return 0;
//不是素数返回零
}
num = num + 1;
}
return 1;
}
int main(){
int i = 100;
while (i < 200){
if (IsPrime(i) == 1){
printf("%d\n", i);
}
i += 1;
}system(“pause”);
return 0;
}
/////////////////////////////
2.打印99乘法口诀表
////////////////////////////
#include<stdio.h>
#include<stdlib.h>
int main(){
int line = 1;
while (line < 10){
//每循环一次,打印一次
int col = 1;
while (col < line){
//打印一个格子
printf("%d
%d=%d “, col, line, col*line);
col += 1;
}
printf(”\n");
line += 1;
}
system(“pause”);
return 0;
}
/////////////////////////////
3.打印1000~2000年之间的所有闰年
////////////////////////////
#include<stdio.h>
#include<stdlib.h>
int IsLeapyear(int year){
if (year % 100 == 0){
//世纪年
if (year % 400 == 0){
return 1;
}
else{
return 0;
}
}
else{
//普通年
if (year % 4 == 0){
return 1;
}
else{
return 0;
}
}
}
int main(){
int year = 1000;
while (year <= 2000){
if (IsLeapyear(year)==1){
printf("%d\n", year);
}
year += 1;
}
system(“pause”);
return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44648896/article/details/88246203