Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
C++ code to print array using for loop

Java code to print array elements using while

Posted on October 9, 2021

Table of Contents

  • Java code to print array elements using while
    • Code to Display array elements
      • Program to print elements of an array using while loop – #1
      • Program to print elements of an array using while loop – #2
      • Program to print Strings of an array using while loop – #1
      • Program to print Strings of an array using while loop – #2
      • Program to print character elements of an array using while loop – #1
      • Program to print character elements of an array using while loop – #2

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

 

 

 

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes