Insertion in Arrays using C [Add an element in Array in C]

Arrays in C

This C program shows insertion in Arrays. It shows how to insert an element in an array at any point.

This program also shows how to print the updated array after the item is inserted in the array.

#include<stdio.h>

void main() {
  int a[10];
  int i, n, j, item, m;

  printf("Enter an array of Ten Elements\n");
  for (i = 1; i <= 10; i++)
    scanf("%d", & a[i]);
  printf("The array you enterd was\n");
  for (i = 1; i <= 10; i++)
    printf("\n%d", a[i]);
  printf("\nEnter an element to insert in the array\n");
  scanf("%d", & item);
  printf("\nEnter Location (1--10) to insert this element\n");
  scanf("%d", & m);
  a[m] = item;

  printf("\nThe number was\n%d ", item);
  printf("\nLocation was\n%d ", m);
  printf("\nUpdated array is\n");
  for (i = 1; i <= 10; i++)
    printf("%d\n ", a[i]);

}

The output of the Insertion in Arrays C program is:

Enter an array of Ten Elements
1
3
4
5
6
7
8
9
0
3
5
The array you enterd was

1
3
5
6
7
8
9
0
3
5
Enter an element to insert in the array
99
Enter Location (1--10) to insert this element
3
The number was
99
Location was
3
Updated array is
1
3
99
6
7
8
9
0
3
5

 

M. Saqib: Saqib is Master-level Senior Software Engineer with over 14 years of experience in designing and developing large-scale software and web applications. He has more than eight years experience of leading software development teams. Saqib provides consultancy to develop software systems and web services for Fortune 500 companies. He has hands-on experience in C/C++ Java, JavaScript, PHP and .NET Technologies. Saqib owns and write contents on mycplus.com since 2004.
Related Post