13.4k views
3 votes
Write a program that asks the user to input a number, store that

number in a variable called num1, then loop num1 times. Inside the
loop output the value of the i variable. in java

User Atrash
by
7.7k points

1 Answer

1 vote

Final answer:

To write this program in Java, you can use a combination of the Scanner class and a for loop.

Step-by-step explanation:

To write a program in Java that asks the user to input a number, stores it in a variable called num1, and then loops num1 times, you can use a combination of the Scanner class and a for loop.

Here is the code:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num1 = sc.nextInt();

for (int i = 0; i < num1; i++) {
System.out.println(i);
}
}
}

This program will ask the user to input a number, store it in num1, and then loop from 0 to num1-1, printing the value of i in each iteration.

User Herrfz
by
8.3k points