Insert array element using One Dimensional Array in C


This program demonstrates how to insert a value at a specific position in an array using C programming. It starts by taking an input for the limit of the array, and then the values for the array. It then prints the original array. Next, it takes input for the position at which the new value is to be inserted and the value itself. Then it uses a for loop to copy the original array elements into a new array, but with the new value inserted at the specified position. Finally, it prints the new array with the inserted value

Source Code

#include<stdio.h>
int main()
{
  int i,t,a[10],n,m,s,j=0,b[10];
  printf("\nEnter the limit:");
  scanf("%d",&n);
  printf("\nEnter the values:");
  for(i=0;i<n;i++)
  {
    scanf("%d",&a[i]);
  }
  printf("\nGiven values are:");
  for(i=0;i<n;i++)
  {
    printf("\na[%d]=%d",i,a[i]);
  }
  printf("\nEnter the position to be changed:");
  scanf("%d",&t);
  printf("\nEnter the value to be changed:");
  scanf("%d",&s);
  for(i=0;i<t;i++)
  {
    b[j]=a[i];
    j++;
  }
  b[j]=s;
  j++;
  for(i=t;i<n;i++)
  {
    b[j]=a[i];
    j++;
  }
  printf("\nInserted value is:");
  for(i=0;i<=n;i++)
  {
    printf("\na[%d]=%d",i,b[i]);
  }
  return 0;
}
To download raw file Click Here

Output

Enter the limit:5
Enter the values:1
2
3
4
5
Given values are:
a[0]=1
a[1]=2
a[2]=3
a[3]=4
a[4]=5
Enter the position to be changed:3
Enter the value to be changed:5
Inserted value is:
a[0]=1
a[1]=2
a[2]=3
a[3]=5
a[4]=4
a[5]=5

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C