How i can print a number triangle in c++

Apichart Chaiyasat
#include<iostream>
using namespace std;
int main(){
int i=1;
do{
    if(i < 10){
        cout<<"00"<<i<<" ";
    }
    else if (i < 100){
        cout<<"0"<<i<<" ";
    }
    else {
        cout<<i<<" ";
    }
    if(i%10 ==0){
        cout<<endl;
    }
    i++;        
}while (i <= 100);
return 0;
}

this is output

001 002 003 004 005 006 007 008 009 010
011 012 013 014 015 016 017 018 019 020
021 022 023 024 025 026 027 028 029 030
031 032 033 034 035 036 037 038 039 040
041 042 043 044 045 046 047 048 049 050
051 052 053 054 055 056 057 058 059 060
061 062 063 064 065 066 067 068 069 070
071 072 073 074 075 076 077 078 079 080
081 082 083 084 085 086 087 088 089 090
091 092 093 094 095 096 097 098 099 100

this is output that I want:

001
011 012
021 022 023
031 032 033 034
041 042 043 044 045
051 052 053 054 055 056
061 062 063 064 065 066 067
071 072 073 074 075 076 077 078
081 082 083 084 085 086 087 088 089
091 092 093 094 095 096 097 098 099 100
463035818_is_not_a_number

You already know how to print the individual numbers, only the larger pattern is wrong. As the pattern is 2D, your code will be easier when that 2D aspect is also present in your code. What I mean: Instead of iterating i you can iterate row and column:

for (int row=0; row<10; ++row) {
    for (int col=0; col<10; ++col) {
        if ( ...... ) {
            print_number(row,col);
        }
    }
    std::cout << "\n";
}

After each row a newline is printed and you just need to figure out the right condition for the if and implement a print_number function (you already have most of what is needed for that function).

For bonus points you can read about break to improve the logic of the above loop. Alternatively adjust the bounds of the inner loop in such a way that you can remove the if.

Este artículo se recopila de Internet, indique la fuente cuando se vuelva a imprimir.

En caso de infracción, por favor [email protected] Eliminar

Editado en
0

Déjame decir algunas palabras

0Comentarios
Iniciar sesiónRevisión de participación posterior

Artículos relacionados

How can I print multidimensional arrays in C?

how can I find and print the content of an attribute by a given number?

how to print a right angle triangle in c++ using single loop

How to print sign of a number in C?

How can I print values in C at intervals of 1 sec?

How can I print cPacket?

How i print a number in caporal js

How can I calculate and create positions of triangle shape?

How can i render triangle after multiply by projection matrix

How can I check if a string is a whole number in C?

How can I check if the data in a string is a number on C++?

How can I make this number pattern using nested loops in C?

Can't get Right aligned number triangle pattern giving space using setw in C++

How can I print the outliers of a Boxplot in Python?

How can I suppress (not print) line numbers?

How can I print a vector in a recursive function?

How can I print subelements in Python?

How can I print objects to the console?

how can I use a function to print the name of args not use define In c++

How do I print the line number of each line in a string?

How can I print numbers between two numbers that I enter?

How can I split a list composed of number?

How can I do this, but with a changeable number of dice?

How can I remove the number of the heading?

How can I call a method from the C API where the number of arguments is known only at runtime?

How can I convert a random number to a multiple of another number?

How can I round up a number in cell to specific number in Excel

How can i print json object with some other text

How can I print the filename which is being processes by xmltask?