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. Go to the editor]
1. Write a C# Sharp program that stores elements in an array and prints them.
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 C# Sharp program to read n values in an array and display them in reverse order.
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 array elements.
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 C# Sharp program to copy the elements of one array into another array.
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 C# Sharp program in to count duplicate elements in an array.
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.
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 C# Sharp program to merge two arrays of the same size sorted in ascending order.
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 C# Sharp program to count the frequency of each element in an array.
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 C# Sharp program to find the maximum and minimum elements in an array.
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 into separate arrays.
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 C# Sharp program to sort elements of an array in ascending order.
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 C# Sharp program to sort array elements in descending order.
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 C# Sharp program to insert an additional value into an array (sorted list).
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 C# Sharp program to insert another value in the array (unsorted list).
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 C# Sharp program to delete an element at the desired position from an array.
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 C# Sharp program to find the second largest element in an array.
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 C# Sharp program to find the second smallest element in an array.
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.
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 C# Sharp program for adding two matrices of the same size.
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 C# Sharp program for the subtraction of two matrixes.
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 C# Sharp program for multiplication of two square matrices.
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 C# Sharp program to find the transpose of a given matrix.
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 C# Sharp program to find the sum of the right diagonals of a matrix.
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 C# Sharp program to find the sum of the left diagonals of a matrix.
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 C# Sharp program to find the sum of rows and columns in a matrix.
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.
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 C# Sharp program to print or display an upper triangular matrix.
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 C# Sharp program to calculate the determinant of a 3 x 3 matrix.
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 C# Sharp program to accept a matrix and determine whether it is a sparse matrix.
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 C# Sharp program to accept two matrices and check equality.
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 C# Sharp program to Check whether a Given Matrix is an Identity Matrix.
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 odd values from a given integer array.
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 that removes all duplicate elements from a given array and returns an updated array.
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.
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 the two lowest negative numbers in a given array of integers.
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 whether the numbers are consecutive or not.
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 numbers in an array of integers.
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. Check whether the digit is present in this array of numbers.
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.
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 an array of integers and finds the smallest positive integer that is not present in that array.
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.
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.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/csharp-exercises/array/index.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics