Basic Programming C# Examples

C# Program to Delete Element at Desired position From Array

The Program in C# Program to Delete Element at Desired position From Array is given below:

using System;

namespace DeleteArrayElement
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello Codeauri Family, enter the number of elements in the array: ");
            int n = int.Parse(Console.ReadLine());
            int[] arr = new int[n];

            Console.WriteLine("Enter the elements of the array: ");
            for (int i = 0; i < n; i++)
            {
                arr[i] = int.Parse(Console.ReadLine());
            }

            Console.WriteLine("Enter the position of the element to be deleted: ");
            int position = int.Parse(Console.ReadLine());

            if (position <= 0 || position > n)
            {
                Console.WriteLine("Invalid position!");
            }
            else
            {
                int[] newArray = new int[n - 1];
                int j = 0;
                for (int i = 0; i < n; i++)
                {
                    if (i == position - 1)
                    {
                        continue;
                    }
                    newArray[j] = arr[i];
                    j++;
                }

                Console.WriteLine("Okay, the updated array after deleting the element is: ");
                foreach (int item in newArray)
                {
                    Console.Write(item + " ");
                }
                Console.WriteLine();
            }
        }
    }
}

Output:

Hello Codeauri Family, enter the number of elements in the array:
4
Enter the elements of the array:
1413
789
456
42
Enter the position of the element to be deleted:
3
Okay, the updated array after deleting the element is:
1413 789 42

Pro-Tips💡

Here are the steps by steps execution of above program:

  1. The program starts by printing a message to the console to ask the user to enter the number of elements in the array.
  2. The user inputs the number of elements in the array and the program stores this value in the ‘n’ variable.
  3. The program then creates an array ‘arr’ of size ‘n’ and asks the user to enter the elements of the array.
  4. The user inputs the elements of the array, and the program stores these values in the ‘arr’ array.
  5. The program then asks the user to enter the position of the element to be deleted.
  6. The user inputs the position of the element to be deleted and the program stores this value in the ‘position’ variable.
  7. The program then checks if the position entered by the user is valid or not. If the position is less than or equal to 0 or greater than ‘n’, the program prints an “Invalid position!” message to the console.
  8. If the position is valid, the program creates a new array ‘newArray’ of size ‘n-1’ and copies all elements of the ‘arr’ array except the element at the ‘position-1’ index to the newArray.
  9. The program then prints the updated array ‘newArray’ after deleting the element to the console.
  10. The program ends with a newline character.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

Programming Languages( Types, Pros and Cons)-Codeauri

What are the types or levels of Programming Languages? The types or levels of programming languages are divided into two types: Types Of Programming Languages: 1.Machine-level Language :1st…

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X