while loop programs in Java language

while loop programs in java

introduction of this article

In the tutorial, We will discuss while loop program examples in Java. in this post, we are going to learn while loop in Java language and how to use it
In this topic, We will discuss some concept of while loops such as the introduction of the while loop, Syntex of while loop flow diagram of while loop and example programs for while loop

 

what is while loop
It executes a block of statements repeatedly until the condition (Boolean expression) is satisfied. it is similar for loop

The usage of while loop in Java have been explained using Given some programs

Syntax of while loop

while(test_Expression){
//codes inside body of while loop
}


Flow diagram of while loop

Example programs in while loop

program1

How to print numeric number 1 to 10  using while in java?
class while_Loop1{
public static void main(String args[]){
int x=1;   //variable declaration and initialization
while(x<=10){
System.out.print(x);//display value of x
x++;

}

}
}

 


When the above code is compiled and executed, it produces the following result:

1 2 3 4 5 6 7 8 9 10

program2

How to print numeric number 1 to 10 vertically  using while in java?

When the above code is compiled and executed, it produces the following result:
class while_Loop1{
public static void main(String args[]){
int x=1;   //variable declaration and initialization
while(x<=10){
System.out.println(x);//display value of x
x++;

}

}
}

When the above code is compiled and executed, it produces the following result:

1
2
3
4
5
6
8
9
10


Program 4

Sum of the numbers using while loop

class sumofnumwhile{
public static void main(String args[]){
int sum=0,i=100;
while(i != 0){
sum+=i;  //sum=sum+int
i--;

}
System.out.println("Sum of 1 to 100 is:"+sum);

}

}

When the above code is compiled and executed, it produces the following result:

 

Sum of 1 to 100 is:5050

nested while loop

program 1

1111
2222
3333
4444

How to print above pattern using while  in java?

class starregtangle {
public static void main(String arg[]){

int x=1;

while (x<5){   // outer while loop

int y=4;
while(y>=1){      // inner while loop
System.out.print(x);
y--;                       // Decrement value by 1

}

System.out.println("");

x++;
}
}
}

 

When the above code is compiled and executed, it produces the following result:

output

while loop pattern 1

                1
              21
            321
          4321
        54321
      654321

How to print above pattern using while in java?

class whilepat1{
public static void main(String args[]){
int i = 1;
int j = 1;
int k = 1;
int max = 7;

while (i < max) {
k = 1;
while (k < max - i) {
System.out.print(' ');
++k;
}
while (j > 0) {
System.out.print(max - (max - j));
--j;
}
++i;
j+=i;
System.out.println("");
}
System.out.println("");
}
}

 

When the above code is compiled and executed, it produces the following result:

output

while loop pattern 2

123456
  12345
    1234
      123
        12
          1
How to print above pattern using while loop in Java

public class whilepat2 {

public static void main(String[] args){
int i = 1;
int j = 1;
int k = 1;
int max = 7;
int max2 = 8;
int tmp = 0;

while (i < max) {
k = 1;
while (k < max - (max - i)) {
System.out.print(' ');
++k;
}
tmp = max2 - i;
j = 1;
while (j < tmp) {
System.out.print(max - (max - j));
++j;
}
++i;
System.out.println("");
}
}
}

 

When the above code is compiled and executed, it produces the following result:

 

Similar post

 

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.