To display the numbers from 1 to 5 in Java, you can use a loop and print the numbers to the console. Here's an example of how to do this using a for
loop:
for (int i = 1; i <= 5; i++) { System.out.println(i); }
This will output the following:
1 2 3 4 5
You can also use a while
loop to achieve the same result:
int i = 1; while (i <= 5) { System.out.println(i); i++; }
This will also output the numbers from 1 to 5.
You can use a similar approach to display the numbers from 1 to any other number. Simply change the loop condition to the desired upper limit. For example, to display the numbers from 1 to 10, you can use the following for
loop:
for (int i = 1; i <= 10; i++) { System.out.println(i); }