博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用c++实现乘法表输出
阅读量:7064 次
发布时间:2019-06-28

本文共 1621 字,大约阅读时间需要 5 分钟。

看过很多自学C++的朋友们,都是从简单程序入手,这里我分享一下我入门的几个简单的程序。


1.使用c++实现乘法表输出

#define _crt_secure_no_warnings 1#include
#include
//为了使用setw来实现输出占位using namespace std;void multiplicationtable()//乘法表{ int i, j,n; cin >> n; for (i = 1; i < n+1; i++){ for (j = 1; j < i + 1; j++){ cout << setw(5) << i << '*' << j << '=' << i*j; } cout << endl; }}int main(){ cout << "multiplicationtable :" << endl; multiplicationtable(); system("pause"); return 0;}

2.判断1000-2000年的闰年

#define _crt_secure_no_warnings 1#include
using namespace std;void leapyear(int i)//判断闰年{ if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0){//闰年判断需满足可被4整除以及可被400整除 cout <
<< "是闰年" << endl; } else { cout << i << "不是闰年" << endl; }}int main(){And: int k; cout << "请输入1000年-2000年的年份:" << endl; cin >> k; leapyear(k); system("pause");//使程序暂停显示结果 goto And;//使用goto语句使程序可以多次实现功能 return 0;-----}

3.输出100至200的素数

#define _crt_secure_no_warnings 1#include
#include
using namespace std;void primenumber()//素数输出{ int i, j,cut=0; for (i = 101; i < 200; i+=2){ for (j = 2; j <= sqrt(i); j++){ if (i%j == 0) break; } if (sqrt(i) < j) cut++; cout << setw(5) << i; } cout <
<<"素数总数为:"<< ' '<
<< endl;}int main(){ cout << "100-200的素数为:" << endl; primenumber(); system("pause"); return 0;}

相信每个初学者对于C语言中简单的代码,在移植至c++环境下运行都有很多的疑问,可以参考以上3段代码。

转载于:https://blog.51cto.com/14232678/2365572

你可能感兴趣的文章
shell 相关操作
查看>>
网络设备
查看>>
倒计时
查看>>
golang从腾讯 lbs 获取全国省市区及经纬度坐标
查看>>
再谈java乱码:GBK和UTF-8互转尾部乱码问题分析
查看>>
iOS绘图例2:增加Undo/Redo功能
查看>>
python字典操作总结
查看>>
QMake study(part 3)
查看>>
掌握python机器学习-读书笔记4(特征选择)
查看>>
服务器指示灯说明
查看>>
LumiSoft收取邮件(含邮件附件)
查看>>
oracle分页查询
查看>>
Hadoop 2.0 NameNode HA和Federation实践
查看>>
半自动化安装dns
查看>>
MySQL常用命令(转)
查看>>
Linux 下压缩与解压.zip和.rar及.7z文件
查看>>
redis配置
查看>>
MySQL存储过程
查看>>
企业级监控 Zabbix 的安装与使用
查看>>
nginx 和apache 性能测试对比
查看>>