Example1: Write a C# program to creating an array of the string as week days, store day values in the weekdays, and prints each value.
Sol//
using System;
class Program
{
static void Main()
{
// Create an array of strings representing weekdays
string[] weekdays = new string[7];
// Store day values in the weekdays array
weekdays[0] = "Sunday";
weekdays[1] = "Monday";
weekdays[2] = "Tuesday";
weekdays[3] = "Wednesday";
weekdays[4] = "Thursday";
weekdays[5] = "Friday";
weekdays[6] = "Saturday";
// Print each value in the weekdays array
Console.WriteLine("Weekdays:");
foreach (string day in weekdays)
{
Console.WriteLine(day);
}
}
}
Example2: Write a C# program to read an array with 10 integer number
and find sum of the total numbers of and print array?
Sol//
using System;
class Program
{
static void Main()
{
// Declare an array to store 10 integer numbers
int[] numbers = new int[10];
// Read 10 integer numbers from the user
Console.WriteLine("Enter 10 integer numbers:");
for (int i = 0; i < 10; i++)
{
Console.Write($"Enter number {i + 1}: ");
numbers[i] = Convert.ToInt32(Console.ReadLine());
}
// Calculate the sum of the numbers
int sum = 0;
foreach (int num in numbers)
{
sum += num;
}
// Print the array
Console.WriteLine("Array:");
foreach (int num in numbers)
{
Console.Write(num + " ");
}
// Print the sum of the numbers
Console.WriteLine($"\nSum of the numbers: {sum}");
}
}
Example3 : Write a C# program to read an array with 10 integer number
and sorting this array ?
Sol//
using System;
class Program
{
static void Main()
{
// Declare an array to store 10 integer numbers
int[] numbers = new int[10];
// Read 10 integer numbers from the user
Console.WriteLine("Enter 10 integer numbers:");
for (int i = 0; i < 10; i++)
{
Console.Write($"Enter number {i + 1}: ");
numbers[i] = Convert.ToInt32(Console.ReadLine());
}
// Sort the array in ascending order
Array.Sort(numbers);
// Print the sorted array
Console.WriteLine("Sorted Array:");
foreach (int num in numbers)
{
Console.Write(num + " ");
}
}
}
Example4 /Write C# program to read an array with 10 integer numbers
and search the value of an element in this array?
Sol//
using System;
class Program
{
static void Main()
{
// Declare an array to store 10 integer numbers
int[] numbers = new int[10];
// Read 10 integer numbers from the user
Console.WriteLine("Enter 10 integer numbers:");
for (int i = 0; i < 10; i++)
{
Console.Write($"Enter number {i + 1}: ");
numbers[i] = Convert.ToInt32(Console.ReadLine());
}
// Read the value to search for
Console.Write("Enter the value to search for: ");
int searchValue = Convert.ToInt32(Console.ReadLine());
// Search for the value in the array
bool found = false;
for (int i = 0; i < 10; i++)
{
if (numbers[i] == searchValue)
{
Console.WriteLine($"Value {searchValue} found at index {i}.");
found = true;
break; // Stop searching once the value is found
}
}
// If the value is not found, print a message
if (!found)
{
Console.WriteLine($"Value {searchValue} not found in the array.");
}
}
}
Example5 / Write a C# program to create an array of 10 integer numbers
and find maximum and minimum number in the array and print array?
Sol//
using System;
class Program
{
static void Main()
{
// Declare an array to store 10 integer numbers
int[] numbers = new int[10];
// Read 10 integer numbers from the user
Console.WriteLine("Enter 10 integer numbers:");
for (int i = 0; i < 10; i++)
{
Console.Write($"Enter number {i + 1}: ");
numbers[i] = Convert.ToInt32(Console.ReadLine());
}
// Find the maximum and minimum numbers in the array
int maxNumber = numbers[0];
int minNumber = numbers[0];
for (int i = 1; i < 10; i++)
{
if (numbers[i] > maxNumber)
{
maxNumber = numbers[i];
}
if (numbers[i] < minNumber)
{
minNumber = numbers[i];
}
}
// Print the array
Console.WriteLine("Array:");
foreach (int num in numbers)
{
Console.Write(num + " ");
}
// Print the maximum and minimum numbers
Console.WriteLine($"\nMaximum number: {maxNumber}");
Console.WriteLine($"Minimum number: {minNumber}");
}
}
Example6 / Write a C# program to create an array (A ) of 5 integer
numbers and an array (B) of 5 integer numbers and the summation of A
and B in an array (C) an and print array A,B,C?
Sol//
using System;
class Program
{
static void Main()
{
// Create arrays A and B of 5 integer numbers
int[] A = new int[5] { 1, 2, 3, 4, 5 };
int[] B = new int[5] { 6, 7, 8, 9, 10 };
// Create array C to store the summation of A and B
int[] C = new int[5];
// Calculate the summation of A and B and store the results in array C
for (int i = 0; i < 5; i++)
{
C[i] = A[i] + B[i];
}
// Print array A
Console.WriteLine("Array A:");
foreach (int num in A)
{
Console.Write(num + " ");
}
// Print array B
Console.WriteLine("\nArray B:");
foreach (int num in B)
{
Console.Write(num + " ");
}
// Print array C
Console.WriteLine("\nArray C (Summation of A and B):");
foreach (int num in C)
{
Console.Write(num + " ");
}
}
}
Example 7/ Write a C# program to create an array of 10 integer numbers then:
1- Find the maximum number with its location?
2 - Find the minimum number with its location?
3- Increment each number in odd position by 5?
4- Count the even and odd numbers in an array?
5- Find the frequency of a number entered by the user?
Sol//
using System;
class Program
{
static void Main()
{
// Create an array of 10 integer numbers
int[] numbers = new int[10] { 5, 8, 12, 3, 9, 7, 15, 2, 10, 6 };
// Task 1: Find the maximum number with its location
int maxNumber = numbers[0];
int maxIndex = 0;
for (int i = 1; i < numbers.Length; i++)
{
if (numbers[i] > maxNumber)
{
maxNumber = numbers[i];
maxIndex = i;
}
}
Console.WriteLine($"Maximum number is {maxNumber} at index {maxIndex}.");
// Task 2: Find the minimum number with its location
int minNumber = numbers[0];
int minIndex = 0;
for (int i = 1; i < numbers.Length; i++)
{
if (numbers[i] < minNumber)
{
minNumber = numbers[i];
minIndex = i;
}
}
Console.WriteLine($"Minimum number is {minNumber} at index {minIndex}.");
// Task 3: Increment each number in odd position by 5
for (int i = 0; i < numbers.Length; i++)
{
if (i % 2 != 0)
{
numbers[i] += 5;
}
}
Console.WriteLine("Array after incrementing numbers in odd positions by 5:");
PrintArray(numbers);
// Task 4: Count the even and odd numbers in the array
int evenCount = 0;
int oddCount = 0;
foreach (int num in numbers)
{
if (num % 2 == 0)
{
evenCount++;
}
else
{
oddCount++;
}
}
Console.WriteLine($"Count of even numbers: {evenCount}");
Console.WriteLine($"Count of odd numbers: {oddCount}");
// Task 5: Find the frequency of a number entered by the user
Console.Write("Enter a number to find its frequency: ");
int targetNumber = Convert.ToInt32(Console.ReadLine());
int frequency = 0;
foreach (int num in numbers)
{
if (num == targetNumber)
{
frequency++;
}
}
Console.WriteLine($"Frequency of number {targetNumber} in the array: {frequency}");
}
// Helper method to print an array
static void PrintArray(int[] arr)
{
foreach (int num in arr)
{
Console.Write(num + " ");
}
Console.WriteLine();
}
}
Example8: Write a C# of a 2-D array of size 5 x 2. Then print it?
Sol//
using System;
class Program
{
static void Main()
{
// Create a 2-D array of size 5x2
int[,] array2D = new int[5, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 }, { 9, 10 } };
// Print the 2-D array
Console.WriteLine("2-D Array:");
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 2; j++)
{
Console.Write(array2D[i, j] + " ");
}
Console.WriteLine();
}
}
}
Example9: Write a C# program to create two multidimensional arrays of
same size. Accept value from user and store them in first array. Then copy
all the elements of first array are second array and print output.
Sol//
using System;
class Program
{
static void Main()
{
// Define the size of the multidimensional arrays
const int rows = 3;
const int columns = 3;
// Create the first multidimensional array
int[,] firstArray = new int[rows, columns];
// Accept values from the user and store them in the first array
Console.WriteLine("Enter values for the first array:");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
Console.Write($"Enter value for element at position ({i},{j}): ");
firstArray[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
// Create the second multidimensional array with the same size
int[,] secondArray = new int[rows, columns];
// Copy all the elements of the first array to the second array
Array.Copy(firstArray, secondArray, firstArray.Length);
// Print the contents of the second array
Console.WriteLine("\nContents of the second array:");
PrintArray(secondArray);
}
// Helper method to print a multidimensional array
static void PrintArray(int[,] array)
{
int rows = array.GetLength(0);
int columns = array.GetLength(1);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
Console.Write(array[i, j] + " ");
}
Console.WriteLine();
}
}
}
Example10: read a two-dimensional array from the console. First, we read the
values (lengths) of the two-dimensions and then by using two nested loops we
assign the value of each element, and in the end we print out the values of the
array.
Sol//
using System;
class Program
{
static void Main()
{
// Read the lengths of the two dimensions
Console.Write("Enter the number of rows: ");
int rows = int.Parse(Console.ReadLine());
Console.Write("Enter the number of columns: ");
int columns = int.Parse(Console.ReadLine());
// Create the two-dimensional array based on the input dimensions
int[,] array = new int[rows, columns];
// Read values for each element of the array
Console.WriteLine("Enter the values for the array:");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
Console.Write($"Enter value for element at position ({i},{j}): ");
array[i, j] = int.Parse(Console.ReadLine());
}
}
// Print the values of the array
Console.WriteLine("\nValues of the array:");
PrintArray(array);
}
// Helper method to print the values of the two-dimensional array
static void PrintArray(int[,] array)
{
int rows = array.GetLength(0);
int columns = array.GetLength(1);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
Console.Write(array[i, j] + " ");
}
Console.WriteLine();
}
}
}
Example11: Write a c# program to add the diagonal of a 2-Dimensional array?
Sol//
using System;
class Program
{
static void Main()
{
// Create a 2-D array
int[,] array = {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
// Calculate the sum of diagonal elements
int diagonalSum = 0;
for (int i = 0; i < array.GetLength(0) && i < array.GetLength(1); i++)
{
diagonalSum += array[i, i];
}
// Print the sum of diagonal elements
Console.WriteLine("Sum of diagonal elements: " + diagonalSum);
}
}
Example6: Write a C# program to create an array of two dimension [2,3]
of integer numbers and print its elements ?
Sol//
using System;
class Program
{
static void Main()
{
// Create a 2-dimensional array of size 2x3
int[,] array = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };
// Print the elements of the array
Console.WriteLine("Elements of the 2-D array:");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
Console.WriteLine($"Element at position ({i}, {j}): {array[i, j]}");
}
}
}
}
13) Write a C# program to create an array of two dimension [4,3] of integer
numbers and find
1- Print the average of its elements.
2- The average of odd and even numbers.
3- The maximum and minimum numbers and their locations.
4- the sum of each row.
5- The sum of each column.
6- Exchange row 2 with row 4.
7- Reverse column 3.
8- The count of numbers divided by 5.
9- Exchange col 1 with col 2.
Sol//
using System;
class Program
{
static void Main()
{
// Create a 2-dimensional array of size 4x3
int[,] array = new int[4, 3] {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 },
{ 10, 11, 12 }
};
// Task 1: Print the average of all elements
double totalSum = 0;
int totalCount = array.GetLength(0) * array.GetLength(1);
foreach (int num in array)
{
totalSum += num;
}
double average = totalSum / totalCount;
Console.WriteLine($"1. Average of all elements: {average}");
// Task 2: Calculate the average of odd and even numbers
double oddSum = 0, evenSum = 0;
int oddCount = 0, evenCount = 0;
foreach (int num in array)
{
if (num % 2 == 0)
{
evenSum += num;
evenCount++;
}
else
{
oddSum += num;
oddCount++;
}
}
double oddAverage = oddSum / oddCount;
double evenAverage = evenSum / evenCount;
Console.WriteLine($"2. Average of odd numbers: {oddAverage}");
Console.WriteLine($" Average of even numbers: {evenAverage}");
// Task 3: Find maximum and minimum numbers and their locations
int maxNumber = array[0, 0];
int minNumber = array[0, 0];
int maxRow = 0, maxCol = 0, minRow = 0, minCol = 0;
for (int i = 0; i < array.GetLength(0); i++)
{
for (int j = 0; j < array.GetLength(1); j++)
{
if (array[i, j] > maxNumber)
{
maxNumber = array[i, j];
maxRow = i;
maxCol = j;
}
if (array[i, j] < minNumber)
{
minNumber = array[i, j];
minRow = i;
minCol = j;
}
}
}
Console.WriteLine($"3. Maximum number: {maxNumber} at [{maxRow},{maxCol}]");
Console.WriteLine($" Minimum number: {minNumber} at [{minRow},{minCol}]");
// Task 4: Sum of each row
Console.WriteLine("4. Sum of each row:");
for (int i = 0; i < array.GetLength(0); i++)
{
int rowSum = 0;
for (int j = 0; j < array.GetLength(1); j++)
{
rowSum += array[i, j];
}
Console.WriteLine($" Sum of row {i + 1}: {rowSum}");
}
// Task 5: Sum of each column
Console.WriteLine("5. Sum of each column:");
for (int j = 0; j < array.GetLength(1); j++)
{
int colSum = 0;
for (int i = 0; i < array.GetLength(0); i++)
{
colSum += array[i, j];
}
Console.WriteLine($" Sum of column {j + 1}: {colSum}");
}
// Task 6: Exchange row 2 with row 4
for (int j = 0; j < array.GetLength(1); j++)
{
int temp = array[1, j];
array[1, j] = array[3, j];
array[3, j] = temp;
}
Console.WriteLine("6. After exchanging row 2 with row 4:");
PrintArray(array);
// Task 7: Reverse column 3
for (int i = 0; i < array.GetLength(0) / 2; i++)
{
int temp = array[i, 2];
array[i, 2] = array[array.GetLength(0) - 1 - i, 2];
array[array.GetLength(0) - 1 - i, 2] = temp;
}
Console.WriteLine("7. After reversing column 3:");
PrintArray(array);
// Task 8: Count of numbers divided by 5
int countDivisibleBy5 = 0;
foreach (int num in array)
{
if (num % 5 == 0)
{
countDivisibleBy5++;
}
}
Console.WriteLine($"8. Count of numbers divisible by 5: {countDivisibleBy5}");
// Task 9: Exchange col 1 with col 2
for (int i = 0; i < array.GetLength(0); i++)
{
int temp = array[i, 0];
array[i, 0] = array[i, 1];
array[i, 1] = temp;
}
Console.WriteLine("9. After exchanging column 1 with column 2:");
PrintArray(array);
}
// Helper method to print a 2-dimensional array
static void PrintArray(int[,] array)
{
for (int i = 0; i < array.GetLength(0); i++)
{
for (int j = 0; j < array.GetLength(1); j++)
{
Console.Write(array[i, j] + "\t");
}
Console.WriteLine();
}
}
}
14) Write a C# program to create an array of two dimension [5,5] of integer
numbers and find:
1- The sum of the elements on main diagonal.
2- The sum of the elements on secondary diagonal.
3- The sum of elements on triangle upper main diagonal.
4- The sum of elements on triangle lower main diagonal.
5- Exchange the elements of two diagonal.
6- Convert the two dimensional array into one dimensional array.
Sol//
using System;
class Program
{
static void Main()
{
// Create a 2-dimensional array of size 5x5
int[,] array = new int[5, 5] {
{ 1, 2, 3, 4, 5 },
{ 6, 7, 8, 9, 10 },
{ 11, 12, 13, 14, 15 },
{ 16, 17, 18, 19, 20 },
{ 21, 22, 23, 24, 25 }
};
// Task 1: Calculate the sum of elements on the main diagonal
int mainDiagonalSum = 0;
for (int i = 0; i < 5; i++)
{
mainDiagonalSum += array[i, i];
}
Console.WriteLine($"1. Sum of elements on main diagonal: {mainDiagonalSum}");
// Task 2: Calculate the sum of elements on the secondary diagonal
int secondaryDiagonalSum = 0;
for (int i = 0; i < 5; i++)
{
secondaryDiagonalSum += array[i, 4 - i];
}
Console.WriteLine($"2. Sum of elements on secondary diagonal: {secondaryDiagonalSum}");
// Task 3: Calculate the sum of elements on the upper main diagonal triangle
int upperMainDiagonalSum = 0;
for (int i = 0; i < 5; i++)
{
for (int j = i; j < 5; j++)
{
upperMainDiagonalSum += array[i, j];
}
}
Console.WriteLine($"3. Sum of elements on upper main diagonal triangle: {upperMainDiagonalSum}");
// Task 4: Calculate the sum of elements on the lower main diagonal triangle
int lowerMainDiagonalSum = 0;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j <= i; j++)
{
lowerMainDiagonalSum += array[i, j];
}
}
Console.WriteLine($"4. Sum of elements on lower main diagonal triangle: {lowerMainDiagonalSum}");
// Task 5: Exchange the elements of two diagonals
for (int i = 0; i < 5; i++)
{
int temp = array[i, i];
array[i, i] = array[i, 4 - i];
array[i, 4 - i] = temp;
}
Console.WriteLine("5. After exchanging the elements of two diagonals:");
PrintArray(array);
// Task 6: Convert the two-dimensional array into a one-dimensional array
int[] oneDimensionalArray = new int[25];
int index = 0;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
oneDimensionalArray[index++] = array[i, j];
}
}
Console.WriteLine("6. Converted one-dimensional array:");
PrintArray(oneDimensionalArray);
}
// Helper method to print a one-dimensional array
static void PrintArray(int[] array)
{
foreach (int num in array)
{
Console.Write(num + " ");
}
Console.WriteLine();
}
// Helper method to print a two-dimensional array
static void PrintArray(int[,] array)
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
Console.Write(array[i, j] + " ");
}
Console.WriteLine();
}
}
}
Example 15: Write a C# program to create an 2-D array a(3X 3) and put even & odd elements of an
in 2 separate single dimension arrays and print these arrays?
Sol//
using System;
class Program
{
static void Main()
{
// Create a 2-dimensional array a(3x3)
int[,] a = {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
// Declare arrays for even and odd elements
int[] evenArray = new int[9]; // Max size for the case all elements are even
int[] oddArray = new int[9]; // Max size for the case all elements are odd
int evenCount = 0, oddCount = 0;
// Iterate through the elements of the 2D array
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
// Check if the element is even or odd
if (a[i, j] % 2 == 0)
{
evenArray[evenCount++] = a[i, j];
}
else
{
oddArray[oddCount++] = a[i, j];
}
}
}
// Print even elements array
Console.WriteLine("Even elements:");
for (int i = 0; i < evenCount; i++)
{
Console.Write(evenArray[i] + " ");
}
Console.WriteLine();
// Print odd elements array
Console.WriteLine("Odd elements:");
for (int i = 0; i < oddCount; i++)
{
Console.Write(oddArray[i] + " ");
}
Console.WriteLine();
}
}