Skip to content

Print 1 to N

Print numbers 1 to N read from user input.

package DSA.Flow_control;
import java.util.Scanner;
public class oton {
public static void main(String[] args) {
try (Scanner sc = new Scanner(System.in)) {
System.out.println("Enter the number");
int n = sc.nextInt();
int c = 1;
while (c <= n) {
System.out.print(c + " ");
c++;
}
} catch (Exception e) {
System.out.println(e);
}
}
}