Thursday, August 14, 2025

Array Program - Create, Display, Insert element at any position, Delete an element

Write a program for an array and perform the following functions: 1) Create an array. 2) Display an array. 3) Change the value at any position in an array. 4) Delete the value from an array


#include<stdio.h>
void main()
{
    int i, n, a[10], pos, no, pos1;
    printf("enter the size of array:");
    scanf("%d",&n);
    for(i=0;i<n;i++) //insert the value
    {
        printf("\n a[%d]=",i);
        scanf("%d",&a[i]);
    }
    for(i=0;i<n;i++) //display an array
    {
        printf("\n a[%d]= %d",i,a[i]);
    }
    printf("\nAt which position do you want to enter a number:");
    scanf("%d",&pos);
    printf("\n enter the value which you want to add in array:");
    scanf("%d",&no);
    for(i=n; i>=pos; i--) //change the value at any given position
    {
      a[i]=a[i-1];
    }
    a[pos-1]=no;
    for(i=0;i<=n;i++) //Display an array
    {
        printf("\n a[%d]= %d",i,a[i]);
    }
    printf("\n enter the position of the number which you want to delete:");
    scanf("%d",&pos1);
    for (i = pos1 - 1; i <= n-1; i++)  //delete the value from an array
        {
            a[i] = a[i+1];
        }
    for(i=0;i<n;i++) //Display an array
    {
        printf("\n a[%d]= %d",i,a[i]);
    }
}

No comments:

Post a Comment