Program to insert an element in array solution using c language--programming_info problem 9

Program to insert an element in array.


This above example is just a reference picture. don't
think it as an input of an array.
you can give your own input.

#include<stdio.h>
#include<conio.h>

void main()
{
    int array[100], position, i, size, value;
    clrscr();

    printf("Enter size of array : ");
    scanf("%d", &size);

    for (i = 0; i < size; i++)
    {
        printf("Enter %d element : ", i);
        scanf("%d", &array[i]);
    }

    printf("Enter the location where you wish to insert an element : ");
    scanf("%d", &position);

    printf("Enter the value to insert : ");
    scanf("%d", &value);

    for (i = size - 1; i >= position - 1; i--)
    {
        array[i + 1] = array[i];
    }

    array[position - 1] = value;

    printf("\nResultant array is : ");

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

    getch();
}
Share:

No comments:

Post a Comment

Translate

Recommended platforms

  1. codechef
  2. hackerrank
  3. codeforces
  4. leetcode
  5. hackerearth

Popular Posts

programming_info. Powered by Blogger.

Blog Archive

Recent Posts

other platforms

  • geeks for geeks
  • w3schools
  • codepen
  • skillshare
  • udemy

Pages

reader support Support