💻 Program Code

import java.util.Scanner;

public class InsertAtPosition {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int[] arr = new int[100];
        int n, i, value, pos;

        System.out.print("Enter number of elements: ");
        n = sc.nextInt();

        System.out.println("Enter elements:");
        for(i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }

        System.out.print("Enter position (0 to " + n + "): ");
        pos = sc.nextInt();

        System.out.print("Enter value to insert: ");
        value = sc.nextInt();

        // shift elements right
        for(i = n; i > pos; i--) {
            arr[i] = arr[i - 1];
        }

        arr[pos] = value;
        n++;

        System.out.println("Array after insertion:");
        for(i = 0; i < n; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}
                        
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.