Tribhuvan University
Institute Of Science and Technology
Hetauda City College, Hetauda
LAB Assignment
NET CENTRIC COMPUTING
Submitted By: Submitted to:
Ayush Kumar Acharya Mr. Mukunda Dhungana
[Link]. CSIT 6TH Semester Lecturer (Department of IT)
Signature:
1. Write a C# program to calculate the sum of two numbers using a class and
constructor.
using System;
namespace AddTwoDigitsConstructor
{
class Addition
{
int x, y;
public Addition(int a, int b)
{
this.x = a;
this.y = b;
}
public int Sum()
{
return x + y;
}
}
class Program
{
static void Main(string[] args)
{
Addition obj = new Addition(30, 20);
[Link]($"The sum is : {[Link]()}");
[Link]();
}
}
}
OUTPUT
1
2. Write a C# program to reverse an array and display both the original and
reversed arrays.
using System;
namespace LAB
{
internal class Program
{
static void Main(string[] args)
{
// Original array
int[] arr = { 14, 22, 13, 54, 5 };
[Link]("Original Array:");
foreach (int item in arr)
{
[Link](item + " ");
}
// Reverse the array
[Link](arr);
[Link]("\n\nReversed Array:");
foreach (int item in arr)
{
[Link](item + " ");
}
[Link](); // keep console open
}
}
}
OUTPUT
2
3. Write a C# program to store and display student names using a class and
indexer.
using System;
namespace LAB
{
class StudentNames
{
private string[] names = new string[5]; // Fixed size array
// Indexer
public string this[int index]
{
get { return names[index]; }
set { names[index] = value; }
}
}
internal class Program
{
static void Main(string[] args)
{
StudentNames students = new StudentNames();
// Store names using indexer
students[0] = "Sandesh";
students[1] = "Ashesh";
students[2] = "Prakash";
students[3] = "Pradip";
students[4] = "Advik";
[Link]("--- Student Names ---");
for (int i = 0; i < 5; i++)
{
[Link]($"Student {i + 1}: {students[i]}");
}
3
[Link](); // Keep console open
}
}
}
OUTPUT
4. Write a C# program to create a student class with automatic properties and
display student details
using System;
namespace StudentApp
{
class Student
{
// Automatic properties
public int StudentId { get; set; }
public string Name { get; set; }
}
class Program
{
static void Main(string[] args)
{
// Create and initialize student object
Student s1 = new Student
{
StudentId = 10,
Name = "Ayush"
};
4
// Display student details
[Link]("--- Student Details ---");
[Link]("Student ID : " + [Link]);
[Link]("Name : " + [Link]);
[Link]();
}
}
}
OUTPUT
5. Write a C# program to demonstrate runtime polymorphism using virtual and
override methods.
using System;
namespace LAB
{
// Base class
class Animal
{
// Virtual method
public virtual void MakeSound()
{
[Link]("Animal makes a sound");
}
}
5
// Derived class 1
class Dog : Animal
{
// Override the virtual method
public override void MakeSound()
{
[Link]("Dog barks");
}
}
// Derived class 2
class Cat : Animal
{
// Override the virtual method
public override void MakeSound()
{
[Link]("Cat meows");
}
}
internal class Program
{
static void Main(string[] args)
{
Animal myAnimal = new Animal();
Animal myDog = new Dog();
Animal myCat = new Cat();
6
[Link]("--- Polymorphism using Virtual Methods ---");
[Link](); // Calls base class method
[Link](); // Calls derived class method
[Link](); // Calls derived class method
[Link]();
}
}
}
OUTPUT
7
6. Write a C# program to create a Calculator class with methods to perform
addition, subtraction, multiplication, and division of two numbers.
using System;
namespace LAB
{
// Calculator class
class Calculator
{
public int Add(int a, int b) => a + b;
public int Subtract(int a, int b) => a - b;
public int Multiply(int a, int b) => a * b;
public double Divide(double a, double b)
{
if (b == 0)
{
[Link]("Division by zero is not allowed!");
return 0;
}
return a / b;
}
}
internal class Program
{
static void Main(string[] args)
{
Calculator calc = new Calculator();
int num1 = 20;
int num2 = 5;
[Link]("--- OOP Calculator ---");
8
[Link]($"{num1} + {num2} = {[Link](num1, num2)}");
[Link]($"{num1} - {num2} = {[Link](num1, num2)}");
[Link]($"{num1} * {num2} = {[Link](num1,
num2)}");
[Link]($"{num1} / {num2} = {[Link](num1,
num2):F2}");
// formatted to 2 decimals
[Link]();
}
}
}
OUTPUT
9
7. Write a C# program to create a Rectangle class that implements two
interfaces for Length and Width and calculates the area.
using System;
namespace LAB
{
// First interface for length
interface ILength
{
double Length { get; set; }
}
// Second interface for width
interface IWidth
{
double Width { get; set; }
}
// Class that inherits both interfaces
class Rectangle : ILength, IWidth
{
public double Length { get; set; }
public double Width { get; set; }
public double GetArea()
{
return Length * Width;
}
}
internal class Program
{
static void Main(string[] args)
{
Rectangle rect = new Rectangle
{
10
Length = 5,
Width = 10
};
[Link]($"Length: {[Link]}");
[Link]($"Width: {[Link]}");
[Link]($"Area of Rectangle: {[Link]()}");
[Link]();
}
}
}
OUTPUT
11