Balance Numbers

solve
Last edited on
Have a look at my post over here: http://www.cplusplus.com/forum/general/200987/


----0
---1-2
--3-4-5
-6-7-8-9
0-1-2-3-4

To get the spaces before the first number on each row, do spaces = total_rows - n - 1, where n is the current row number, with the first row being 0. For example, total_rows = 5, n = 0, therefore spaces = 5 - 0 - 1, which is 4. If we look above, we can see that there are 4 spaces on the first row.

Note that if n > 0, that is from the second line onward, there are spaces in between each number. Second row = 1, third row = 2, fourth row = 3 etc... The number of spaces between each number is actually equal to n, the row number(zero indexed).

We can see that as the number reaches 9, it is reset to 0. Note that s = s-10; won't work for numbers greater than 19. A simple way to fix this is to do s %= 10.
Last edited on
closed account (48T7M4Gy)
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
26
27
28
29
30
31
32
#include <iostream>

using namespace std;

int main()
{
    int height;
    int start;
    
    cout << "  Input Height: ";
    cin >> height;
    
    cout << "Input Start No: ";
    cin >> start;
    
    for(int n = 0; n < height; n++)
    {
        for (int k = height; k > n + 1; --k)
        {
            cout << "-";
        }
        
        for (int i = 0; i < n; ++i)
        {
            cout << start % 10 << "*";
            start++;
        }
        cout<<endl;
    }
    
    return 0;
}


Needs a little modification ...
Please DON'T delete your original post after getting a solution. It makes the thread useless as a learning resource for others.

Please go back and edit your post, to reinstate the original question.
Topic archived. No new replies allowed.