C Program to Insert Element in Array
Write a C program to Insert element in Array at any position:
Required Knowledge:
C basics, ArraysProgram:
1 #include<stdio.h>
2 void insert(int,int);
3 int static size=5;
4 int arr[5];
5 void main()
6
7 {
8 int i,position,e,d;
9 printf("Enter the elements of array:\n");
10 for(i=0; i<size; i++)
11 scanf("%d",&arr[i]);
12
13 printf("\nThe elements are:");
14 for(i=0; i<size; i++)
15 printf("%d ",arr[i]);
16
17
18 printf("\nEnter the position to insert an element:");
19 scanf("%d",&position); //position according to index no
20
21 printf("\nEnter the element to insert:");
22 scanf("%d",&e);
23
24 insert(position,e);
25
26 }
27
28
29 void insert(position,e)
30 {
31
32 int i;
33 for(i=size; i>=position-1; i--)
34 {
35 arr[i+1]=arr[i];
36
37 }
38 arr[position]=e;
39 size++;
40 printf("\nThe elements are:");
41 for(i=0; i<size; i++)
42 printf("%d ",arr[i]);
43 }
Output:
Enjoy it...........
Comments
Post a Comment
Dears! Yours Comment approved in 24 hours also your answer in 24 hours.......