-1

This is what I have done so far. Although the problem in this code is that in 6th line, after 9 it prints 10,11 and so on instead of starting again at 0,1... etc.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Katherine
  • 281
  • 1
  • 2
  • 13

2 Answers2

1

When you do any calculation (+ or *) that produces an integer that might be >= 10, but you only want numbers 0-9, you really want the remainder when dividing by 10, meaning you want x % 10.

Example: 7 * 2 = 14, 14 % 10 = 4. Combined: (7 * 2) % 10 = 4.

Andreas
  • 154,647
  • 11
  • 152
  • 247
1
public void generate(int integer_input) {
    int count = 0;
    int k = 0;
    int count1 = 0;

    for (int i = 1; i <= integer_input; ++i) {
        for (int space = 1; space <= integer_input - i; ++space) {
            System.out.print(" ");
            ++count;
        }

        while (k != 2 * i - 1) {
            if (count <= integer_input - 1) {
                System.out.print((i + k) % 10);
                ++count;
            } else {
                ++count1;
                System.out.print((i + k - 2 * count1) % 10);
            }
            ++k;
        }
        count1 = count = k = 0;
        System.out.print("\n");
    }
}