Program to Insert an Element in an Array [C, C++, Python & Java]

Posted in

Program to Insert an Element in an Array [C, C++, Python & Java]
vinaykhatri

Vinay Khatri
Last updated on March 29, 2024

    This tutorial will help you learn to write a program in C, C++, Python, and Java to insert an element in an array.

    To do so, you must have knowledge of an array, for loop, if...else statement, and the programming language syntax.

    So, let us start with the definition of an array.

    What is an Array?

    An array is a data structure that stores multiple items of a similar data type at contiguous memory locations. We can easily access an element in an array using its index number.

    For example, consider an array A.

    A = [34, 45, 56, 23, 98]

    If you want to retrieve item 56, you can simply do it using its index number A[2].

    Let us see how to insert an element in an array with the help of the following programs. We have written programs that accept an element and insert it at the first position in an array.

    C Program in Insert an Element in an Array

    #include<stdio.h>
    int main()
    {
        int arr[10],n,pos=0,in, i, j;
        printf("Enter the number of element in an array: ");
        scanf("%d", &n);
        printf("Enter the array elements in ascending order: \n");
        for(int i=0;i<n;i++)
        {
            scanf("%d", &arr[i]);
        }
        printf("Enter the Element you want to insert: ");
        scanf("%d", &in);
        for(i=0; i<n;i++)
         if(arr[i]<=in && in<arr[i+1])
         {
             pos = i+1;
             break;
         }
         for(j=n+1; j>pos;j--)
         {
             arr[j]=arr[j-1];
         }
         arr[pos]=in;
         printf("The element has been inserted: ");
         for(i=0;i<n+1;i++)
         {
             printf(" %d ", arr[i]);
         }
         return 0;
    }

    Output:

    Enter the number of element in an array: 4
    Enter the array elements in ascending order: 
    34 56 78 99
    Enter the Element you want to insert: 32
    The element has been inserted:  32  34  56  78  99 

    C++ Program in Insert an Element in an Array

    #include<iostream>
    using namespace std;
    int main()
    {
        int arr[10],n,pos=0,in, i, j;
        cout<<"Enter the number of elements in an array: ";
        cin>>n;
        cout<<"Enter the array elements in ascending order:"<<endl;
        for(int i=0;i<n;i++)
        {
            cin>>arr[i];
        }
        cout<<"Enter the Element you want to insert: ";
        cin>>in;
        for(i=0; i<n;i++)
         if(arr[i]<=in && in<arr[i+1])
         {
             pos = i+1;
             break;
         }
         for(j=n+1; j>pos;j--)
         {
             arr[j]=arr[j-1];
         }
         arr[pos]=in;
         cout<<"The element has been inserted: ";
         for(i=0;i<n+1;i++)
         {
             cout<<arr[i]<<" " ;
         }
         return 0;
    }

    Output:

    Enter the number of elements in an array: 5
    Enter the array elements in ascending order:
    2 3 4 5 7
    Enter the Element you want to insert: 10
    The element has been inserted: 10 2 3 4 5 7 

    Python Program in Insert an Element in an Array

    arr=[]
    pos=0
    n=int(input("How many elements you want to enter in the array: "))
    print("Enter the array elements in Ascending order:")
    for i in range(n):
        arr.append(int(input()))
    inp= int(input("Enter the Element you want to insert: "))
    for i in range(len(arr)):
        if ((arr[i]<=inp) and (inp<arr[i+1])):
            pos = i+1
            break
    if pos <len(arr):
       arr.insert(pos,inp)
    else:
        arr.append(inp)
    for i in range(len(arr)):
        print(arr[i],end=' ')

    Output:

    How many elements you want to enter in the array: 5
    Enter the array elements in Ascending order:
    
    1
    23
    34
    45
    57
    
    Enter the Element you want to insert: 36
    1 23 34 36 45 57

    Java Program in Insert an Element in an Array

    import java.util.Scanner;
    public class Main
    {
        public static void main(String[] args)
        {
            int arr[] = new int[50];
            int n,pos=0,in, i, j;
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter the number of element in an array: ");
            n = sc.nextInt();
            System.out.println("Enter the array elements in ascending order:");
            for( i=0;i<n;i++)
            {
                arr[i] = sc.nextInt();
            }
            System.out.println("Enter the Element you want to insert: ");
            in = sc.nextInt();
            for(i=0; i<n;i++)
             if(arr[i]<=in && in<arr[i+1])
             {
                 pos = i+1;
                 break;
             }
             for(j=n+1; j>pos;j--)
             {
                 arr[j]=arr[j-1];
             }
             arr[pos]=in;
             System.out.println("The element has been inserted: ");
             for(i=0;i<n+1;i++)
             {
                 System.out.print(arr[i]+ " ");
             }
             System.out.println(" ");
        }
    }

    Output:

    Enter the number of element in an array: 
    4
    Enter the array elements in ascending order:
    22 34 87 98 
    Enter the Element you want to insert: 
    44
    The element has been inserted: 
    22 34 44 87 98  

    Conclusion

    This was the most basic program, as it inserts the element at index 0, i.e., the first position in an array. To make it more complex, you can tweak it to ask the user to enter the position where they need to insert an element in an array. If the user enters 3, the program should insert the user-entered element at index 3.

    If you encounter any issues while implementing the program, let us know in the comments. We would be glad to help you.

    People are also reading:

    Leave a Comment on this Post

    0 Comments