Print Numbers 1 to 5
This program prints numbers from 1 to 5 using a while loop.
Problem Statement
Write a Java program that prints numbers from 1 to 5 using a while loop.
Source Code
| 1 | public class PrintWhile { |
| 2 | public static void main(String[] args) { |
| 3 | int i = 1; |
| 4 | while(i <= 5) { |
| 5 | System.out.println(i + " - Eduinq"); |
| 6 | i++; |
| 7 | } |
| 8 | } |
| 9 | } |
Program Output
1 - Eduinq 2 - Eduinq 3 - Eduinq 4 - Eduinq 5 - Eduinq
Explanation
The program initializes i=1. The while loop checks condition i<=5. If true, it prints i and increments it. The loop continues until i exceeds 5. This demonstrates how while loops execute repeatedly until a condition becomes false. Shows how while loop can print numbers sequentially.