Number Triangle question

I received this problem from my teacher, could someone help me with it?
Number Triangle - Print the following pattern, using horizontal tabs to separate
numbers in the same line. Let the user decide how many lines to print (i.e. what
number to start at):
4
3 3
2 2 2
1 1 1 1

I have this so far:
<#include<iostream>

using namespace std;

int main () {
int x;
int y=0;
cout << "Enter a number." << endl;
cin >>x;
cout << x << endl;
while (x!=0){
cout << x-1 << endl;
cout << y+1 << "\t" << endl;
x--;



}
}>
I know it isn't right, but it's my best attempt thus far. We've also studied for loops and voids.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int main()
{
    std::cout << "enter a number: " ;
    int number ;
    std::cin >> number ;

    int cnt = 1 ;
    while( number > 0 )
    {
        for( int i = 0 ; i < cnt ; ++i ) std::cout << number << ' ' ;
        std::cout << '\n' ;

        --number ;
        ++cnt ;
    }
}
can this be written with a while loop? I've tried numerous times and not getting it.
closed account (48T7M4Gy)
can this be written with a while loop? I've tried numerous times and not getting it.

Yes, they're interchangeable.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

int main()
{
    std::cout << "enter a number: " ;
    int number ;
    std::cin >> number ;
    
    int i = 0;
    
    int cnt = 1 ;
    while( number > 0 )
    {
        i = 0;
        while (i < cnt)
        {
            std::cout << number << ' ' ;
            i++;
        }
        std::cout << '\n' ;
        
        --number ;
        ++cnt ;
    }
}
enter a number: 4
4 
3 3 
2 2 2 
1 1 1 1 
Program ended with exit code: 0
Last edited on
thanks got it.
Topic archived. No new replies allowed.