WikiGalaxy

Personalize

Java While Loop

Introduction:

The while loop in Java is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. The while loop can be thought of as a repeating if statement.

Syntax:

The syntax of a while loop is:


while (condition) {
    // code to be executed
}
      

Example 1: Basic While Loop

Description:

This example demonstrates a simple while loop that prints numbers from 1 to 5.


int i = 1;
while (i <= 5) {
    System.out.println(i);
    i++;
}
      

Console Output:

1 2 3 4 5

Example 2: Sum of Natural Numbers

Description:

This example calculates the sum of natural numbers up to a given number.


int sum = 0;
int n = 10;
int i = 1;
while (i <= n) {
    sum += i;
    i++;
}
System.out.println("Sum = " + sum);
      

Console Output:

Sum = 55

Example 3: Factorial Calculation

Description:

This example calculates the factorial of a number using a while loop.


int n = 5;
int factorial = 1;
int i = 1;
while (i <= n) {
    factorial *= i;
    i++;
}
System.out.println("Factorial = " + factorial);
      

Console Output:

Factorial = 120

Example 4: Fibonacci Series

Description:

This example generates the Fibonacci series up to a given number using a while loop.


int n = 10, t1 = 0, t2 = 1;
System.out.print("First " + n + " terms: ");
int i = 1;
while (i <= n) {
    System.out.print(t1 + " ");
    int sum = t1 + t2;
    t1 = t2;
    t2 = sum;
    i++;
}
      

Console Output:

First 10 terms: 0 1 1 2 3 5 8 13 21 34

Example 5: Reverse a Number

Description:

This example reverses a given number using a while loop.


int num = 1234, reversed = 0;
while (num != 0) {
    int digit = num % 10;
    reversed = reversed * 10 + digit;
    num /= 10;
}
System.out.println("Reversed Number: " + reversed);
      

Console Output:

Reversed Number: 4321

Example 6: Count Digits in a Number

Description:

This example counts the number of digits in a given number using a while loop.


int num = 123456, count = 0;
while (num != 0) {
    num /= 10;
    count++;
}
System.out.println("Number of digits: " + count);
      

Console Output:

Number of digits: 6

Example 7: Check Palindrome

Description:

This example checks if a given number is a palindrome using a while loop.


int num = 121, originalNum = num, reversed = 0;
while (num != 0) {
    int digit = num % 10;
    reversed = reversed * 10 + digit;
    num /= 10;
}
if (originalNum == reversed) {
    System.out.println(originalNum + " is a palindrome.");
} else {
    System.out.println(originalNum + " is not a palindrome.");
}
      

Console Output:

121 is a palindrome.

Example 8: Find GCD

Description:

This example finds the greatest common divisor (GCD) of two numbers using a while loop.


int num1 = 56, num2 = 98;
while (num1 != num2) {
    if (num1 > num2)
        num1 -= num2;
    else
        num2 -= num1;
}
System.out.println("GCD: " + num1);
      

Console Output:

GCD: 14

Example 9: Print Multiplication Table

Description:

This example prints the multiplication table of a given number using a while loop.


int num = 5, i = 1;
while (i <= 10) {
    System.out.println(num + " * " + i + " = " + (num * i));
    i++;
}
      

Console Output:

5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 5 * 6 = 30 5 * 7 = 35 5 * 8 = 40 5 * 9 = 45 5 * 10 = 50

Example 10: Find Power of a Number

Description:

This example calculates the power of a number using a while loop.


int base = 3, exponent = 4;
int result = 1;
int i = 1;
while (i <= exponent) {
    result *= base;
    i++;
}
System.out.println("Result: " + result);
      

Console Output:

Result: 81

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025