﻿ C# Sharp Exercises: Insert New value in the array (unsorted list ) - w3resource

# C# Sharp Exercises: Insert New value in the array (unsorted list )

## C# Sharp Array: Exercise-14 with Solution

Write a program in C# Sharp to insert New value in the array (unsorted list ).

Sample Solution:-

C# Sharp Code:

using System;
public class Exercise14
{
public static void Main()
{
int[] arr1 = new int[10];
int i,n,p,x;

Console.Write("\n\nInsert New value in the unsorted array : \n");
Console.Write("-----------------------------------------\n");
Console.Write("Input the size of array : ");
/* Stored values into the array*/
Console.Write("Input {0} elements in the array in ascending order:\n",n);
for(i=0;i<n;i++)
{
Console.Write("element - {0} : ",i);
}

Console.Write("Input the value to be inserted : ");
Console.Write("Input the Position, where the value to be inserted :");

Console.Write("The current list of the array :\n");
for(i=0;i<n;i++)
Console.Write("{0} ",arr1[i]);
/* Move all data at right side of the array */
for(i=n;i>=p;i--)
arr1[i]= arr1[i-1];
/* insert value at given position */
arr1[p-1]=x;

Console.Write("\n\nAfter Insert the element the new list is :\n");
for(i=0;i<=n;i++)
Console.Write("{0} ",arr1[i]);
Console.Write("\n\n");
}
}

Sample Output:

Insert New value in the unsorted array :
-----------------------------------------
Input the size of array : 2
Input 2 elements in the array in ascending order:
element - 0 : 5
element - 1 : 15
Input the value to be inserted : 10
Input the Position, where the value to be inserted :2
The current list of the array :
5 15
After Insert the element the new list is :
5 10 15

Flowchart:

C# Sharp Code Editor: