QUESTION 1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication4
public partial class Form1 : Form
private string f = "subject.txt";
public Form1()
InitializeComponent();
private void button1_Click(object sender, EventArgs e)
if(!File.Exists(f)){
File.Create(f).Close();
}
private void button2_Click(object sender, EventArgs e)
using (StreamWriter write = new StreamWriter(f,true)){
write.WriteLine(textBox1.Text);
private void button3_Click(object sender, EventArgs e)
using (StreamReader read = new StreamReader(f))
string line;
while ((line = read.ReadLine()) != null) {
listBox1.Items.Add(line);
}
QUESTION 2
2a)
using System;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] numbers = {10,20,30,40,50,60,70,80,90,100};
Console.WriteLine("Array: " + string.Join(",", numbers));
Console.Write("Enter a number to delete: ");
int numToDelete = int.Parse(Console.ReadLine());
numbers = numbers.Where(x => x != numToDelete).ToArray();
Console.WriteLine("Updated Array: " + string.Join(", ", numbers));
Console.ReadKey();
}
}
}
2b)
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// Abstract Shape class
public abstract class Shape
{
public abstract double CalculateArea();
public abstract double CalculatePerimeter();
}
// Rectangle class
public class Rectangle : Shape
{
public double Width { get; set; }
public double Height { get; set; }
public Rectangle(double width, double height)
{
Width = width;
Height = height;
}
public override double CalculateArea()
{
return Width * Height;
}
public override double CalculatePerimeter()
{
return 2 * (Width + Height);
}
}
// Square class
public class Square : Rectangle
{
public Square(double sideLength) : base(sideLength, sideLength)
{
}
}
// Circle class
public class Circle : Shape
{
public double Radius { get; set; }
public Circle(double radius)
{
Radius = radius;
}
public override double CalculateArea()
{
return Math.PI * Math.Pow(Radius, 2);
}
public override double CalculatePerimeter()
{
return 2 * Math.PI * Radius;
}
}
class Program
{
static void Main()
{
Rectangle rectangle = new Rectangle(4, 5);
Console.WriteLine("Rectangle Area: " + rectangle.CalculateArea() );
Console.WriteLine("Rectangle Perimeter:
"+rectangle.CalculatePerimeter());
Square square = new Square(4);
Console.WriteLine("Square Area: "+ square.CalculateArea());
Console.WriteLine("Square Perimeter: "+ square.CalculatePerimeter());
Circle circle = new Circle(3);
Console.WriteLine("Circle Area: "+ circle.CalculateArea());
Console.WriteLine("Circle Circumference: " +
circle.CalculatePerimeter());
}
}
QUESTION 5
a)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static void Main()
{
try
{
Console.Write("Enter first number: ");
int a = int.Parse(Console.ReadLine());
Console.Write("Enter second number: ");
int b = int.Parse(Console.ReadLine());
Console.WriteLine("Addition: "+ a + b);
Console.WriteLine("Division: "+ a/b);
}
catch (DivideByZeroException) {
Console.WriteLine("Division by zero is not allowed.");
}
Console.ReadKey();
}
}
}