Problem Description
In this problem, we will learn how to use for loop in Java with the help of examples and we will also learn about the working of Loop in computer programming.
In computer programming, loops are used to repeat a block of code. For example, if you want to show a message 100 times, then rather than typing the same code 100 times, you can use a loop.
Java for Loop
Java for loop is used to run a block of code for a certain number of times. The syntax of for loop is:
for (initialExpression; testExpression; updateExpression) { // body of the loop }
Here,
Example: Display numbers from 1 to 5
// Program to print numbers from 1 to 5 class Main { public static void main(String[] args) { int n = 5; // for loop for (int i = 1; i <= n; ++i) { System.out.println(i); } } }
Task:
Given an integer n as an input print the numbers from 1 to n in separate lines.
Problem Constraints
1 <= n <= 100
Input Format
A single line of input containing an integer n.
Output Format
Print all the numbers from 1 to n in separate lines.
Example Input
Input 1:
5
Example Output
Output 1:
1 2 3 4 5