C program to insert an element in a specified location in an array | My CS Tutorial - My CS Tutorial

Breaking

Programming languages full tutorial and programs, exam papers, visual basic( Vb.net ), information of new technologies and more....

Tuesday, February 4, 2020

C program to insert an element in a specified location in an array | My CS Tutorial

C program to insert an element in a given location in an array.

Here we will write or discuss that how to solve a problem to insert an element or number in an array in a specified location that will entered by user.

In this program we will write a program to insert a number in a given location in an array.

This program will accept the range or size of an array that entered by user through the keyboard.
After it we will enter the elements of this array.

Now we will enter an element to insert in given array.

Then we will enter a number to choose the location of insert element.

After these steps compiler check and execute the conditions and print a new array elements list with inserted element.


PROGRAM CODE


#include<stdio.h>
#include<conio.h>
void main()
{
    int size,x;
    int position,insertitem;
    int array[50],temp[50+1];
    clrscr();
    
    printf("\n ENTER THE SIZE OF THIS ARRAY:\n ");
    scanf("%d",&size);
   
  
    printf("\n ENTER THE NUMBERS OR ELEMENT IN THIS ARRAY:\n ");
    
    for(x=0;x<size;x++)
    {
        scanf("%d",&array[x]);
    }

    printf("\n ENTER AN ELEMENT TO BE INSERTED IN THIS ARRAY:\n ");
    scanf("%d",&insertitem);
    
    printf("\n ENTER POSITION FOR AN ELEMENT TO BE INSERTED IN THIS ARRAY:\n ");
    
    scanf("%d",&position);
    
    position=position-1;
    
        for(x=0;x<=size;x++)
        {
        if(x<position)
        {
            temp[x]=array[x];
        }
        
        if(x>position)
        {
         temp[x]=array[x-1];
        }
        
        if(x==position)
        {
            temp[x]=insertitem;
        }

        }

        printf("\n ARRAY AFTER INSERTING AN ELEMENT:\n ");
        
        for(x=0;x<=size;x++)
        {
        printf("%d\n ",temp[x]);
         }
         
         getch();
}


OUTPUT:





Please share this post and blog link with your friends.For more programs use this blog.
If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.If you have any method of this program or want to give any suggestion send email on hc78326@gmail.com

Created by-- HARSH CHAUHAN


No comments:

Post a Comment