简体   繁体   中英

How do you make a rectangle and triangle shape in C++

How do i achieve this format when i keyed in int of ' 245 ':

(where if it is odd number it will be a rectangle, and even number will be triangle)

 1
 1 2

 1 
 1 2
 1 2 3
 1 2 3 4

 1 2 3 4 5
 1 2 3 4 5
 1 2 3 4 5
 1 2 3 4 5
 1 2 3 4 5

This is my code so far:

(I can't seemed to output triangle and rectangle at the same time)

int n;
int lastDigit;

do
{
    cout << "Enter a positive integer: ";
    cin >> n;

}while ( n <= 1 || n == '0');

cout << endl;

// If even digit - tri
do
{
    lastDigit = n%10;

    if (lastDigit / 2 ==0)
    {
        for (int i = 1; i <= lastDigit; ++i)
            for (int tri = 1; tri <= i; ++tri)
                cout << "\t" << tri;

        cout << endl;
    }

    // if odd digit - rect
    else if (lastDigit / 2 != 0)
    {
        for (int i = 1; i <= lastDigit; i++)
        {
            for (int rect = 1; rect <= i; rect++)
                cout << "\t" << rect;

            cout << endl;
        }
        n = n/10;
    }

    cout << endl;

}while (lastDigit != 0);

n = n/10;
cout << endl;

return 0;

And, how should i code when keyed in the int, the compiler will extract the first digit (From left to right) and output it accordingly?

Any help would be appreciated!

Following is the complete code.

Step1 : Take user input and check whether it's odd or even.

Step2 : If it is odd then perform triangle else rectangle.

#include<iostream>
int main () {

int n;

cout<<"Enter number: ";
cin>>n;

if (n % 2 != 0)
{
    for(int i = 1; i <= n; i++)
    {
        cout<<endl;
        for(int j = 1; j <= n; j++)
        {
            cout<<j<<" ";
        }
    }
}
else
{
    for(int i = 1; i <= n; i++)
    {
        cout<<endl;
        for(int j = 1; j <= i; j++)
        {
            cout<<j<<" ";
        }
    }
}

return 0; 
}

Both of your print is similar. I suggest:

#include<iostream>

using namespace std;

int main () {

    int n;

    cout << "Enter number: ";
    cin >> n;

    int i = 1;
    int& rowLim = ((n % 2) ? n : i);

    for(i = 1; i <= n; i++)
    {
        cout << endl;
        for(int j = 1; j <= rowLim; j++)
        {
            cout<<j<<" ";
        }
    }

    return 0; 

}

Simplest would be to use a string , iterate char by char and output accordingly. For instance

#include <iostream>
using namespace std;

int main() {
    string s;
    cin >> s;
    for (char c : s) {
        int n = c - '0';
        bool k = n % 2;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= (k ? n : i); ++j)
                cout << " " << j;
            cout << endl;
        }
        cout << endl;
    }
    return 0; 
}

Output

 1
 1 2

 1
 1 2
 1 2 3
 1 2 3 4

 1 2 3 4 5
 1 2 3 4 5
 1 2 3 4 5
 1 2 3 4 5
 1 2 3 4 5

See a DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM