Insert an element in an array using C
Hey guys once again welcome to the Decoders blog, I hope you all are doing well and today I'm present with another logic but this time with something different it is a C logic which will insert an element in a given array using C logic
Here's the code which you can go through...........
//Always remember while inserting an element use backwards loop otherwise it will not give the desired output
#include<stdio.h>
int main()
{
int array[100];
int i,num,pos,insert;
printf("Enter the values:\n");
for(i=0;i<9;i++)
{
scanf("%d",&array[i]);
}
//Displaying the stored array
printf("Array:\n");
for(i=0;i<9;i++)
{
printf("%d\t",array[i]);
}
printf("Enetr the new element:\n");
scanf("%d",&insert);
printf("Enter the positon:\n");
scanf("%d",&pos);
for(i=11;i>pos-1;i--)
{
array[i]=array[i-1];
}
array[pos-1]=insert;
//Displaying the final arry
for(i=0;i<10;i++)
{
printf("%d\t",array[i]);
}
}
Comments
Post a Comment