0% found this document useful (0 votes)
10 views2 pages

Program 2

The document contains a C program that performs operations on an array, including insertion, deletion, and traversal of elements. It prompts the user to input the size of the array and its elements, then allows for the insertion of a new element at a specified position and the deletion of a specified element. Finally, it displays the array before and after these operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

Program 2

The document contains a C program that performs operations on an array, including insertion, deletion, and traversal of elements. It prompts the user to input the size of the array and its elements, then allows for the insertion of a new element at a specified position and the deletion of a specified element. Finally, it displays the array before and after these operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Program 2

Write a program to perform various operation on array –


insertion, deletion and traverse of element

#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[100];
int element,i,loc,size,n=0,j=0;
printf("Enter the size of an array\n");
scanf("%d",&size);
printf("Enter %d array elements\n",size);
for(i=0; i<size; i++)
{
scanf("%d",&a[i]);
}
printf("List before Insertion: ");
for(i=0; i<size; i++)
{
printf("%d ",a[i]);
}
printf("\nEnter an element to insert\n");
scanf("%d",&element);
printf("Enter a position to insert an element %d\n",element);
scanf("%d",&loc);
loc--;
for(i=size-1;i>=loc;i--)
{
a[i+1]=a[i];
}
a[loc]=element;
printf("\nList after Insertion: ");
for(i=0;i<size+1;i++)
{
printf("%d ",a[i]);
}
printf("\nEnter an element to delete\n");
scanf("%d",&n);
i=0;
for(i=0;i<size;i++)
{
if(a[i]==n)
{
for(j=i;j<(size-1);j++)
{
a[j]=a[j+1];
}
break;
}
}
printf("List after deletion\n");
for(i=0;i<(size-1);i++)
{
printf("%d ",a[i]);
}
return 0;
}

You might also like