Skip to content

Commit 2a57d41

Browse files
committed
feat: Add OneToTen class to print numbers from 1 to user-specified limit
Add a program that: - Prompts the user to enter a positive number - Prints numbers from 1 up to the entered value using a `for` loop - Includes input validation for positive integers - Demonstrates use of `Scanner`, conditional logic, and loop control in Java.
1 parent 3836276 commit 2a57d41

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import java.util.Scanner;
2+
3+
public class OneToTen {
4+
public static void main(String[] args) {
5+
Scanner scanner = new Scanner(System.in);
6+
System.out.print("Enter a number to print from 1 to that number: ");
7+
8+
int number = scanner.nextInt(); // Take input from the user
9+
10+
// Check if the user entered a valid positive number
11+
if (number > 0) {
12+
for (int i = 1; i <= number; i++) {
13+
System.out.println(i);
14+
}
15+
} else {
16+
System.out.println("Please enter a positive number.");
17+
}
18+
System.out.println("Program terminated.");
19+
20+
scanner.close(); // Close the scanner to free resources
21+
}
22+
}

0 commit comments

Comments
 (0)