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

发布时间:2020-08-06 13:08:15 作者:ZJL2019
来源:网络 阅读:315

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


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

#define _crt_secure_no_warnings 1
#include<iostream>
#include<iomanip>//为了使用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<iostream>
using namespace std;
void leapyear(int i)//判断闰年
{
    if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0){//闰年判断需满足可被4整除以及可被400整除
        cout <<i<< "是闰年" << 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<iostream>
#include<iomanip>
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<<"素数总数为:"<< ' '<<cut << endl;
}

int main(){
    cout << "100-200的素数为:" << endl;
    primenumber();
    system("pause");
    return 0;
}

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

推荐阅读:
  1. python怎么实现输出九九乘法表
  2. 用python输出99乘法表

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

初级程序 c++

上一篇:非中断业务数据库迁移一例

下一篇:Android Studio3.0以后outputfile不能用更改

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》