In: Computer Science
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 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)