Question

In: Computer Science

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

Solutions

Expert Solution

// Import required package

import java.util.Scanner;

// create a class for userNum

public class userNum

{

// main function

public static void main(String[] args)

{

// initialize the required variables.

int userNum = 0;

int i_num = 0;

int j_space = 0;

// make an object of scanner class

Scanner scan = new Scanner(System.in);

// Display message to user, asking for input

System.out.print("userNum :");

// take input from user

userNum = scan.nextInt();

// iterate the loop till the user input

for (i_num = 0; i_num <= userNum; i_num++)

{

// iterate the loop for blank spaces

for (j_space = 0; j_space < i_num; j_space++)

{

// Use space in output

System.out.print(" ");

}

// display the number in output.

System.out.println(i_num);

}

// return

return;

}

}

// include required header files

#include <iostream>

using namespace std;

// start with main function

int main()

{

// initialize required variables

int userNum;

int i_num=0;

int j_space=0;

// display message

cout<<"userNum: ";

// take input from user

cin>>userNum;

// iterate loop for numbers accepted by user.

for(i_num=0;i_num<=userNum;i_num++)

{

// iterate loop for spaces.

for(j_space=i_num;j_space>0;j_space--)

{

// display blank spaces.

cout << " ";

}

// display numbers

cout << i_num << endl;

}

return 0;

}


// include required header files

#include <stdio.h>

// start with main function

int main(void) {

// initialize required variables

int userNum;

int i_num=0;

int j_space=0;

// Display message.

printf("userNum: ");

// take user input

scanf("%d", &userNum);

// iterate loop for numbers accepted by user.

for(i_num=0;i_num<=userNum;i_num++)

{

// iterate loop for spaces.

for(j_space=i_num;j_space>0;j_space--)

{

// display blank spaces.

printf(" ");

}

// display numbers

printf("%d\n",i_num);

}

return 0;

}

Code to copy:

# Take input from user and store in userNum variable

userNum=int(input("userNum : "))

# iterate loop in the range of user input

for i_num in range(userNum+1):

# iterate loop for blank spaces

for j_space in range(i_num):

# print blank spaces

    print(" ",end="")

# print the numbers

print(i_num)


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 import java.util.Scanner; public class NestedLoop {    public static void main (String [] args)...
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
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...
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=" ")     ___
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...
USING a LOOP for C++ In this lab the completed program should print the numbers 0...
USING a LOOP for C++ In this lab the completed program should print the numbers 0 through 10, along with their values multiplied by 2 and by 10. You should accomplish this using a for loop instead of a counter-controlled while loop. Instructions Write a for loop that uses the loop control variable to take on the values 0 through 10. In the body of the loop, multiply the value of the loop control variable by 2 and by 10....
Write a program in Python that will print first 100 numbers of the following series: 0,...
Write a program in Python that will print first 100 numbers of the following series: 0, 1, 1, 2, 3, 5, 8……..
Days 1 to 120: Number of New Cases 0 1 0 0 2 0 3 0...
Days 1 to 120: Number of New Cases 0 1 0 0 2 0 3 0 0 0 0 14 2 1 0 27 80 51 18 26 216 81 37 117 167 108 151 178 111 414 337 195 706 214 300 68 118 160 190 209 154 193 170 154 166 68 119 152 146 101 98 159 175 195 195 157 130 129 161 116 133 217 209 75 298 240 205 196 233 76 339 170...
Consider the set of integer numbers from 0 to 9, that is {0, 1, 2, 3,...
Consider the set of integer numbers from 0 to 9, that is {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}. Bob wishes to use these numbers to create a 7-digit password to secure his new laptop. Note that each number can appear in any position (for example, 0 can be the first number in the password). (a) Find the number of 7-digit passwords that are possible. (b) Find the number of 7-digit passwords with distinct digits. (c) Find...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT