# C# Sharp Array : Exercises, Practice, Solution

## C# Sharp Array [41 exercises with solution]

[*An editor is available at the bottom of the page to write and execute the scripts.*]

**1.** Write a program in C# Sharp to store elements in an array and print it. Go to the editor

Test Data:

Input 10 elements in the array:

element - 0 : 1

element - 1 : 1

element - 2 : 2

.......

*Expected Output* :

Elements in array are: 1 1 2 3 4 5 6 7 8 9

Click me to see the solution

**2.** Write a program in C# Sharp to read n number of values in an array and display it in reverse order. Go to the editor

Test Data :

Input the number of elements to store in the array :3

Input 3 number of elements in the array :

element - 0 : 2

element - 1 : 5

element - 2 : 7

*Expected Output*:

The values store into the array are:

2 5 7

The values store into the array in reverse are :

7 5 2

Click me to see the solution

**3.** Write a program in C# Sharp to find the sum of all elements of the array. Go to the editor

Test Data :

Input the number of elements to be stored in the array :3

Input 3 elements in the array :

element - 0 : 2

element - 1 : 5

element - 2 : 8

*Expected Output* :

Sum of all elements stored in the array is : 15

Click me to see the solution

**4.** Write a program in C# Sharp to copy the elements one array into another array. Go to the editor

Test Data :

Input the number of elements to be stored in the array :3

Input 3 elements in the array :

element - 0 : 15

element - 1 : 10

element - 2 : 12

*Expected Output*:

The elements stored in the first array are :

15 10 12

The elements copied into the second array are :

15 10 12

Click me to see the solution

**5.** Write a program in C# Sharp to count a total number of duplicate elements in an array. Go to the editor

Test Data :

Input the number of elements to be stored in the array :3

Input 3 elements in the array :

element - 0 : 5

element - 1 : 1

element - 2 : 1

*Expected Output* :

Total number of duplicate elements found in the array is : 1

Click me to see the solution

**6.** Write a program in C# Sharp to print all unique elements in an array. Go to the editor

Test Data :

Input the number of elements to be stored in the array :3

Input 3 elements in the array :

element - 0 : 1

element - 1 : 5

element - 2 : 1

*Expected Output* :

The unique elements found in the array are :

5

Click me to see the solution

**7.** Write a program in C# Sharp to merge two arrays of same size sorted in ascending order. Go to the editor

Test Data :

Input the number of elements to be stored in the first array :3

Input 3 elements in the array :

element - 0 : 1

element - 1 : 2

element - 2 : 3

Input the number of elements to be stored in the second array :3

Input 3 elements in the array:

element - 0 : 1

element - 1 : 2

element - 2 : 3

*Expected Output*:

The merged array in ascending order is :

1 1 2 2 3 3

Click me to see the solution

**8.** Write a program in C# Sharp to count the frequency of each element of an array. Go to the editor

Test Data :

Input the number of elements to be stored in the array :3

Input 3 elements in the array :

element - 0 : 25

element - 1 : 12

element - 2 : 43

*Expected Output* :

Frequency of all elements of array :

25 occurs 1 times

12 occurs 1 times

43 occurs 1 times

Click me to see the solution

**9.** Write a program in C# Sharp to find maximum and minimum element in an array. Go to the editor

Test Data :

Input the number of elements to be stored in the array :3

Input 3 elements in the array :

element - 0 : 45

element - 1 : 25

element - 2 : 21

*Expected Output* :

Maximum element is : 45

Minimum element is : 21

Click me to see the solution

**10.** Write a program in C# Sharp to separate odd and even integers in separate arrays. Go to the editor

Test Data :

Input the number of elements to be stored in the array :5

Input 5 elements in the array :

element - 0 : 25

element - 1 : 47

element - 2 : 42

element - 3 : 56

element - 4 : 32

*Expected Output*:

The Even elements are:

42 56 32

The Odd elements are :

25 47

Click me to see the solution

**11.** Write a program in C# Sharp to sort elements of array in ascending order. Go to the editor

Test Data :

Input the size of array : 5

Input 5 elements in the array :

element - 0 : 2

element - 1 : 7

element - 2 : 4

element - 3 : 5

element - 4 : 9

*Expected Output* :

Elements of array in sorted ascending order:

2 4 5 7 9

Click me to see the solution

**12.** Write a program in C# Sharp to sort elements of the array in descending order. Go to the editor

Test Data :

Input the size of array : 3

Input 3 elements in the array :

element - 0 : 5

element - 1 : 9

element - 2 : 1

*Expected Output* :

Elements of the array in sorted descending order:

9 5 1

Click me to see the solution

**13.** Write a program in C# Sharp to insert New value in the array (sorted list ). Go to the editor

Test Data :

Input the size of array : 3

Input 3 elements in the array in ascending order:

element - 0 : 5

element - 1 : 7

element - 2 : 9

Input the value to be inserted : 8

*Expected Output* :

The exist array list is :

5 7 9

After Insert the list is :

5 7 8 9

Click me to see the solution

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

Test Data :

Input the size of array : 4

Input 4 elements in the array in ascending order:

element - 0 : 1

element - 1 : 8

element - 2 : 7

element - 3 : 10

Input the value to be inserted : 5

Input the Position, where the value to be inserted :2

*Expected Output* :

The current list of the array :

1 8 7 10

After Insert the element the new list is :

1 5 8 7 10

Click me to see the solution

**15.** Write a program in C# Sharp to delete an element at desired position from an array. Go to the editor

Test Data :

Input the size of array : 5

Input 5 elements in the array in ascending order:

element - 0 : 1

element - 1 : 2

element - 2 : 3

element - 3 : 4

element - 4 : 5

Input the position where to delete: 3

*Expected Output* :

The new list is : 1 2 4 5

Click me to see the solution

**16.** Write a program in C# Sharp to find the second largest element in an array. Go to the editor

Test Data :

Input the size of array : 5

Input 5 elements in the array :

element - 0 : 2

element - 1 : 9

element - 2 : 1

element - 3 : 4

element - 4 : 6

*Expected Output* :

The Second largest element in the array is: 6

Click me to see the solution

**17.** Write a program in C# Sharp to find the second smallest element in an array. Go to the editor

Test Data :

Input the size of array : 5

Input 5 elements in the array (value must be <9999) :

element - 0 : 0

element - 1 : 9

element - 2 : 4

element - 3 : 6

element - 4 : 5

*Expected Output* :

The Second smallest element in the array is : 4

Click me to see the solution

**18.** Write a program in C# Sharp for a 2D array of size 3x3 and print the matrix. Go to the editor

Test Data :

Input elements in the matrix :

element - [0],[0] : 1

element - [0],[1] : 2

element - [0],[2] : 3

element - [1],[0] : 4

element - [1],[1] : 5

element - [1],[2] : 6

element - [2],[0] : 7

element - [2],[1] : 8

element - [2],[2] : 9

*Expected Output* :

The matrix is :

1 2 3

4 5 6

7 8 9

Click me to see the solution

**19.** Write a program in C# Sharp for addition of two Matrices of same size. Go to the editor

Test Data :

Input the size of the square matrix (less than 5): 2

Input elements in the first matrix :

element - [0],[0] : 1

element - [0],[1] : 2

element - [1],[0] : 3

element - [1],[1] : 4

Input elements in the second matrix :

element - [0],[0] : 5

element - [0],[1] : 6

element - [1],[0] : 7

element - [1],[1] : 8

*Expected Output*:

The First matrix is:

1 2

3 4

The Second matrix is :

5 6

7 8

The Addition of two matrix is :

6 8

10 12

Click me to see the solution

**20.** Write a program in C# Sharp for subtraction of two Matrices. Go to the editor

Test Data :

Input the size of the square matrix (less than 5): 2

Input elements in the first matrix :

element - [0],[0] : 5

element - [0],[1] : 6

element - [1],[0] : 7

element - [1],[1] : 8

Input elements in the second matrix :

element - [0],[0] : 1

element - [0],[1] : 2

element - [1],[0] : 3

element - [1],[1] : 4

*Expected Output* :

The First matrix is :

5 6

7 8

The Second matrix is :

1 2

3 4

The Subtraction of two matrix is :

4 4

4 4

Click me to see the solution

**21.** Write a program in C# Sharp for multiplication of two square Matrices. Go to the editor

Test Data :

Input the rows and columns of first matrix : 2 2

Input the rows and columns of second matrix : 2 2

Input elements in the first matrix :

element - [0],[0] : 1

element - [0],[1] : 2

element - [1],[0] : 3

element - [1],[1] : 4

Input elements in the second matrix :

element - [0],[0] : 5

element - [0],[1] : 6

element - [1],[0] : 7

element - [1],[1] : 8

*Expected Output* :

The First matrix is :

1 2

3 4

The Second matrix is :

5 6

7 8

The multiplication of two matrix is :

19 22

43 50

Click me to see the solution

**22.** Write a program in C# Sharp to find transpose of a given matrix. Go to the editor

Test Data :

Input the rows and columns of the matrix : 2 2

Input elements in the first matrix :

element - [0],[0] : 1

element - [0],[1] : 2

element - [1],[0] : 3

element - [1],[1] : 4

*Expected Output* :

The matrix is :

1 2

3 4

The Transpose of a matrix is :

1 3

2 4

Click me to see the solution

**23.** Write a program in C# Sharp to find sum of right diagonals of a matrix.Go to the editor

Test Data :

Input the size of the square matrix : 2

Input elements in the first matrix :

element - [0],[0] : 1

element - [0],[1] : 2

element - [1],[0] : 3

element - [1],[1] : 4

*Expected Output* :

The matrix is :

1 2

3 4

Addition of the right Diagonal elements is :5

Elements in array are:

Click me to see the solution

**24.** Write a program in C# Sharp to find the sum of left diagonals of a matrix. Go to the editor

Test Data:

Input the size of the square matrix : 2

Input elements in the first matrix :

element - [0],[0] : 1

element - [0],[1] : 2

element - [1],[0] : 3

element - [1],[1] : 4

*Expected Output* :

The matrix is :

1 2

3 4

Addition of the left Diagonal elements is :5

Click me to see the solution

**25.** Write a program in C# Sharp to find sum of rows an columns of a Matrix.Go to the editor

Test Data :

Input the size of the square matrix: 2

Input elements in the first matrix:

element - [0],[0] : 5

element - [0],[1] : 6

element - [1],[0] : 7

element - [1],[1] : 8

*Expected Output* :

The First matrix is :

The matrix is :

5 6

7 8

The sum or rows and columns of the matrix is :

5 6 11

7 8 15

12 14

Click me to see the solution

**26.** Write a program in C# Sharp to print or display the lower triangular of a given matrix. Go to the editor

Test Data :

Input the size of the square matrix : 3

Input elements in the first matrix :

element - [0],[0] : 1

element - [0],[1] : 2

element - [0],[2] : 3

element - [1],[0] : 4

element - [1],[1] : 5

element - [1],[2] : 6

element - [2],[0] : 7

element - [2],[1] : 8

element - [2],[2] : 9

*Expected Output* :

The matrix is :

1 2 3

4 5 6

7 8 9

Setting zero in lower triangular matrix

1 2 3

0 5 6

0 0 9

Click me to see the solution

**27.** Write a program in C# Sharp to print or display the upper triangular of a given matrix. Go to the editor

Test Data :

Input the size of the square matrix : 3

Input elements in the first matrix :

element - [0],[0] : 1

element - [0],[1] : 2

element - [0],[2] : 3

element - [1],[0] : 4

element - [1],[1] : 5

element - [1],[2] : 6

element - [2],[0] : 7

element - [2],[1] : 8

element - [2],[2] : 9

*Expected Output* :

The matrix is :

1 2 3

4 5 6

7 8 9

Setting zero in upper triangular matrix

1 0 0

4 5 0

7 8 9

Click me to see the solution

**28.** Write a program in C# Sharp to calculate determinant of a 3 x 3 matrix. Go to the editor

Test Data :

Input elements in the first matrix :

element - [0],[0] : 1

element - [0],[1] : 0

element - [0],[2] : -1

element - [1],[0] : 0

element - [1],[1] : 0

element - [1],[2] : 1

element - [2],[0] : -1

element - [2],[1] : -1

element - [2],[2] : 0

*Expected Output*:

The matrix is :

1 0 -1

0 0 1

-1 -1 0

The Determinant of the matrix is: 1

Click me to see the solution

**29.** Write a program in C# Sharp to accept a matrix and determine whether it is a sparse matrix. Go to the editor

Test Data :

Input the number of rows of the matrix : 2

Input the number of columns of the matrix : 2

Input elements in the first matrix :

element - [0],[0] : 0

element - [0],[1] : 0

element - [1],[0] : 1

element - [1],[1] : 0

*Expected Output* :

The given matrix is sparse matrix.

There are 3 number of zeros in the matrix

Click me to see the solution

**30.** Write a program in C# Sharp to accept two matrices and check whether they are equal. Go to the editor

Test Data :

Input Rows and Columns of the 1st matrix :2 2

Input Rows and Columns of the 2nd matrix :2 2

Input elements in the first matrix :

element - [0],[0] : 1

element - [0],[1] : 2

element - [1],[0] : 3

element - [1],[1] : 4

Input elements in the second matrix :

element - [0],[0] : 1

element - [0],[1] : 2

element - [1],[0] : 3

element - [1],[1] : 4

*Expected Output* :

The first matrix is :

1 2

3 4

The second matrix is :

1 2

3 4

The Matrices can be compared :

Two matrices are equal.

Click me to see the solution

**31.** Write a program in C# Sharp to check whether a given matrix is an Identity Matrix. Go to the editor

Test Data :

Input the orders(2x2, 3x3, ...) of squere matrix : 2

Input elements in the matrix :

element - [0],[0] : 1

element - [0],[1] : 0

element - [1],[0] : 0

element - [1],[1] : 1

*Expected Output* :

The matrix is :

1 0

0 1

The matrix is an Identity Matrix.

Click me to see the solution

**32.** Write a C# Sharp program to get only the odd values from a given array of integers. Go to the editor

Test Data :

Only the odd values of the said array:

1

3

5

3

Only the odd values of the said array:

Click me to see the solution

**33.** Write a C# Sharp program to remove all duplicate elements from a given array and returns a new array. Go to the editor

Test Data :

Original array elements:

25

Anna

False

25

4/15/2021 12:10:51 PM

112.22

Anna

False

After removing duplicate elements from the said array:

25

Anna

False

4/15/2021 12:10:51 PM

112.22

Click me to see the solution

**34.** Write a C# Sharp program to find the missing number in a given array of numbers between 10 and 20. Go to the editor

Test Data :

Original array elements:

10

11

12

13

14

15

16

17

18

19

20

Missing number in the said array (10-20): 0

Original array elements:

11

12

13

14

15

16

17

18

19

20

Missing number in the said array (10-20): 10

Original array elements:

10

11

12

13

14

16

17

18

19

20

Missing number in the said array (10-20): 15

Original array elements:

10

11

12

13

14

15

16

17

18

19

Missing number in the said array (10-20): 20

Click me to see the solution

**35.** Write a C# Sharp program to calculate the sum of two lowest negative numbers of a given array of integers. Go to the editor

An integer (from the Latin integer meaning "whole") is colloquially defined as a number that can be written without a fractional component. For example, 21, 4, 0, and −2048 are integers,

Test Data :

Original array elements:

-10 -11 -12 -13 -14 15 16 17 18 19 20

Sum of two lowest negative numbers of the said array of integers: -27

Original array elements:

-1 -2 -4 0 3 4 5 9

Sum of two lowest negative numbers of the said array of integers: -6

Click me to see the solution

**36.** Write a C# Sharp program to re-arrange the elements in a given array of numbers and check the numbers are consecutive or not. Go to the editor

From Wikipedia,

Consecutive numbers are numbers that follow each other in order. They have a difference of 1 between every two numbers. In a set of consecutive numbers, the mean and the median are equal.

If n is a number, then the next numbers will be n+1 and n+2.

Examples:

Consecutive numbers that follow each other in order:

- 1, 2, 3, 4, 5
- -3, −2, −1, 0, 1, 2, 3, 4
- 6, 7, 8, 9, 10, 11, 12, 13

Original array elements:

-10 -11 -12 -13 -14 15 16 17 18 19 20

Check the numbers of the said array are consecutive or not! False

Original array elements:

-1 -2 -3 0 1 4 3 2

Check the numbers of the said array are consecutive or not! True

Click me to see the solution

**37.** Write a C# Sharp program that calculates the smallest gap between the numbers in an array of integers. Go to the editor

**Sample Data:**

({ 7, 5, 8, 9, 11, 23, 18 }) -> 1

({ 200, 300, 250, 151, 162 }) -> 11

Click me to see the solution

**38.** Write a C# Sharp program that takes an array of numbers and a digit. Now check whether the digit is present in the said array of numbers. Go to the editor

**Sample Data:**

({7, 5, 85, 9, 11, 23, 18}, "1") - > "Present"

({7, 5, 85, 9, 11, 23, 18}, "1") - > "Present"

({7, 5, 85, 9, 11, 23, 18}, "1") - > "Not present..!"

Click me to see the solution

**39.** Write a C# Sharp program that calculates the sum of all prime numbers in an array of numbers. Go to the editor

**Sample Data:**

({ 7, 5, 85, 9, 11, 23, 18 }) -> 46

({ 200, 300, 250, 151, 162 }) -> 151

Click me to see the solution

**40.** Write a C# Sharp program that takes and array of integers and find the smallest positive which is not present in the said array. Go to the editor

**Sample Data:**

({ 1,2,3,5,6,7}) -> 4

({-1, -2, 0, 1, 3, 4, 5, 6}) -> 2

Click me to see the solution

**41.** Write a C# Sharp program to find two numbers in an array of integers whose product is equal to a given number. Go to the editor

**Sample Data:**

({10, 18, 39, 75, 100}, 180) -> {10, 18}

({10, 18, 39, 75, 100}, 200) -> {}

({10, 18, 39, 75, 100}, 702) -> {18, 39}

Click me to see the solution

## C# Sharp Code Editor:

**More to Come !**

**Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.**

**Weekly Trends**- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises