Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Print 1–100 in Java

Print 1 through 100 in Java with a for loop, i++, and System.out.print. Spaces separate the numbers on one line; <= includes the final value of 100.

BeginnerJava Loop ProgramsExample 1 of 25
print-1-100.java
1public class Main {
2 public static void main(String[] args) {
3 for (int i = 1; i <= 100; i++) {
4 System.out.print(i + " ");
5 }
6 }
7}

Sample Output

1 2 3 ... 100

The counter increases by one each time. A space follows every printed number, including the last one.

In this one

  • Set a loop start and end
  • Update a counter
  • Print a spaced sequence

How it works

  1. 1

    Set i to 1.

  2. 2

    Check i <= 100 before printing.

  3. 3

    Print i followed by a space.

  4. 4

    Increase i by one and repeat.

Edge cases

Exclusive end

Change <= 100 to < 100

Only 1 through 99 print.

Missing update

Remove i++

The value never changes, so the condition never becomes false.

What's going on

Print every whole number from 1 through 100 on one line. This is a simple way to practise a for loop, a statement that repeats a section of code while a condition stays true. The same counting pattern can later number questions or student records.

The loop starts i at 1. Before each pass it checks i <= 100, prints the current i, and then adds one with i++. The space joined to i separates neighbouring numbers. System.out.print keeps them on the same line instead of adding a newline after each number.

The final number is 100 because the condition includes equality. Changing it to i < 100 would stop at 99. If you forget the update, i stays at 1 and the loop keeps running. The output here lists the complete sequence rather than using dots to hide the middle values.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What is the first printed number?

Pick an answer to check it.

Question 2

What happens after printing 100?

Pick an answer to check it.

Questions people ask

Does 100 appear?

Yes. <= includes the upper limit.

Why is print used rather than println?

It keeps all the numbers on the same line.

How many passes run?

Exactly 100, one for each number from 1 to 100.