Header Ads

Write a program to insert an element at the end of an array.

What is array ?

Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. To create an array, define the data type (like int ) and specify the name of the array followed by square brackets []


Code :


#include <stdio.h>
void main()
{
    int i, n, value, arr[100];
    printf("enter number of elements in a Array\n");
    scanf("%d", &n);
    arr[n];
   for(i = 0; i < n; i++)
    {
        printf("give value for index %d : ",i);
        scanf("%d",&arr[i]);
    }

    printf("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 :

enter number of elements in a Array
5
give value for index 0 : 1
give value for index 1 : 2
give value for index 2 : 3
give value for index 3 : 4
give value for index 4 : 5
number to insert at end 
6
Element 6 is inserted at 5 index 
New Array is
 1      2       3       4       5       6





Related Links :

Post a Comment

0 Comments