w3resource
C# Sharp Programming Exercises

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 : ");
		 n = Convert.ToInt32(Console.ReadLine());  
    /* 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);
		 arr1[i] = Convert.ToInt32(Console.ReadLine());  
	    }
	
	
   Console.Write("Input the value to be inserted : ");
		 x = Convert.ToInt32(Console.ReadLine());  
   Console.Write("Input the Position, where the value to be inserted :");
		 p = Convert.ToInt32(Console.ReadLine());  
   
   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:

Flowchart: Insert New value in the array (unsorted list )

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to insert New value in the array (sorted list ).
Next: Write a program in C# Sharp to delete an element at desired position from an array.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming