-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_position.c
More file actions
39 lines (39 loc) · 1.39 KB
/
array_position.c
File metadata and controls
39 lines (39 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*Q. C program for inserting an element at a specific position in an array?
Name Anish sharma
Roll no. 140*/
#include<stdio.h>
void main()
{
int i,n,pos,arr[100]; //variable declaration
/*
i is for looping variable
n is for storing the maximum number of elements of array
pos is for position where you want to insert element
*/
printf("Enter the number of elements you want to enter:");
scanf("%d",&n);
printf("Enter elements of the array\n");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]); //inserting elements in array
}
printf("Elements of array are\n");
for(i=0;i<n;i++)
{
printf("%d\n",arr[i]); //displaying elements of array before inserting at a specific position
}
printf("Enter the position where you want to add element : ");
scanf("%d",&pos);
n++; //incrementing value of n by 1 for changing size of array
for(i=n;i>=pos;i--)
{
arr[i]=arr[i-1]; //incrementing indexes of elements by 1
}
printf("Enter the element : ");
scanf("%d",&arr[pos-1]); //getting the value of element at that specific position
printf("Elements of array are\n");
for(i=0;i<n;i++)
{
printf("%d\n",arr[i]); //displaying elements of array before inserting at a specific position
}
}