rectangle pattern

Java code to display patterns using do while loop

Java code to display patterns using do while loop

In this tutorial, we will discuss the concept of Java code to display patterns using do while loop.

In Java, we can use for loop, while loop, do-while loop to display various number, star, alphabet and binary number patterns

In this topic, we demonstrate how to display print some number and star patterns using the nested do-while loop in Java language.

Program 1

Rectangle number pattern

class DoWhilepattern1{
public static void main(String args[]){
int i=0;
do{
int j=1;
while(j<=6)
{
System.out.print(j);
j++;
}
System.out.println();
i++;

}
while(i<=6);
}
}

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

123456
123456
123456
123456
123456
123456

 

Triangle number pattern

Program 2

class NestedDoWhile1{
public static void main(String args[]){
int row=1,column=1;
int x;
do{
x=6;
do{
System.out.print("");
x--;
}while(x>=row);
column=1;
do{
System.out.print(column+" ");
column++;

}while(column<=row);
System.out.println(" ");
row++;
}while (row<=6);
}

}

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

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6

 

Rectangle star pattern

Program 3

class DoWhilepattern1{
public static void main(String args[]){
int i=0;
do{
int j=1;
while(j<=8)
{
System.out.print("*");
j++;
}
System.out.println();
i++;

}
while(i<=6);
}
}

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

********
********
********
********
********
********
********

 

Triangle star pattern

Program4

class NestedDoWhile{
public static void main(String args[]){
int row=1,column=1;
int x;
do{
x=4;
do{
System.out.print("");
x--;
}while(x>=row);
column=1;
do{
System.out.print("*"+" ");
column++;

}while(column<=row);
System.out.println(" ");
row++;
}while (row<=5);
}

}

 

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

*
**
***
****
*****

 

Suggested for you

Do-while loop in Java

Nested do-while loop in Java

Operator in C language

triangle number pattern Using nested while in Cpp
C program to display patterns using do while loop
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago