C# Array Exercises
Array exercises with soluting using C#.
- Program that reads a number of values in an array and displays them in reverse order.
static void Main()
{
Console.Write("How many grades you want to input?: ");
int n = Convert.ToInt32(Console.ReadLine());
int[] grades = new int[n];
Console.WriteLine("Enter the grades:");
for (int i = 0; i < n; i++)
{
Console.Write($"Grade {i + 1}: ");
grades[i] = int.Parse(Console.ReadLine());
}
Console.WriteLine();
Console.WriteLine("Array in reverse order:");
for (int i = n - 1; i >= 0; i--)
{
Console.Write(grades[i] + " ");
}
}
Output:
How many grades you want to input?: 5
Enter the grades:
Grade 1: 89
Grade 2: 90
Grade 3: 78
Grade 4: 88
Grade 5: 86
Array in reverse order:
86 88 78 90 89
- Program that copies the element from one array into another array.
static void Main()
{
Console.Write("Enter the number of elements: ");
int n = Convert.ToInt32(Console.ReadLine());
int[] sourceArray = new int[n];
int[] destinationArray = new int[n];
Console.WriteLine("Enter the elements of the source array:");
for (int i = 0; i < n; i++)
{
Console.Write($"Element {i + 1}: ");
sourceArray[i] = Convert.ToInt32(Console.ReadLine());
}
// Copy elements from sourceArray to destinationArray
for (int i = 0; i < n; i++)
{
destinationArray[i] = sourceArray[i];
}
Console.WriteLine("\nElements in the destination array:");
for (int i = 0; i < n; i++)
{
Console.Write(destinationArray[i] + " ");
}
Console.WriteLine();
}
Output:
Enter the number of elements: 4
Enter the elements of the source array:
Element 1: 7
Element 2: 27
Element 3: 15
Element 4: 92
Elements in the destination array:
7 27 15 92
- Program that counts the total number of duplicate elements in an array.
static void Main()
{
Console.Write("Enter the number of elements: ");
int n = Convert.ToInt32(Console.ReadLine());
int[] array = new int[n];
Console.WriteLine("Enter the elements of the array:");
for (int i = 0; i < n; i++)
{
Console.Write($"Element {i + 1}: ");
array[i] = Convert.ToInt32(Console.ReadLine());
}
int duplicateCount = 0;
bool[] visited = new bool[n];
for (int i = 0; i < n; i++)
{
if (visited[i])
continue;
int count = 1;
for (int j = i + 1; j < n; j++)
{
if (array[i] == array[j])
{
visited[j] = true;
count++;
}
}
if (count > 1)
duplicateCount++;
}
Console.WriteLine($"Total number of duplicate elements: {duplicateCount}");
}
Output:
Enter the number of elements: 4
Enter the elements of the array:
Element 1: 7
Element 2: 27
Element 3: 7
Element 4: 15
Total number of duplicate elements: 1
- Program that counts the frequency of each element of an array.
static void Main()
{
Console.Write("Enter the number of elements: ");
int n = Convert.ToInt32(Console.ReadLine());
int[] array = new int[n];
Console.WriteLine("Enter the elements of the array:");
for (int i = 0; i < n; i++)
{
Console.Write($"Element {i + 1}: ");
array[i] = Convert.ToInt32(Console.ReadLine());
}
bool[] visited = new bool[n];
Console.WriteLine("\nFrequency of each element:");
for (int i = 0; i < n; i++)
{
if (visited[i])
continue;
int count = 1;
for (int j = i + 1; j < n; j++)
{
if (array[i] == array[j])
{
visited[j] = true;
count++;
}
}
Console.WriteLine($"{array[i]} occurs {count} time(s)");
}
}
Output:
Enter the number of elements: 4
Enter the elements of the array:
Element 1: 7
Element 2: 27
Element 3: 7
Element 4: 15
Frequency of each element:
7 occurs 2 time(s)
27 occurs 1 time(s)
15 occurs 1 time(s)
- Program that finds the maximum and minimum elements in an array.
static void Main(string[] args)
{
Console.Write("How many grades do you want to input?: ");
int n = Convert.ToInt32(Console.ReadLine());
int[] grades = new int[n];
Console.WriteLine("Enter grades...");
for (int i = 0; i < n; i++)
{
Console.Write($"Grade {i + 1}: ");
grades[i] = Convert.ToInt32(Console.ReadLine());
}
int max = grades[0];
int min = grades[0];
for (int i = 1; i < n; i++)
{
if (grades[i] > max)
max = grades[i];
if (grades[i] < min)
min = grades[i];
}
Console.WriteLine($"Highest grade: {max}");
Console.WriteLine($"Lowest grade: {min}");
}
Output:
How many grades do you want to input?: 5
Enter grades...
Grade 1: 87
Grade 2: 85
Grade 3: 96
Grade 4: 89
Grade 5: 88
Highest grade: 96
Lowest grade: 85
- Program that sort elements of an array in ascending order.
static void Main(string[] args)
{
Console.Write("Enter the number of elements: ");
int n = Convert.ToInt32(Console.ReadLine());
int[] array = new int[n];
Console.WriteLine("Enter the elements of the array:");
for (int i = 0; i < n; i++)
{
Console.Write($"Element {i + 1}: ");
array[i] = Convert.ToInt32(Console.ReadLine());
}
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (array[j] > array[j + 1])
{
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
Console.WriteLine("\nArray elements in ascending order:");
foreach (int element in array)
{
Console.Write(element + " ");
}
Console.WriteLine();
}
Output:
Enter the number of elements: 5
Enter the elements of the array:
Element 1: 7
Element 2: 27
Element 3: 9
Element 4: 2
Element 5: 15
Array elements in ascending order:
2 7 9 15 27
- Program that sort elements of an array in descending order.
static void Main(string[] args)
{
Console.Write("Enter the number of elements: ");
int n = Convert.ToInt32(Console.ReadLine());
int[] array = new int[n];
Console.WriteLine("Enter the elements of the array:");
for (int i = 0; i < n; i++)
{
Console.Write($"Element {i + 1}: ");
array[i] = Convert.ToInt32(Console.ReadLine());
}
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (array[j] < array[j + 1])
{
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
Console.WriteLine("\nArray elements in descending order:");
foreach (int element in array)
{
Console.Write(element + " ");
}
Console.WriteLine();
}
Output:
Enter the number of elements: 5
Enter the elements of the array:
Element 1: 7
Element 2: 27
Element 3: 9
Element 4: 2
Element 5: 15
Array elements in descending order:
27 15 9 7 2
🔚 end of document 🔚