0% found this document useful (0 votes)
5 views1 page

Insert Element in C Array Program

This document provides a C program that demonstrates how to insert an element into an array. It outlines the algorithm and includes the complete code, which takes user input for the number of elements, the elements themselves, the item to insert, and the position for insertion. After performing the insertion, it displays the updated array.

Uploaded by

pravindongre1689
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views1 page

Insert Element in C Array Program

This document provides a C program that demonstrates how to insert an element into an array. It outlines the algorithm and includes the complete code, which takes user input for the number of elements, the elements themselves, the item to insert, and the position for insertion. After performing the insertion, it displays the updated array.

Uploaded by

pravindongre1689
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C Program to Insert an Element into an Array

Algorithm:
Step 1: Begin
Step 2: Declare array A[50], and variables N, ITEM, POS, i
Step 3: Input number of elements N
Step 4: Input N elements into the array A
Step 5: Input ITEM (element to insert)
Step 6: Input POS (position where to insert)
Step 7: For i = N - 1 down to POS - 1, do
A[i + 1] = A[i]
Step 8: Set A[POS - 1] = ITEM
Step 9: Increment N = N + 1
Step 10: Display the array after insertion
Step 11: End

C Program:
#include <stdio.h>

int main() {
int A[50], N, ITEM, POS, i;

printf("Enter number of elements: ");


scanf("%d", &N);

printf("Enter %d elements:\n", N);


for (i = 0; i < N; i++) {
scanf("%d", &A[i]);
}

printf("Enter element to insert: ");


scanf("%d", &ITEM);

printf("Enter position to insert (1 to %d): ", N + 1);


scanf("%d", &POS);

// Shift elements to right


for (i = N - 1; i >= POS - 1; i--) {
A[i + 1] = A[i];
}

// Insert the new element


A[POS - 1] = ITEM;
N++;

printf("\nArray after insertion:\n");


for (i = 0; i < N; i++) {
printf("%d ", A[i]);
}

return 0;
}

Sample Output:
Enter number of elements: 5
Enter 5 elements:
10 20 30 40 50
Enter element to insert: 25
Enter position to insert (1 to 6): 3

Array after insertion:


10 20 25 30 40 50

You might also like