C++ 创建各类三角形图案

 文章目录


前言

介绍完了c++,我们来实例看一下怎样用c++。

一、直角三角形

我们先来做一个简单的三角形。

#include <iostream>
using namespace std;
 
int main()
{
    int rows;
 
    cout << "输入行数: ";
    cin >> rows;
 
    for(int i = 1; i <= rows; ++i)
    {
        for(int j = 1; j <= i; ++j)
        {
            cout << "* ";
        }
        cout << "\n";
    }
    return 0;
}

运行 结果:

*
* *
* * *
* * * *
* * * * *

二、数字直角三角形

继续来做个数字的。

#include <iostream>
using namespace std;
 
int main()
{
    int rows;
 
    cout << "输入行数: ";
    cin >> rows;
 
    for(int i = 1; i <= rows; ++i)
    {
        for(int j = 1; j <= i; ++j)
        {
            cout << j << " ";
        }
        cout << "\n";
    }
    return 0;
}

三、 字母直角三角形

#include <iostream>
using namespace std;
 
int main()
{
    char input, alphabet = 'A';
 
    cout << "输入最后一个大写字母: ";
    cin >> input;
 
    for(int i = 1; i <= (input-'A'+1); ++i)
    {
        for(int j = 1; j <= i; ++j)
        {
            cout << alphabet << " ";
        }
        ++alphabet;
 
        cout << endl;
    }
    return 0;
}

四、倒直角三角形 

#include <iostream>
using namespace std;
 
int main()
{
    int rows;
 
    cout << "输入行数: ";
    cin >> rows;
 
    for(int i = rows; i >= 1; --i)
    {
        for(int j = 1; j <= i; ++j)
        {
            cout << "* ";
        }
        cout << endl;
    }
    
    return 0;
}


总结

四种三角形输出,你学会了吗?

世上本没有路,走的人多了,也便成了路。——鲁迅

猜你喜欢

转载自blog.csdn.net/we123aaa4567/article/details/128006325
今日推荐