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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值