Array

Java code to print array elements using while

Java code to print array elements using while

In this article, we will discuss the concept of Java code to print array elements using while

In this post, we are going to learn how to write a program to  print in an array using while loop in Java language

Code to Display array elements

Program to print elements of an array using while loop – #1

In this code, we are going to learn how to display array elements in an array using while loop in Java language

Program 1

//Display integer of an array using for loop

class Disp_Array_int_whileloop{
public static void main (String args[]){
int arr[]=new int[]{13,61,19,11,67,18}; 
//diclaration and initialization of an array

System.out.println("Display elements of given array");

//for loop use to displays the elements by incrementing value of i
int i=0;
while(i<arr.length){
System.out.print(arr[i]+" ");
i++;

}
}
}

When the above code is executed, it produces the following result

Display elements of given array

13
61
19
11
67
18

 

Program to print elements of an array using while loop – #2

In this code, we are going to learn how to display array elements in an array using while loop in Java language

Program 2

//Display integer of an array using while loop

class Disp_Array_int_whileloop1{
public static void main (String args[]){
int arr[]=new int[6]; 
arr[0]=120;
arr[1]=130;
arr[2]=140;
arr[3]=150;
arr[4]=160;
arr[5]=170;
//diclaration and initialization of an array

System.out.println("Display elements of given array");

//while loop use to displays the elements by incrementing value of i
int i=0; 
while(i<arr.length){
System.out.println(arr[i]+" ");
i++;
}
}
}

When the above code is executed, it produces the following result

Display elements of given array

120
130
140
150
160
170


 

Program to print Strings of an array using while loop – #1

In this code, we are going to learn how to display String array elements in an array using while loop in Java language

Program 1

//Java program to read and print string of an array using while

class Disp_Array_of_String_While1{
public static void main (String args[]){
String str[]=new String[5]; 
//diclaration and creation of an array

str[0]="Canada";
str[1]="UK";
str[2]="USA";
str[3]="India";
str[4]="UAE";
//input the String input

System.out.println("\nYour strings are:");
int j=0;
while(j<str.length){
System.out.println(str[j]); 
 j++;
}//display all entered string
}
}

When the above code is executed, it produces the following result

Your strings are

Canada
UK
USA
India
UAE

 

Program to print Strings of an array using while loop – #2

In this code, we are going to learn how to display String array elements in an array using while loop in Java language

Program 2

//Java program to read and print string of an array using while

class Disp_Array_of_String_While2{
public static void main (String args[]){
String str[]=new String[]{"China","India","Brazil","Japan","Egypt"}; 
//diclaration and creation of an array


System.out.println("\nYour strings are:");
int j=0;
while(j<str.length){
System.out.println(str[j]); 
 j++;
}//display all entered string
}
}

 

When the above code is executed, it produces the following result

Your Strings are:

China
India
Brazil
Japan
Egypt


Program to print character elements of an array using while loop – #1

In this code, we are going to learn how to display character array elements in an array using while loop in Java language

Program 1

//Print character of an array using while loop
class Disp_Array_Char_Whileloop{
public static void main (String args[]){
char Char[]=new char[6]; 
//Declaration and creation of character array
Char[0]='X';
Char[1]='Y';
Char[2]='Z';
Char[3]='A';
Char[4]='B';
Char[5]='C';
//initialization of characters index wise
System.out.println("\n characters are:");
int j=0; 
while(j<Char.length){
System.out.println(Char[j]);
 j++; 
}//display all characters
}
}

When the above code is executed, it produces the following result

characters are:

X
Y
Z
A
B
C

 

Program to print character elements of an array using while loop – #2

In this code, we are going to learn how to display character array elements in an array using while loop in Java language

Program 2

//Print character of an array using while loop
class Disp_Array_Char_Whileloop1{
public static void main (String args[]){
char Char[]=new char[]{'K','l','M','n','O','p'}; 
//Diclaration and creation of character array

//initialization of characters index wise
System.out.println("\n characters are:");
int j=0; 
while(j<Char.length){
System.out.println(Char[j]);
 j++; 
}//display all characters
}
}

When the above code is executed, it produces the following result

characters are:

K
l
M
n
O
p

 

Suggested for you

For loop in Java language

While loop in Java language

Operator in Java language

Variable in Java language

Data type in Java language

class and main method in Java

One dimensional array in Java

 

Similar post

Java program to read and print array elements using for loop

Java program to read and print array elements using while loop

Java program to read and print array elements using Do-while loop

C code to take and print array elements using for loop

C code to take and print array elements using while loop

C code to take and print array elements using do-while loop

C++ program to read and print array elements using for loop

C++ program to read and print array elements using while loop

C++ program to read and print array elements using do-while loop

 

 

 

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.