c++while-loop

Is that possible by only using while loop?


My tutor gave me an assignment where I have to write a code that only contains while loop and prints:

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

I tried it 100 times and failed 100 times. Due to my limited knowledge, I began to think that my tutor is just messing with my brain. If it's possible, please introduce me a code that prints the numbers in that order. Thanks...


Solution

  •     int i = 1;
        int LIMIT = 5;
        while (i <= LIMIT)
        {
            int j = 1;
            while (j <= LIMIT -i)   //Loop to print the desired space.
            {
                cout << " ";
                j++;
            }
            int k = i;             
            while(k)
            {
                cout<<k;               //Printing the digits
                k--;
            }
            cout << endl;         //Adding new line character at the end.
            i++;
        }
    

    Say hello to your tutor :)