Table of Contents
Program to display array value using for loop in Java
In this article, we will discuss the concept of Program to display array value using for loop in Java
In this post, we are going to learn how to write a program to print in an array using for loop in Java language
Code to print array elements
Code to print elements of an array using for loop – #1
In this code, we are going to learn how to display array elements in an array using for loop in Java language
Program 1
//Display integer of an array using for loop class Disp_Array_element_int{ public static void main (String args[]){ int arr[]=new int[]{3,6,9,12,15,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 for(int i=0; i<arr.length; i++){ System.out.print(arr[i]+" "); } } }
When the above code is executed, it produces the following result
Display elements of given array 3,6,9,12,15,18
Code to print elements of an array using for loop – #2
In this code, we are going to learn how to display array elements in an array using for loop in Java language
Program 2
//Display integer of an array using for loop class Disp_Array_element_int1{ public static void main (String args[]){ int arr[]=new int[6]; arr[0]=12; arr[1]=13; arr[2]=14; arr[3]=15; arr[4]=16; arr[5]=17; //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 for(int i=0; i<arr.length; i++){ System.out.print(arr[i]+" "); } } }
When the above code is executed, it produces the following result
Display elements of given array 12,13,14,15,16,17
Code to print Strings of an array using for loop – #1
In this code, we are going to learn how to display string array elements in an array using for loop in Java language
Program 1
//Java program to read and print string of an array using while class Disp_Array_of_String_forloop{ public static void main (String args[]){ String str[]=new String[5]; //diclaration and creation of an array str[0]="Malala"; str[1]="Poma"; str[2]="Neha"; str[3]="Sona"; str[4]="Monika"; //input the String input System.out.println("\nYour strings are:"); for(int j=0; j<str.length; j++){ System.out.println(str[j]); }//display all entered string } }
When the above code is executed, it produces the following result
Your Strings are Malala Poma Neha Sona Monika
Code to print Strings of an array using for loop – #2
In this code, we are going to learn how to display string array elements in an array using for loop in Java language
Program 2
//Java program to read and print string of an array using while class Disp_Array_of_String_forloop1{ public static void main (String args[]){ String str[]=new String[]{"Joe","Kumu","Nelo","Puni","Sri"}; //diclaration and creation of an array System.out.println("\nYour strings are:"); for(int j=0; j<str.length; j++){ System.out.println(str[j]); }//display all entered string } }
When the above code is executed, it produces the following result
Your Strings are Joe Kumu Nelo Puni Sri
Code to print character of an array using for loop – #1
In this code, we are going to learn how to display character array elements in an array using for loop in Java language
Program 1
//Print character of an array using for looop class Disp_Array_Char_Forloop{ public static void main (String args[]){ char Char[]=new char[6]; //diclaration and creation of character array Char[0]='A'; Char[1]='C'; Char[2]='E'; Char[3]='G'; Char[4]='I'; Char[5]='K'; System.out.println("\n characters are:"); for(int j=0; j<Char.length; j++){ System.out.println(Char[j]); }//display all entered characters } }
When the above code is executed, it produces the following result
characters are: A C E G I K
Code to print character of an array using for loop – #2
In this code, we are going to learn how to display character array elements in an array using for loop in Java language
Program 2
//Print character of an array using for looop class Disp_Array_Char_Forloop1{ public static void main (String args[]){ char Char[]=new char[]{'A','D','G','J','M'}; //diclaration and creation of character array System.out.println("\n characters are:"); for(int j=0; j<Char.length; j++){ System.out.println(Char[j]); }//display all entered characters } }
When the above code is executed, it produces the following result
Characters are A D G J M
Suggested for you
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