To write a program in C to insert an element at end of an Array, First of all, we will find the last index of elements. After finding the last index we will add one index and insert an element.
Programming logic and working of insertion program in c at end
- Our Program will take size of the array and an element that need to insert in the array at end.
- Now to insert element at the end of array we will just insert at the last index directly.
- Becuase we already know the index number till element is filled in the array.
C program to insert an element at end of an Array
#include <stdio.h> void main() { int position, i, n, value,ch, arr[100]; printf("C Program to insert element at end of Array\n"); printf("First enter number of elements you want in Array\n"); scanf("%d", &n); arr[n]; for(i = 0; i < n; i++) { printf("Please give value for index %d : ",i); scanf("%d",&arr[i]); } printf("Let's Insert Element at end \n "); printf("Please give a number to insert at end \n"); scanf("%d", &value); arr[n] = value; printf("Element %d is inserted at %d index \n",value,n); printf("New Array is \n "); for(i = 0; i < n+1; i++) { printf("%d \t",arr[i]); } }
Output
[wpusb]