Java-Write Array Values Using Loop

Write Array Values Using Loop

We shall use a for loop for providing the values of the elements of an array as input data. For example, in the following program, the array_v1 array size is 5. The values of its elements shall be fed in by using for loop. The values are then retrieved from the array and displayed.

Program

Program Source

import java.util.Scanner;

public class Javaapp {
 
    public static void main(String[] args) {
     
        Scanner sc = new Scanner(System.in);
     
        int[] array_v1 = new int[5];
     
        for(int i=0;i<array_v1.length;i++)
        {
            System.out.print("array_v1["+i+"] = ");
            array_v1[i] = sc.nextInt();
        }
     
        System.out.println();
     
        for(int i=0;i<array_v1.length;i++)
        {
            System.out.println("array_v1["+i+"] = "+array_v1[i]);
        }
     
    }
}

Leave a Comment