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.
Asked
Active
Viewed 31 times
2 Answers
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
-
So where do you think I should modify to get only numbers from 0-9? – Katherine Aug 25 '15 at 19:30
-
Where are you getting numbers greater than 9? That's where. --- In your case, perhaps the print statements??? – Andreas Aug 25 '15 at 19:32
-
Thanks. I got it now. :D – Katherine Aug 25 '15 at 19:35
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");
}
}

Michael Lasso
- 50
- 5