Can anybody help me solve this problem?
Print numbers 0, 1, 2, ..., userNum as shown, with each number indented by that number of spaces. For each printed line, print the leading spaces, then the number, and then a newline. Hint: Use i and j as loop variables (initialize i and j explicitly). Note: Avoid any other spaces like spaces after the printed number. Ex: userNum = 3 prints:
The code given is as follows:
public class NestedLoop {
public static void main (String [] args) { int userNum = 0; int i = 0; int j = 0; /* Your solution goes here */ return;
}
}Any suggestions would be appreciated. Thank you.
08 Answers
This is a little late but I am doing this same challenge in zybooks for school. The above code marked solved is a little off. Using that code places a space at the beginning of the iteration so it should look more like this so the "whitespace" doesn't differ and because "int i" and "int j" were already defined in the main string...
for (i = 0; i < userNum; i++) { for (j = 1; j < i; j++) { System.out.print(" "); } System.out.println(i);
}Remove the "int" from both "i" & "j" because they have already been defined in your main string. You also have to make "j = 1" otherwise it will put a space before 0 which will have a whitespace difference. Hope this helps anyone else who comes across this challenge.
I don't think i and j are necessary in that sense...
for (int i = 0; i <= userNum; i++) { for (int j = 0; j < i; j++) { System.out.print(" "); } System.out.println(i);
} 1 for (i = 0; i <= userNum; i++) { for (j = 0; j < i; j++) { System.out.print(" "); } System.out.println(i); } 0 So I actually ran the above codes in zybooks, and they didn't work because the relational operators didn't include the userNum, which threw off the result.
Try this:
for (i = 0; i <= userNum; ++i) { for (j = 1; j <= i; ++j) { System.out.print(" "); } System.out.println(i);
} for (i = 0; i <= userNum; i++) { for (j = 1; j <= i; j++) { System.out.print(" "); } System.out.println(i); } 1 After working on this problem myself for 1 and half hours, I got the code below to work correctly in zyBooks. After finishing the problem, I wanted to see what others did to solve this problem, and see that I made it more complicated than it should have been, oh well. I hope this may help anyone else that may have been thinking about the problem in the way I was thinking about it.
userNum = scnr.nextInt(); /* Your solution goes here */ for (i = 0; i <= userNum; ++i) { System.out.println(i); for (j = 0; j <= i; ++j) { if (i == userNum) { System.out.print(""); } else { System.out.print(" "); } } } This was successful when I input it in my zyBook assignment!
#include <stdio.h>
int main(void) { int userNum; int i; int j; scanf("%d", &userNum); for (i=0; i<=userNum; i++){ for (j=0; j<i; j++){ printf(" "); } printf("%d\n", i); } return 0;
} enter code here for (i = 0; i < userNum; ++i) {enter code here System.out.println(i);enter code here for (j = 1; j <=i; ++j) {enter code here System.out.print(" ");
}enter code here System.out.print(" ");