Home / Programs / Example 1: Integer Array: Write a Java program to update array elements at three different positions: start, middle, and end. Provide 3 different examples using user input for integer arrays.
Programming Example

Example 1: Integer Array: Write a Java program to update array elements at three different positions: start, middle, and end. Provide 3 different examples using user input for integer arrays.

👁 23 Views
💻 Practical Program
📘 Step by Step Learning
Study this program carefully to understand the logic, output, and explanation in a structured way.

Information & Algorithm

Example 1: Integer Array

Program Code

import java.util.Scanner;

public class UpdateIntArrayUser {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] arr = {10, 20, 30, 40, 50};

        System.out.println("Original Array:");
        for(int num : arr){
            System.out.print(num + " ");
        }

        // User input to update start, middle, and end
        System.out.print("\nEnter new value for start: ");
        arr[0] = sc.nextInt();

        System.out.print("Enter new value for middle: ");
        arr[arr.length / 2] = sc.nextInt();

        System.out.print("Enter new value for end: ");
        arr[arr.length - 1] = sc.nextInt();

        System.out.println("Updated Array:");
        for(int num : arr){
            System.out.print(num + " ");
        }
    }
}

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.