0% found this document useful (0 votes)
3 views

C# Assignment

Uploaded by

yosefmuluye42
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

C# Assignment

Uploaded by

yosefmuluye42
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

1. Write a C# program that prints the first 10 even numbers using a for loop.

Answer
using System;
public class EvenNumberPrinter
{
public static void Main()
{
for (int i = 2; i <= 10; i += 2)
{
Console.WriteLine(i);
Output
2
4
6
8
10
2. Write a C# program that takes a number (1 to 7) as input and prints the corresponding day
of the week using a switch statement.
Answer
using System;
public class DayOfTheWeekPrinter
{
public static void Main()
{
Console.Write("Enter a number (1-7): ");
int dayNumber = int.Parse(Console.ReadLine());
switch (dayNumber)
{
case 1:
Console.WriteLine("Monday");
break;
case 2:
Console.WriteLine("Tuesday");
break;
case 3:
Console.WriteLine("Wednesday");
break;
case 4:
Console.WriteLine("Thursday");
break;
case 5:
Console.WriteLine("Friday");
break;
case 6:
Console.WriteLine("Saturday");
break;
case 7:
Console.WriteLine("Sunday");
break;
default:
Console.WriteLine("Invalid input. Please enter a number between 1 and 7.");
break;
}
}
}
Example of Out put
if the user enters 5 out put is Friday

3. Write a C# program that takes two integers as input and prints whether their
sum is positive, negative, or zero. Use nested if statements.
Answer
using System;
public class SumAnalyzer
{
public static void Main()
{
Console.Write("Enter the first number: ");
int num1 = int.Parse(Console.ReadLine());
Console.Write("Enter the second number: ");
int num2 = int.Parse(Console.ReadLine());
int sum = num1 + num2;
if (sum > 0)
{
Console.WriteLine($"The sum of {num1} and {num2} is positive: {sum}");
}
else
{
if (sum < 0)
{
Console.WriteLine($"The sum of {num1} and {num2} is negative: {sum}");
}
else
{
Console.WriteLine($"The sum of {num1} and {num2} is zero: {sum}");
}
}
}
}
Example of Out put
For example, if the user enters 5 and 3, the program will output:
The sum of 5 and 3 is positive: 8
If the user enters -2 and 4, the program will output:
The sum of -2 and 4 is positive: 2
If the user enters 0 and 0, the program will output:
The sum of 0 and 0 is zero: 0
4. Write a C# program that creates an array of integers and prints its length.

Answer
using System;
public class ArrayLengthPrinter
{
public static void Main()
{
// Create an array of integers
int[] numbers = { 10, 20, 30, 40, 50 };

// Get the length of the array


int arrayLength = numbers.Length;
// Print the length of the array
Console.WriteLine($"The length of the array is: {arrayLength}");
}
}

Out put
The length of the array is: 5

5. Write a C# program that creates a 3x3 matrix (2D array) of integers and
performs the following operations:
 Populate the matrix with random numbers between 1 and 10.
 Print the matrix.
 Find the sum of all elements in the matrix.
 Find the maximum value in the matrix.
 Transpose the matrix (convert rows to columns and vice versa) and print the transposed
matrix.
Answer
using System;
public class MatrixOperations
{
public static void Main()
{
// Create a 3x3 matrix of integers
int[,] matrix = new int[3, 3];
// Populate the matrix with random numbers between 1 and 10
Random random = new Random();
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
matrix[i, j] = random.Next(1, 11);
}
}

// Print the matrix


Console.WriteLine("The matrix:");
PrintMatrix(matrix);
// Find the sum of all elements in the matrix
int sum = FindMatrixSum(matrix);
Console.WriteLine($"The sum of all elements in the matrix is: {sum}");
// Find the maximum value in the matrix
int maxValue = FindMatrixMax(matrix);
Console.WriteLine($"The maximum value in the matrix is: {maxValue}");
// Transpose the matrix and print the transposed matrix
int[,] transposedMatrix = TransposeMatrix(matrix);
Console.WriteLine("The transposed matrix:");
PrintMatrix(transposedMatrix);
}
// Helper method to print the matrix
private static void PrintMatrix(int[,] matrix)
{
for (int i = 0; i < matrix.GetLength(0); i++)
{
for (int j = 0; j < matrix.GetLength(1); j++)
{
Console.Write($"{matrix[i, j]} ");
}
Console.WriteLine();
}
Console.WriteLine();
}

// Helper method to find the sum of all elements in the matrix


private static int FindMatrixSum(int[,] matrix)
{
int sum = 0;
for (int i = 0; i < matrix.GetLength(0); i++)
{
for (int j = 0; j < matrix.GetLength(1); j++)
{
sum += matrix[i, j];
}
}
return sum;
}
// Helper method to find the maximum value in the matrix
private static int FindMatrixMax(int[,] matrix)
{
int maxValue = int.MinValue;
for (int i = 0; i < matrix.GetLength(0); i++)
{
for (int j = 0; j < matrix.GetLength(1); j++)
{
if (matrix[i, j] > maxValue)
{
maxValue = matrix[i, j];
}
}
}
return maxValue;
}
// Helper method to transpose the matrix
private static int[,] TransposeMatrix(int[,] matrix)
{
int[,] transposedMatrix = new int[matrix.GetLength(1), matrix.GetLength(0)];
for (int i = 0; i < matrix.GetLength(0); i++)
{
for (int j = 0; j < matrix.GetLength(1); j++)
{
transposedMatrix[j, i] = matrix[i, j];
}
}
return transposedMatrix;
}
}
Example Out put
 The matrix:
384
619
275
 The sum of all elements in the matrix is: 45
 The maximum value in the matrix is: 9
 The transposed matrix:
362
817
495

You might also like