Skip to content

Commit 3836276

Browse files
committed
feat: Add OneToNPrint class to print numbers from 1 to 10 using while loop
Add a program that: - Initializes a counter variable - Uses a `while` loop to print numbers from 1 through 10 - Demonstrates basic loop control and counter incrementation in Java.
1 parent 1929652 commit 3836276

File tree

1 file changed

+4
-39
lines changed

1 file changed

+4
-39
lines changed
Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,12 @@
11
//1. Program to Print Numbers from 1 to 10 Using while Loop
22

3-
public class OneToNPrint
4-
{
5-
public static void main(String[] args)
6-
{
7-
int i = 1; // Initialize the counter
8-
while (i <= 10)
9-
{
10-
System.out.println(i); // Print the number
11-
i++; // Increment the counter
12-
}
13-
}
14-
}
15-
16-
/*
17-
Input from users.
18-
19-
import java.util.Scanner;
20-
213
public class OneToNPrint {
224
public static void main(String[] args) {
23-
Scanner scanner = new Scanner(System.in);
5+
int i = 1;
246

25-
System.out.print("Enter a number to print from 1 to that number: ");
26-
27-
int number = scanner.nextInt(); // Take input from the user
28-
29-
// Check if the user entered a valid positive number
30-
31-
if (number > 0) {
32-
for (int i = 1; i <= number; i++)
33-
{
34-
System.out.println(i); // Print numbers from 1 to the entered number
35-
}
36-
}
37-
else
38-
{
39-
System.out.println("Please enter a positive number.");
7+
while (i <= 10) {
8+
System.out.println(i);
9+
i++; // Increment the counter
4010
}
41-
42-
System.out.println("Program terminated.");
43-
44-
scanner.close(); // Close the scanner to free resources
4511
}
4612
}
47-
*/

0 commit comments

Comments
 (0)