Question

In: Computer Science

FOR JAVA Print numbers 0, 1, 2, ..., userNum as shown, with each number indented by...

FOR 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

Solutions

Expert Solution

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<i;j++){
        System.out.print(" ");
      }
      System.out.println(i);
    }
  }
}

Related Solutions

Print numbers 0, 1, 2, ..., userNum as shown, with each number indented by that number...
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
Print numbers 0, 1, 2, ..., userNum as shown, with each number indented by that number...
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 import java.util.Scanner; public class NestedLoop {    public static void main (String [] args)...
Java- Fill in the blanks Print numbers 0, 1, 2, ..., userNum as shown, with each...
Java- Fill in the blanks 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 -------------------------------------------- public class NestedLoop { public static void main...
write a java program that takes three numbers from the user and print the greatest number...
write a java program that takes three numbers from the user and print the greatest number (using if-statement). sample output: input the first number:35 input the second number:28 input the third number:87 the greatest number:87
1: Answer these questions: (a) Write a Java program to print whole numbers between 1 to...
1: Answer these questions: (a) Write a Java program to print whole numbers between 1 to 1000 which are divisible by 3, 5 and by both numbers. (b) Differentiate between instance and local variable in Java (c) In a tabular form, differentiate between instance and class variable
You MUST use a while loop to print numbers 1-100. If the number is divisible by 3 print Soda instead of the number.
You MUST use a while loop to print numbers 1-100. If the number is divisible by 3 print Soda instead of the number. If the number is divisible by 5 print Pop instead of the number. If the number is divisible by 3 AND 5 print *SP*.Print 10 numbers/words to a line. Display the numbers/words in right-aligned columns. Use printf statements to do this. For example:System.out.printf( "5d", number );    System.out.printf( "5s", "Soda" );Every time there are ten numbers on a...
Fill in the blanks of this code to print out the numbers 1 through 7. number...
Fill in the blanks of this code to print out the numbers 1 through 7. number = 1 while number ___ 7:     print(number, end=" ")     ___
Java program to print all the powers of 2 below a certain number. Calculate sum, accept...
Java program to print all the powers of 2 below a certain number. Calculate sum, accept upper limit and make sure sum variable is long type.   Run- Enter the upper limit: 100 5 + 8 + 9 + 11 + 20 + 32 + 30 = 115
def annoying_valley(n): if n == 0: print() elif n == 1: print("*") elif n == 2:...
def annoying_valley(n): if n == 0: print() elif n == 1: print("*") elif n == 2: print("./") print("*") print(".\\") elif n == 3: print("../") print("./") print("*") print(".\\") print("..\\") elif n == 4: print(".../") annoying_valley(3) print("...\\") elif n == 5: print("..../") annoying_valley(4) print("....\\") elif n == 6: print("...../") annoying_valley(5) print(".....\\") else: print("." * (n - 1) + "/") annoying_valley(n - 1) print("." * (n - 1) + "\\") def annoying_int_sequence(n): if n == 0: return [] elif n == 1: return...
Write a Java program that uses nested for loops to print a multiplication table as shown...
Write a Java program that uses nested for loops to print a multiplication table as shown below.   Make sure to include the table headings and separators as shown.   The values in the body of the table should be computed using the values in the heading   e.g. row 1 column 3 is 1 times 3.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT