DEV Community

Deepikandas
Deepikandas

Posted on

#21 While Loop in JAVA with examples

  • The while statement is a control flow statement.
  • It continually executes a block of statements while a particular condition is true.

*syntax *

while (expression) {
statement(s)
}
A while loop becomes infinite when the condition never becomes false
🔥 Common Causes
Missing update
while (i < 5) { } → i never changes
Wrong update direction
while (i < 5) { i--; }
Always true condition
while (true) { }
Assignment instead of comparison
while (flag = true) { }
Variable not modified
while (num > 0) { }
Condition never reachable
while (i != 10) {
i += 2;
}

Example 1:Print 1 1 1 1 1

public class Sample {
    public static void main(String[] args) {
        int i = 0;
        while (i < 5) {
            System.out.print(1 + " ");
            i++;
        }
    }
}
Output:
1 1 1 1 1               
Enter fullscreen mode Exit fullscreen mode

Example 2:Printing 1 2 3 4 5

public class Sample {
    public static void main(String[] args) {
        int i = 1;
        while (i<=5) {
            System.out.print(i + " ");
            i++;
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

Example 3:Printing odd number 1 3 5 7 9

public class Sample {
    public static void main(String[] args) {
        int i = 1;
        while (i<=10) {
            if(i%2!=0) {
            System.out.print(i + " ");
            }
            i++;
        }
    }
}
Output:1 3 5 7 9     
Enter fullscreen mode Exit fullscreen mode

Example 4:Printing 3 6 9 12 15

public class Sample {
    public static void main(String[] args) {
        int i = 1;
        while (i<=15) {
            if(i%3==0) {
            System.out.print(i + "  ");
            }
            i++;
        }
    }
}
Output:
3  6  9  12  15  
Enter fullscreen mode Exit fullscreen mode

Example 5: Multiples of 3 and 5

public class Sample {
    public static void main(String[] args) {
        int i = 1;
        System.out.print("Multiples of both 3 and 5 are ");
        while (i<=50) {
            if(i%3==0 && i%5==0) {
            System.out.print(i + " , ");
            }
            i++;
        }
    }
}
Output:
Multiples of both 3 and 5 are 15 30 45  

Enter fullscreen mode Exit fullscreen mode

Example 6: Multiples of 3 or 5

public class Sample {
    public static void main(String[] args) {
        int i = 1;
        System.out.print("Multiples of both 3 and 5 are ");
        while (i<=20) {
            if(i%3==0 || i%5==0) {
        System.out.print(i + "  ");
            }
            i++;
                }
    }
}

Output:
Multiples of both 3 and 5 are 3  5  6  9  10  12  15  18  20  
Enter fullscreen mode Exit fullscreen mode

Example 7: Finding All Divisors and number of divisor of a given Number


public class WhileExample {

    public static void main(String[] args) {
        int user=15;
        int i=1;
        byte count=0;
        while(i<=user)
        {
            if(user%i==0) {
            System.out.println(i);
            count++;
                    }
        i++;
    }
        System.out.println("Number of divisors "+count);
}
}
Output:
1
3
9
27
Number of divisors 4
Enter fullscreen mode Exit fullscreen mode

Example 8: Finding whether a number is prime number or not.
Note:Negative numbers,0 and 1 cannot be prime.
Prime number is a positive integer greater than 1 that has exactly two positive divisors: 1 and itself

public class PrimeNumber {

    public static void main(String[] args) {
    int userInput=9976;
        int i=2;
    boolean status=true;
    if(userInput<=0) {
        status=false;
    }
    while(i<userInput) {    
        if(userInput%i==0) {
            status=false;
            break;
        }
        i++;
    }
    if(status==true)
    {
        System.out.println(userInput+"  number is  prime");
    }
    else
        System.out.println(userInput +" number is not prime number");
    }

}

Output:
9976 number is not prime number
Enter fullscreen mode Exit fullscreen mode

Example 9: Reversing Positive integer

public class ReverseNumber {

    public static void main(String[] args) {
        int userInput=123456789;
        int reverse=0;

while(userInput>0) {
            int lastDigitGet=userInput%10;
            reverse=reverse*10+lastDigitGet;
            userInput=userInput/10;

        }
System.out.println("Reversed number is "+reverse);
    }

}
Output:
Reversed number is 987654321
Enter fullscreen mode Exit fullscreen mode


plaintext
Example 10: Counting number of digits in a positive integer

public class NumberOfDigits {

    public static void main(String[] args) {
        long userInput=12345678901234567L;
        int count=0;
        while(userInput>0) {
                count++;
            userInput=userInput/10;

        }
        System.out.println("Number of digits : "+count);
    }

}
Output:
Number of digits : 17
Enter fullscreen mode Exit fullscreen mode


plaintext
Example 11:Sum of digits in a positive integer


public class SumOfDigits {

    public static void main(String[] args) {
    int givenNumber=123456789;
    int sum=0;
    while(givenNumber>0) {
        int getLastDigit=givenNumber%10;
        sum=sum+getLastDigit;
        givenNumber=givenNumber/10;
    }
    System.out.println("Sum of digits: "+sum);
    }

}
Output:
Sum of digits: 45
Enter fullscreen mode Exit fullscreen mode


plaintext
12.To find whether a number is palindrome or not

public class PalindromeNumber {

    public static void main(String[] args) {
    int givenNumber=906609;
    int temp=givenNumber;
    int reverse=0;
    while( temp>0) {
        int getLastDigit=temp%10;
        reverse=reverse*10+getLastDigit;
        temp=temp/10;
        }
    if(reverse==givenNumber)
    System.out.println(givenNumber+" is a palindrome number");
    else
        System.out.println(givenNumber+" is NOT a palindrome number");
    }
OUTPUT:
906609 is a palindrome number
Enter fullscreen mode Exit fullscreen mode

Top comments (0)