Question

4.9.1: Nested loops: Indent text. JAVA Print numbers 0, 1, 2, ..., userNum as shown, with...

4.9.1: Nested loops: Indent text. JAVA

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:

0
 1
  2
   3

Please use my template

import java.util.Scanner;
public class NestedLoop {
public static void main (String [] args) {
Scanner scnr = new Scanner(System.in);
int userNum;
int i;
int j;

userNum = scnr.nextInt();

/* Your solution goes here */

}
}

Homework Answers

Answer #1
import java.util.Scanner;

public class NestedLoop {
    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        int userNum;
        int i;
        int j;

        userNum = scnr.nextInt();
        for (i = 0; i <= userNum; ++i) {
            for (j = 0; j <= userNum; ++j) {
                if (i == j) {
                    System.out.print(i);
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write an if-else statement that checks patronAge. If 55 or greater, print "Senior citizen", otherwise print...
Write an if-else statement that checks patronAge. If 55 or greater, print "Senior citizen", otherwise print "Not senior citizen" (without quotes). End with newline. import java.util.Scanner; public class DetectSenior {    public static void main (String [] args) {       Scanner scnr = new Scanner(System.in);       int patronAge; //Solution goes here//       patronAge = scnr.nextInt();    } }
Write an expression that will cause "greater or equal to -10" to print if the value...
Write an expression that will cause "greater or equal to -10" to print if the value of userNum is greater than or equal to -10. import java.util.Scanner; public class EqualityAndRelational {    public static void main (String [] args) {       int userNum;       Scanner scnr = new Scanner(System.in);       userNum = scnr.nextInt(); // Program will be tested with values: -9, -10, -11, -12.       if (//solution goes here//) {          System.out.println("greater or equal to -10");       }       else...
Write a for loop to print all elements in courseGrades, following each element with a space...
Write a for loop to print all elements in courseGrades, following each element with a space (including the last). Print forwards, then backwards. End each loop with a newline. Ex: If courseGrades = {7, 9, 11, 10}, print: 7 9 11 10 10 11 9 7 Hint: Use two for loops. Second loop starts with i = courseGrades.length - 1. (Notes) IN JAVA: import java.util.Scanner; public class CourseGradePrinter { public static void main (String [] args) { Scanner scnr =...
4.2.2 Basic while loop with user input. JAVA Write an expression that executes the loop while...
4.2.2 Basic while loop with user input. JAVA Write an expression that executes the loop while the user enters a number greater than or equal to 0. Note: These activities may test code with different test values. This activity will perform three tests, with user input of 9, 5, 2, -1, then with user input of 0, -17, then with user input of 0 1 0 -1. See "How to Use zyBooks". Also note: If the submitted code has an...
in JAVA Write a switch statement that checks origLetter. If 'a' or 'A', print "Alpha". If...
in JAVA Write a switch statement that checks origLetter. If 'a' or 'A', print "Alpha". If 'b' or 'B', print "Beta". For any other character, print "Unknown". Use fall-through as appropriate. End with newline. import java.util.Scanner; public class ConvertToGreek { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); char origLetter; origLetter = scnr.next().charAt(0); /* Your solution goes here */ } }
Write a for loop that prints: 1 2 ... userNum Ex: userNum = 4 prints: 1...
Write a for loop that prints: 1 2 ... userNum Ex: userNum = 4 prints: 1 2 3 4 import java.util.Scanner; public class ForLoops { public static void main (String [] args) { int userNum; int i; Scanner input = new Scanner(System.in); userNum = input.nextInt(); for (/* Your code goes here */) { System.out.print(i + " "); } } } Need to fill out the "for" solved in JAVA
Write an expression that will cause "Foot or more" to print if the value of numInches...
Write an expression that will cause "Foot or more" to print if the value of numInches is greater than or equal to 12. import java.util.Scanner; public class EqualityAndRelational {    public static void main (String [] args) {       int numInches;       Scanner scnr = new Scanner(System.in);       numInches = scnr.nextInt(); // Program will be tested with values: 10, 11, 12, 13.       if (/* Your solution goes here */) {          System.out.println("Foot or more");       }       else {...
Summary In this lab, you add nested loops to a Java program provided. The program should...
Summary In this lab, you add nested loops to a Java program provided. The program should print the letter E. The letter E is printed using asterisks, three across and five down. Note that this program uses System.out.print("*"); to print an asterisk without a new line. Instructions Write the nested loops to control the number of rows and the number of columns that make up the letter E. In the loop body, use a nested if statement to decide when...
Write a loop that sets each array element to the sum of itself and the next...
Write a loop that sets each array element to the sum of itself and the next element, except for the last element which stays the same. Be careful not to index beyond the last element. Ex: Initial Scores: 10, 20, 30, 40 Scores after loop: 30, 50, 70, 40 Import. java.util.Scanner; public class StudentScores { public static void main (String [] args){ Scanner scnr = new Scanner (System.in); final int SCORES_SIZE = 4; int [] bonusScores = new int[SCORES_SIZE]; int...
In Java ReWrite the for loop program code example 3, above as a while loop. Code...
In Java ReWrite the for loop program code example 3, above as a while loop. Code Example 3 – Try it import java.util.Scanner; public static void main(String args[]) { int count = 0; int maxNumber = 0; int enteredNumber = -9999999; System.out.println("This program determines which entered number is the largest "); System.out.println("Enter count of numbers to check: "); Scanner scanIn = new Scanner(System.in); double count = scanIn.nextDouble(); scanIn.close(); // Print out message – input number – compare to see if...