AMITY UNIVERSITY MADHYA PRADESH
Practical File
Of
[Link] LAB
(CSE 824)
Submitted To: Submitted by:
Mr Manish Khule Devashish Pandey
ASET, AUMP B. Tech CSE
Semester-VIII’A
A60205221211
Department of Computer Science and Engineering
Amity School Of Engineering & Technology
INDEX
S. No Practical Name Page No Date Sign
Write a C# program that checks whether a given 1
1 number is prime or not.
Write a C# program to perform matrix addition and 2-4
2 matrix multiplication.
Write a C# program that demonstrates the concepts of 5-6
3 classes, objects, constructors, and inheritance.
Write a C# program to connect to a Microsoft Access 7
4 database and retrieve records from a table using
[Link].
Create a simple [Link] Web Forms application with 8-9
5 text boxes, labels, and buttons to collect and validate
user input.
Write a C# program to demonstrate multi-threading by 10
6 creating two threads that print numbers 1 to 10 and 11
to 20 respectively.
Develop a simple [Link] Web Service that performs 11
7 basic arithmetic operations like addition, subtraction,
multiplication, and division.
Create a Windows Forms application with different 12-13
8 controls such as buttons, text boxes, and labels.
Implement event handlers for user interactions.
Create a C# program that uses LINQ to query a list of 14
9 students and find those whose age is above 20.
Write a C# program to demonstrate exception handling 15-16
10 by creating custom exceptions and handling runtime
errors.
Exercise 1: C# Program to Find Prime Numbers
Write a C# program that checks whether a given number is prime or not.
using System;
class Program{
static bool IsPrime(int number){
if (number <= 1)
return false;
for (int i = 2; i * i <= number; i++){
if (number % i == 0)
return false;
}
return true;
}
static void Main(){
[Link]("Enter a number: ");
int num;
if ([Link]([Link](), out num)){
if (IsPrime(num))
[Link]($"{num} is a prime number.");
else
[Link]($"{num} is not a prime number.");
}
else{
[Link]("Invalid input. Please enter an integer.");
}
}
}
1
Exercise 2: Matrix Operations
Write a C# program to perform matrix addition and matrix multiplication.
using System;
class MatrixOperations
{
static int[,] AddMatrices(int[,] A, int[,] B)
{
int rows = [Link](0);
int cols = [Link](1);
int[,] result = new int[rows, cols];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
result[i, j] = A[i, j] + B[i, j];
}
}
return result;
}
static int[,] MultiplyMatrices(int[,] A, int[,] B)
{
int rowsA = [Link](0);
int colsA = [Link](1);
int rowsB = [Link](0);
int colsB = [Link](1);
if (colsA != rowsB)
{
throw new InvalidOperationException("Matrix multiplication is not possible with the given
dimensions.");
}
int[,] result = new int[rowsA, colsB];
for (int i = 0; i < rowsA; i++)
{
for (int j = 0; j < colsB; j++)
{
result[i, j] = 0;
for (int k = 0; k < colsA; k++)
2
{
result[i, j] += A[i, k] * B[k, j];
}
}
}
return result;
}
static void PrintMatrix(int[,] matrix)
{
int rows = [Link](0);
int cols = [Link](1);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
[Link](matrix[i, j] + " ");
}
[Link]();
}
}
static void Main()
{
int[,] A = { { 1, 2 }, { 3, 4 } };
int[,] B = { { 5, 6 }, { 7, 8 } };
[Link]("Matrix A:");
PrintMatrix(A);
[Link]("Matrix B:");
PrintMatrix(B);
int[,] sum = AddMatrices(A, B);
[Link]("Matrix Addition Result:");
PrintMatrix(sum);
int[,] product = MultiplyMatrices(A, B);
[Link]("Matrix Multiplication Result:");
PrintMatrix(product);
}
}
3
4
Exercise 3: Object-Oriented Programming (OOP) Concepts
Write a C# program demonstrating the concepts of classes, objects, constructors, and
inheritance.
using System;
class Person{
public string Name { get; set; }
public int Age { get; set; }
public Person(string name, int age) {
Name = name;
Age = age;
}
public void DisplayInfo(){
[Link]($"Name: {Name}, Age: {Age}");
}
}
class Student : Person{
public int StudentID { get; set; }
public Student(string name, int age, int studentID) : base(name, age) {
StudentID = studentID;
}
public void ShowStudentInfo() {
DisplayInfo();
[Link]($"Student ID: {StudentID}");
}
}
class Program{
static void Main() {
Person person = new Person("Alice", 30);
[Link]();
[Link]();
Student student = new Student("Bob", 20, 12345);
[Link]();
}
}
5
6
Exercise 4: [Link] - Database Connection
Write a C# program to connect to a Microsoft Access database and retrieve records from a
table using [Link].
using System;
using [Link];
class DatabaseConnection{
static void Main(){
string connectionString = "Provider=[Link].12.0;Data
Source=[Link];";
string query = "SELECT * FROM Students";
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
OleDbCommand command = new OleDbCommand(query, connection);
try{
[Link]();
OleDbDataReader reader = [Link]();
while ([Link]()) {
[Link]($"ID: {reader["ID"]}, Name: {reader["Name"]}, Age:
{reader["Age"]}");
}
[Link]();
}
catch (Exception ex){
[Link]("Error: " + [Link]);
}
}
}
}
7
Exercise 5: Web Forms and Validation
Create a simple [Link] Web Forms application with text boxes, labels, and buttons to collect
and validate user input.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="[Link]"
Inherits="WebFormsValidation._Default" %>
<!DOCTYPE html>
<html xmlns="[Link]
<head runat="server">
<title>Web Forms Validation</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblName" runat="server" Text="Name:"></asp:Label>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvName" runat="server" ControlToValidate="txtName"
ErrorMessage="Name is required!" ForeColor="Red"></asp:RequiredFieldValidator>
<br /><br />
<asp:Label ID="lblEmail" runat="server" Text="Email:"></asp:Label>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvEmail" runat="server" ControlToValidate="txtEmail"
ErrorMessage="Email is required!" ForeColor="Red"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="revEmail" runat="server"
ControlToValidate="txtEmail"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ErrorMessage="Invalid
Email Format!" ForeColor="Red"></asp:RegularExpressionValidator>
<br /><br />
<asp:Label ID="lblAge" runat="server" Text="Age:"></asp:Label>
<asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvAge" runat="server" ControlToValidate="txtAge"
ErrorMessage="Age is required!" ForeColor="Red"></asp:RequiredFieldValidator>
<asp:RangeValidator ID="rvAge" runat="server" ControlToValidate="txtAge"
MinimumValue="1" MaximumValue="120" Type="Integer" ErrorMessage="Age must be between 1
and 120!" ForeColor="Red"></asp:RangeValidator>
<br /><br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
<br /><br />
<asp:Label ID="lblMessage" runat="server" ForeColor="Green"></asp:Label>
8
</div>
</form>
</body>
</html>
// [Link]
using System;
public partial class _Default : [Link]{
protected void btnSubmit_Click(object sender, EventArgs e){
if ([Link]){
[Link] = "Form submitted successfully!";
}
}
}
9
Exercise 6: Multi-threading in C#
Write a C# program to demonstrate multi-threading by creating two threads that print
numbers 1 to 10 and 11 to 20 respectively.
using System;
using [Link];
class MultiThreadingDemo{
static void PrintNumbers(int start, int end){
for (int i = start; i <= end; i++){
[Link](i);
[Link](500); // Adding delay to simulate work
}
}
static void Main() {
Thread thread1 = new Thread(() => PrintNumbers(1, 10));
Thread thread2 = new Thread(() => PrintNumbers(11, 20));
[Link]();
[Link]();
[Link]();
[Link]();
[Link]("Both threads have completed execution.");
}
}
10
Exercise 7: Web Services in [Link]
Develop a simple [Link] Web Service that performs basic arithmetic operations like
addition, subtraction, multiplication, and division.
using System;
using [Link];
[WebService(Namespace = "[Link]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class ArithmeticService : WebService{
public int Add(int a, int b) {
return a + b;
}
public int Subtract(int a, int b){
return a - b;
}
public int Multiply(int a, int b) {
return a * b;
}
public double Divide(int a, int b) {
if (b == 0)
throw new DivideByZeroException("Division by zero is not allowed.");
return (double)a / b;
}
}
11
Exercise 8: Windows Forms Application
Create a Windows Forms application with different controls such as buttons, text boxes, and
labels. Implement event handlers for user interactions.
using System;
using [Link];
public class MyForm : Form
{
private TextBox txtInput;
private Button btnClickMe;
private Label lblMessage;
public MyForm()
{
[Link] = "Windows Forms Example";
[Link] = 400;
[Link] = 300;
lblMessage = new Label()
{
Text = "Enter your name:",
Location = new [Link](20, 20),
AutoSize = true
};
[Link](lblMessage);
txtInput = new TextBox()
{
Location = new [Link](20, 50),
Width = 200
};
[Link](txtInput);
btnClickMe = new Button()
{
Text = "Click Me",
Location = new [Link](20, 90)
};
[Link] += new EventHandler(this.BtnClickMe_Click);
[Link](btnClickMe);
}
private void BtnClickMe_Click(object sender, EventArgs e)
12
{
[Link]("Hello, " + [Link] + "!", "Greeting");
}
static void Main()
{
[Link]();
[Link](new MyForm());
}
}
13
Exercise 9: LINQ to SQL
Create a C# program that uses LINQ to query a list of students and find those who are over 20.
using System;
using [Link];
using [Link];
class Student{
public string Name { get; set; }
public int Age { get; set; }
}
class Program{
static void Main(){
List<Student> students = new List<Student>{
new Student { Name = "Alice", Age = 22 },
new Student { Name = "Bob", Age = 19 },
new Student { Name = "Charlie", Age = 25 },
new Student { Name = "David", Age = 18 },
new Student { Name = "Emma", Age = 21 }
};
var result = from student in students
where [Link] > 20
select student;
[Link]("Students older than 20:");
foreach (var student in result) {
[Link]($"Name: {[Link]}, Age: {[Link]}");
}
}
}
14
Exercise 10: Exception Handling in C#
Write a C# program to demonstrate exception handling by creating custom exceptions and
handling runtime errors.
using System;
// Custom Exception Class
class CustomException : Exception
{
public CustomException(string message) : base(message) { }
}
class Program
{
static void CheckNumber(int number)
{
if (number < 0)
{
throw new CustomException("Negative numbers are not allowed.");
}
else if (number == 0)
{
throw new ArgumentException("Zero is not a valid input.");
}
else
{
[Link]("Valid number: " + number);
}
}
static void Main(){
try{
[Link]("Enter a number: ");
int num = [Link]([Link]());
CheckNumber(num);
}
catch (CustomException ex){
[Link]("Custom Exception: " + [Link]);
}
catch (ArgumentException ex){
[Link]("Argument Exception: " + [Link]);
}
catch (FormatException)
{
15
[Link]("Invalid input! Please enter a valid number.");
}
catch (Exception ex){
[Link]("General Exception: " + [Link]);
}
finally
{
[Link]("Execution completed.");
}
}
}
16