0% found this document useful (0 votes)
1K views

C# Practical Solution

This document contains a certificate certifying that Ms. Bhavana R. Kadge of T.Y.B.Sc. IT with Roll No. 18 has successfully completed the practical of C# in semester VI under the supervision of the lecturer in charge of IT at Deccan Education Society’s Kirti M. Doongurse College in Mumbai during the academic year 2011-2012. It also includes an index listing 11 practical assignments completed as part of the C# practical including programs on factors, arrays, strings, structures, classes, interfaces and exception handling.

Uploaded by

Komal Nirbhavane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

C# Practical Solution

This document contains a certificate certifying that Ms. Bhavana R. Kadge of T.Y.B.Sc. IT with Roll No. 18 has successfully completed the practical of C# in semester VI under the supervision of the lecturer in charge of IT at Deccan Education Society’s Kirti M. Doongurse College in Mumbai during the academic year 2011-2012. It also includes an index listing 11 practical assignments completed as part of the C# practical including programs on factors, arrays, strings, structures, classes, interfaces and exception handling.

Uploaded by

Komal Nirbhavane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 61

DECCAN EDUCATION SOCIETYS

KIRTI.M.DOONGURSEE COLLEGE,
DADAR
MUMBAI
(NAAC Accreditation A Grade)

Department Of Computer Science & Information Technology


T. Y. B. Sc. I. T.

C#

CERTIFICATE
This is to certify that Ms. BHAVANA.R.KADGE of T.Y.B.Sc. IT With Roll No.
18 has successfully completed the practical of C# in semester VI under my
supervision in this college during the year
2011-2012.

Lecturer in Charge
I.T.

H.O.D
Department of Comp. Sci. &

INDEX
Sr.No

Title

1)

Practical No.1
(FACTORIAL,REVERSE,SUM,FIB
ONACCI SERIES)
Practical No.2
(AVERAGE,SWITCH
CASE,SWAP,SORT)
Practical No.3
(MUL 2D ARRAY,ADD &MUL
3X3 MATRICES,SORT 1D
ARRAY,ARRAYLIST CLASS)
Practical No.4
(STRING CLASS &
STRINGBUILDER CLASS)
Practical No.5
(STRUCTURE AND
ENUMERATION)
Practical No.6
(CONSTRUCTOR AND
OVERLOADING)
Practical No.7
(ACCOUNT AND SHAPE)
Practical No.8
(INTERFACE)
Practical No.9
(OVERLOAD +OPERATOR)
Practical No.10
(EXCEPTION HANDLING)
Practical No.11
(DELEGATE AND EVENT
HANDLING)

2)

3)

4)

5)

6)

7)
8)
9)
10)
11)

Date

Sign

PRACTICAL NO:1
1.1)Write a C# program to calculate a Factorial of a
Number.
Solution
using System;
class Factorial
{
public static void Main()
{
int n;
Console.WriteLine("Enter the Number ");
n=int.Parse(Console.ReadLine());
int Fact = 1;

for (int i = n; i > 1; i--)


{
Fact = Fact * i;
}
Console.WriteLine("Factorial is " +Fact);
Console.ReadKey();
}
}

OUTPUT :

1.2)Write a C# program to Reverse a 4 Digit integer


Number.
Solution
using System;

class Reverse
{
public static void Main()
{
int n, temp, rn = 0;
Console.WriteLine("Enter the Number ");
n = int.Parse(Console.ReadLine());
temp = n;
while(n != 0)
{
rn =(rn*10)+(n%10);
n = n / 10;
}
Console.WriteLine("The Reverse Number of " + temp
+ " is " + rn);
}
}

OUTPUT :

1.3)Write a C# program to print first 100 Fibonacci


Series.
Solution

using System;
class Fibonacci
{
public static void Main()
{
int n1, n2, Fib;
Console.WriteLine("The Fibonacci Series is...");
n1=0;
n2=1;
Fib=n1+n2;
Console.Write(n1+" "+n2);
for(int i=1; i<=98; i++)
{
Console.Write(" "+ Fib);
n1=n2;
n2=Fib;
Fib=n1+n2;
}
Console.ReadKey();
}
}

OUTPUT :

1.4)Write a C# program to print Sum of Digits of a


Number.
Solution
using System;

class SumDigits
{
public static void Main()
{
int n, temp, sum = 0;
Console.WriteLine("Enter the Number ");
n = int.Parse(Console.ReadLine());
temp = n;
while(n > 0)
{
sum = sum + n % 10;
n = n / 10;
}
Console.WriteLine("The Sum of Digits of Number "
+ temp + " is " + sum);
}
}

OUTPUT :

PRACTICAL NO:2
2.1 Write a C# program to calculate the Average of 5
Numbers.
Solution

using System;
class SwitchOperation
{
public static void Mian()
{
int a, b, c, d, e;
Console.WriteLine("Enter the Numbers...");
a = double.Parse(Console.ReadLine());
b = double.Parse(Console.ReadLine());
c = double.Parse(Console.ReadLine());
d = double.Parse(Console.ReadLine());
e = double.Parse(Console.ReadLine());
double avg = (a + b + c + d + e) / 5;
Console.WriteLine("Average of 5 Numbers is " +
avg);
Console.ReadKey();
}
}

OUTPUT :

2.2

Write a C# program to demonstrate the use of Switch Case.

Solution
using System;
class SwitchOperation
{
public static void Main()
{

double a, b, c;
Console.WriteLine("Enter the two Operands...");
a = double.Parse(Console.ReadLine());
b = double.Parse(Console.ReadLine());
Console.WriteLine("\n Choose the Operation...");
Console.WriteLine("1. ADDITION");
Console.WriteLine("2. SUBTRACTION");
Console.WriteLine("3. MULTIPLICATION");
Console.WriteLine("4. DIVISION");
Console.WriteLine("\nEnter Your Choice...");
int ch = int.Parse(Console.ReadLine());
switch (ch)
{
case 1:
c = a + b;
Console.WriteLine("\nThe Additon is " + c);
break;
case 2:
c = a - b;
Console.WriteLine("\nThe Subtraction is " + c);
break;
case 3:
c = a * b;
Console.WriteLine("\nThe Multiplication is " + c);
break;
case 4:
c = a / b;
Console.WriteLine("\nThe Division is " + c);
break;
default:
Console.WriteLine("Please Enter the Right Choice...");
break;
}
Console.ReadKey();

}
}
OUTPUT :

2.3 Write a C# program to swap the values of 2 variables.


Solution

using System ;
class SwapNumber{
static void Main(){

string str;
Console.Write("enter value in A:");
str= Console.ReadLine();
int A = int.Parse(str);
Console.Write("enter value in B:");
str= Console.ReadLine();
int B = int.Parse(str);
Console.Clear();
Console.WriteLine("value in A: "+A);
Console.WriteLine("value in B: "+B);
int temp;
temp =A;
A=B;
B=temp;
Console.WriteLine();
Console.WriteLine("After swapping values are");
Console.WriteLine("Values in A: "+A);
Console.WriteLine("Values in B: "+B);
Console.ReadKey();
}
}

OUTPUT:

2.4 Write a C# program to sort 5 numbers.


Solution
using System;
using System.Collections;

class Example
{
public static void Main()
{
ArrayList num= new ArrayList();
num.Add(20);
num.Add(26);
num.Add(80);
num.Add(49);
num.Add(78);
num.Sort();
/*

for (int i = 0; i < num.Count; i++)


{

Console.Write("Enter a Number: ");


string str = Console.ReadLine();
num[i] = int.Parse(str);
num.Sort();
}*/
Console.WriteLine();
Console.WriteLine("sorted numbers");
for (int i = 0; i < num.Count; i++)
{

Console.WriteLine( num[i]);
}
}

}
OUTPUT:

PRACTICAL NO: 3
3.1 Write a C# program to create a multiplication table using 2-D
array.
Solution

using System;

class ExampleMul
{
public static void Main()
{
int[ , ]a=new int[9,10];
int i,j;
Console.WriteLine("Mul Table 2 to 10");
Console.WriteLine();
for(i=2;i<10;i++)
{
for(j=1;j<=10;j++)
{
Console.Write((i*j)+" ");
}
Console.WriteLine();
}
}
}

OUTPUT:

3.2

Write a C# program to add and multiply 2 3X 3 matrices

Solution
using System;
class TwoDMulti
{
public static void Main()
{
int [,]a=new int[3,3];

int [,]b=new int[3,3];


int [,]c=new int[3,3];
//Initializa and Assigning the Values to Array
Console.WriteLine("Enter the Elements of Array a");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
a[i,j]=int.Parse(Console.ReadLine());
}
}
Console.WriteLine("Enter the Elements of Array b");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
b[i,j]=int.Parse(Console.ReadLine());
}
}
//Perform Addition Operation
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
c[i,j] = a[i,j] + b[i,j];
}
}
//Display The Matrix
Console.WriteLine("The 1st Matrix is...");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write(a[i,j] + " ");
}
Console.WriteLine("\n");
}

Console.WriteLine("The 2nd Matrix is...");


for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write(b[i,j] + " ");
}
Console.WriteLine("\n");
}
//Dsiaply the Additon
Console.WriteLine("The Addition Of Matrices is...");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write(c[i,j] + " ");
}
Console.WriteLine("\n");
}
//Set the Values of Array to zero
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
c[i,j]=0;
}
}
//Perform Multiplication Operation
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
for (int k = 0; k < 3; k++)
{
c[i,j] = c[i,j] + a[i,k] * b[k,j];
}
}
}
//Disaplay Multiplication

Console.WriteLine("The Multiplication Of Matrices is...");


for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write(c[i,j] + " ");
}
Console.WriteLine("\n");
}
Console.ReadKey();
}
}
OUTPUT :

3.3 Write a C# program to sort a One-Dimensional array of


numbers
Solution
using System;
class Sorting
{
public static void Main()
{
int []a=new int[5];
int temp;
Console.WriteLine("Enter the Numbers in Array");
for(int i=0; i<5; i++)
{
a[i]=int.Parse(Console.ReadLine());
}
for(int i=0; i<4; i++)
{
for(int j=(i+1); j<5; j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
Console.WriteLine("The Sorted Array is ...\n");

for(int i=0; i<5; i++)


{
Console.Write(a[i] +" ");
}
}
}
OUTPUT :

3.4 Write a C# program to create an array using ArrayList


class. Sort and reverse the array.
Solution
using System;
class SortReverse
{
public static void Main()
{
int []a=new int[5];
Console.WriteLine("Enter the Numbers in Array");
for(int i=0; i<5; i++)
{
a[i]=int.Parse(Console.ReadLine());
}
Console.WriteLine("The Sorted Array is ...\n");
Array.Sort(a);
foreach (int i in a)
{
Console.Write(i + " ");
}
Console.WriteLine("\n\nThe Reverse Array is ...\n");
Array.Reverse(a);
foreach (int i in a)
{
Console.Write(i + " ");
}
}
}

OUTPUT :

PRACTICAL NO:4
4.1

Write a C# program to demonstrate the functions of System.String class.

Solution

using System;
using System.Text;
using System.Collections.Generic;
namespace StingOperation
{
class StringMethods
{
public static void Main()
{
//Assigning String Literals
string s1 = "Lean";
//Inserting String
string s2 = s1.Insert(3, "r");
string s3 = s2.Insert(4, "er");
//Display the Original String
Console.Write("Sting1 = ");
for (int i = 0; i < s1.Length; i++)
Console.Write(s1[i]);
Console.WriteLine("\n");
//Display the Modified String
Console.Write("Sting3 = ");
for (int i = 0; i < s3.Length; i++)
Console.Write(s3[i]);
Console.WriteLine("\n");
//Copying the String
string s4 = string.Copy(s3);
//Display the Copied String
Console.WriteLine("The Copied String4 from String3 = {0}", s4);
Console.WriteLine("\n");
//Assigning String through Keyboard
string s5 = "";
Console.Write("Enter a String5");

s5 = Console.ReadLine();
Console.WriteLine("\n");
string s6 = "";
Console.Write("Enter a String6");
s6 = Console.ReadLine();
//Concate the two Strings
string s7 = string.Concat(s5, s6);
Console.WriteLine("\n");
//Display th Concated String
Console.WriteLine("The Concated String7 {0}", s7);
Console.WriteLine("\n");
//Convert integer object to String
int num1 = 123;
string s8 = num1.ToString();
//Display the ToString method Outout
Console.WriteLine("The number to String8 = {0}", s8);
Console.WriteLine("\n");
//Display the Length of the String
Console.WriteLine("The Length of String7 = {0}", s7.Length);
Console.WriteLine("\n");
//Check if String ends with a set of Characters
Console.WriteLine("String s7 : {0}\n Ends with IDE? : {1}", s7,
s7.EndsWith("IDE"));
Console.WriteLine("Ends with Studio? : {0}", s7.EndsWith("Studio"));
Console.WriteLine("\n");
//Returning the Index of SubString
int num2 = s7.IndexOf("a") + 1;
Console.WriteLine("The first time character 'a' occurred in String7 at Position :
{0}", num2);
Console.WriteLine("\n");
//Comparing the Two Strings
int num3 = string.Compare(s5, s6);

switch (num3)
{
case 0:
Console.WriteLine("Two Strings are Equal");
break;
case 1:
Console.WriteLine("String5 is Gerater than String6");
break;
default:
Console.WriteLine("String5 is Less than String6");
break;
}
}
}
}

OUTPUT :

4.2 Write a C# program to create a mutable string and


demonstrate the functions of StringBuilder class.
Solution
using System;
using System.Text;
using System.Collections.Generic;
class StringBuilderMethods
{
public static void Main()
{
string s1, s2;
Console.Write("Please Enter a String1 ");
s1 = Console.ReadLine();
Console.Write("\nPlease Enter a String2 ");
s2 = Console.ReadLine();
StringBuilder sb1 = new StringBuilder(s1, 4);
StringBuilder sb2 = new StringBuilder(s2, 4);
int cap = sb1.EnsureCapacity(55);
Console.WriteLine("\nString1 Appended to : ");
sb1.Append(" This is a Class Test");
Console.WriteLine(sb1);
Console.WriteLine("\nString2 Inserted with : ");
sb2.Insert(2, " String builder");
Console.WriteLine(sb2);
Console.WriteLine("\nSecond Character of String2 Removed
");
sb2.Remove(2, 4);

sb1.Replace(' ', '*');


Console.WriteLine("\nSpaces removed from String1 ");
Console.WriteLine(sb1);
Console.WriteLine("\nLength of String1 = {0}",
sb1.Length.ToString());
Console.WriteLine("\nLength of String2 = {0}",
sb2.Length.ToString());
}
}
OUTPUT :

PRACTICAL NO:5
5.1

Write a C# program to create a structure Rectangle. It declares two variables to define


the dimension of a rectangle. It has two functions to calculate the area of the rectangle and
perimeter of a rectangle.
Solution

using System;
struct Rectangle
{
public int Length, Breadth;
public Rectangle(int l, int b)
{
Length = l;
Breadth = b;
}
public int Area()
{
return (Length * Breadth);
}
public int Perimeter()
{
return (2 * (Length + Breadth));
}
public void Display()
{
Console.WriteLine("Area of Rectangle " + Area());
Console.WriteLine("Perimeter of Rectangle " + Perimeter());
}
}

class StructRectangle
{
public static void Main()
{
int l, b;
Console.WriteLine("Enter the Length of Rectangle ");

l = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the Breadth of Rectangle ");
b = int.Parse(Console.ReadLine());
Rectangle R1 = new Rectangle(l, b);
R1.Display();
}
}

OUTPUT :

5.2

Write a C# program to create a class Area. It declares an enumeration Shape. It has a


function AreaShape to calculate the area of circle, rectangle, square, and triangle.
Solution

using System;
class Area
{
public enum Shape
{
Circle,
Rectangle,
Square,
Triangle
}
public void AreaShape(int x, Shape shape)
{
double area;
switch(shape)
{
case Shape.Circle :
area = Math.PI * x * x;
Console.WriteLine("Area of Circle = " + area);
break;
case Shape.Rectangle :
area = x * x;
Console.WriteLine("Area of Rectangle = " + area);
break;
case Shape.Square :
area = x * x;
Console.WriteLine("Area of Square = "+ area);
break;
case Shape.Triangle :
area=(0.5*x*x);
Console.WriteLine("Area of Triangle = " + area);
break;
default :
Console.WriteLine("Invalid Input");
break;
}

}
}
class EnumTest
{
public static void Main()
{
Area a1=new Area();
a1.AreaShape(10, Area.Shape.Circle);
a1.AreaShape(15, Area.Shape.Rectangle);
a1.AreaShape(15, Area.Shape.Square);
a1.AreaShape(10, Area.Shape.Triangle);
a1.AreaShape(15, (Area.Shape) 1);
}
}

OUTPUT :

PRACTICAL NO:6
6.1 Write a C# program to create a class Box. Initialize the variables
using 3 different types of constructors and calculate volume of a box.
Solution
using System;
public class Box
{
//Declaring Variables
public double Height;
public double Base;
public double Depth;
//Default Constructor
public Box()
{
Height = 10;
Base = 15;
Depth = 12;
}
//Parameterised Constructor
public Box(double h, double b, double d)
{
Height = h;
Base = b;
Depth = d;
}
//Copy Constructor
public Box(Box b)
{
Height = b.Height;
Base = b.Base;
Depth = b.Depth;
}

//Declaring Method
public double Volume_Box()
{
return (Height * Base * Depth);
}
}
class ConstructorOverload
{
public static void Main()
{
double area;
//Calling Default Constructor
Box B1 = new Box();
area = B1.Volume_Box();
Console.WriteLine("Volume of Box using Default Constructor...
{0}",area);
//Calling Parameterised Constructor
Box B2 = new Box(10, 10, 10);
area=B2.Volume_Box();
Console.WriteLine("\nVolume of Box using Parameterised
Constructor... {0}",area);
double a, b, c;
Console.WriteLine("\nEnter the Parameters...\n");
a = double.Parse(Console.ReadLine());
b = double.Parse(Console.ReadLine());
c = double.Parse(Console.ReadLine());
Box B3=new Box(a, b, c);

area=B3.Volume_Box();
Console.WriteLine("\nVolume of Box using Parameterised
Constructor by passing values... {0}",area);
//Calling Copy Constructor
Box B4 = new Box(B2);
area = B4.Volume_Box();
Console.WriteLine("\nVolume of Box using Copy Constructor of
object B2... {0}",area);
}
}

OUTPUT :

6.2 Write a C# program to create a class Area1. It has 3 functions to


calculate the area of square, circle and triangle with the same name
but different signatures.
Solution
using System;
public class AreaShape
{
public double area(double r)
{
return(Math.PI*r*r);
}
public double area(int s)
{
return(s*s);
}
public double area(double h, double b)
{
return(0.5*b*h);
}
}
class MethodOverload
{
public static void Main()
{
double a;
AreaShape A1 = new AreaShape();
a = A1.area(10.0);
Console.WriteLine("Area of Circle... {0}", a);
a = A1.area(20);
Console.WriteLine("\nArea of Circl,,, {0}", a);

a = A1.area(10.0, 20.0);
Console.WriteLine("\nArea of Triangle... {0}", a);
}

OUTPUT :

PRACTICAL NO:7
1)Create a class Account that stores customer name,
account number and type of the account. From this
derive the classes Cur-Acct and Sav-Acct to make them
more specific to their requirements. Include the
necessary member methods in order to accomplish the
following tasks :
(a)
Accept deposit form a customer and update the
balance.
(b)
Display the balance.
(c)
Compute and deposit interest.
(d)
Permit withdrawal and update the balance.
(e)
Check for the minimum balance, impose penalty,
if necessary and update the balance.
Solution
using System;
using A= System.Console;
class Acc
{
protected string c_name;
protected int accno;
public string type;
public void set(string s1,int a,string s2)
{
c_name = s1;
accno = a;
type = s2;
}
public void display()
{
A.WriteLine("Customer Name : " + c_name);
A.WriteLine("Account Number : "+ accno);
A.WriteLine("Type of Account : " + type);

}
}
class sav_acc : Acc
{
double bal2 = 5000;
public void deposit()
{
A.WriteLine("Current Balance : Rs."+ bal2);
A.WriteLine("Enter an amount to deposit");
int x = int.Parse(Console.ReadLine());
bal2 = bal2+x;
A.WriteLine("Rs."+x+"deposited");
A.WriteLine("Current Balance : Rs."+ bal2);
}
public void interest()
{
bal2 =bal2 + (bal2*0.04);
A.WriteLine("Balance after calculating interest:
Rs."+ bal2);
}
public void withdraw()
{
A.WriteLine("Enter an amount to be withdrawn");
int y = int.Parse(Console.ReadLine());
bal2 = bal2 - y;
if(bal2 < 500)
{
A.WriteLine("Insufficient Balance.......Can Not
Proceed");
bal2 =bal2+y;
}
A.WriteLine("Rs."+y+"withdrawn");
A.WriteLine("Current Balance : Rs."+ bal2);

}
}
class cur_acc : Acc
{
double bal1 = 5000;
public void deposit()
{
A.WriteLine("Current Balance : Rs."+ bal1);
A.WriteLine("Enter an amount to deposit");
int x1 = int.Parse(Console.ReadLine());
bal1 = bal1+x1;
A.WriteLine("Current Balance : Rs."+ bal1);
}
public void penalty()
{
A.WriteLine("Enter an amount to be withdrawn");
int y = int.Parse(Console.ReadLine());
bal1 = bal1 - y;
if(bal1 < 1000)
{
bal1 = bal1 - 500;
A.WriteLine("Insufficient Balance");
A.WriteLine("Penalty of Rs.500 has been
imposed on your account ");
}
else
{
A.WriteLine("Rs." + y + "withdrawn");
}
A.WriteLine("Current Balance : Rs."+ bal1);
}
}
class Test

{
public static void Main()
{
Acc ac1 = new Acc();
ac1.set("Manasi",321456,"Savings");
sav_acc sa1 = new sav_acc();
cur_acc ca1 = new cur_acc();
if(ac1.type =="Savings")
{
ac1.display();
A.WriteLine("Rate of Interest : "+" 4%");
sa1.deposit();
sa1.withdraw();
sa1.interest();
}
else
{
ac1.display();
ca1.deposit();
ca1.penalty();
}
}
}

Output :

2) Create a base class called Shape. Use this class to store two
double type values that could be used to compute the area of
figures.
(a)
Derive two specialized classes called Triangle and
Rectangle from base Shape.
(b)
Add to the base class, method Set to initialized base class
data members and another method Area to compute and
display the area of figures.
(c)
Make Area as a virtual method and redefine this method
suitable in the derived class.
(d)
Using these classes, design a program that will accept
dimensions of a triangle or rectangle interactively and display
the area.

Solution
using System;
using A=System.Console;
class Shape
{
protected double x,y;
public void set(double a, double b)
{
x=a;
y=b;
}
public virtual void Area()
{}
}
class Triangle : Shape
{
public override void Area()

{
A.WriteLine("Base:"+" "+x+" "+"Height:"+" "+y);
double c = (0.5*x*y);
A.WriteLine("Area of Triangle : "+c);
}
}
class Rectangle : Shape
{
public override void Area()
{
A.WriteLine("Length:"+" "+x+" "+"Breadth:"+" "+y);
double c= (x*y);
A.WriteLine("Area of Rectangle : "+c);
}
}
class Test
{
public static void Main()
{
Triangle t1 = new Triangle();
t1.set(5,4);
Rectangle r1 = new Rectangle();
r1.set(3,2);
t1.Area();
r1.Area();
}
}

Output :

PRACTICAL NO:8
Write a C# program to create an interface Area. It contains a function
area1 to calculate the area. Implement the interface in class Circle to calculate
the area of a circle and class Triangle to calculate the area of a triangle.
Solution

using System;
interface ComputeArea
{
double area1(double x);
}
class Circle : ComputeArea
{
public double area1(double x)
{
return (Math.PI * x * x);
}
}
class Triangle : ComputeArea
{
public double area1(double x)
{
return (0.5 * x * x);
}
}
class InterfaceTest
{
public static void Main()
{
Circle C1=new Circle();
Triangle T1=new Triangle();
ComputeArea A;

A = C1 as ComputeArea;
Console.WriteLine("Area Of Cicrle = " + A.area1(10.0));
A = T1 as ComputeArea;
Console.WriteLine("Area Of Triangle = " + A.area1(10.0));
}
}

OUTPUT :

PRACTICAL NO:9
Write a C# program to overload the + Operator to add two Complex Numbers.
Solution

using System;
class Complex
{
double x;
double y;
public Complex()
{
}
public Complex(double real, double imag)
{
x = real;
y = imag;
}
public static Complex operator +(Complex c1, Complex c2)
{
Complex c3 = new Complex();
c3.x = c1.x + c2.x;
c3.y = c1.y + c2.y;
return (c3);
}
public void Display()
{
Console.WriteLine(x+ " +"+ y+ "j");
}
}
class OperatorOverload
{
public static void Main()
{
Complex a,b,c;
double x, y;
Console.WriteLine("Enter the 1st Complex Number Parameters... ");
x = double.Parse(Console.ReadLine());
y = double.Parse(Console.ReadLine());

a = new Complex(x, y);


Console.WriteLine("Enter the 2nd Complex Number Parameters... ");
x = double.Parse(Console.ReadLine());
y = double.Parse(Console.ReadLine());
b = new Complex(x, y);
c = a+b;
Console.WriteLine("The 1st Complex Number...");
Console.Write("a= ");
a.Display();
Console.WriteLine("The 2nd Complex Number...");
Console.Write("b= ");
b.Display();
Console.WriteLine("The Addition of Complex Numbers...");
Console.Write("c =");
c.Display();
}
}
OUTPUT :

PRACTICAL NO:10
Write a C# program to demonstrate the concept of Exception Handling.
Solution
using System;
class ExceptionHandle
{
public static void Main()
{
int [] p={5,10};
int q=5;
int r,s;
try
{
r = p[2] / (q - p[1]);
}
catch (ArithmeticException)
{
Console.WriteLine("Division By Zero");
}
catch (IndexOutOfRangeException)
{
Console.WriteLine("Array Index Error");
}
catch (ArrayTypeMismatchException)
{
Console.WriteLine("Wrong Data Type");
}
s = p[1] / p[0];
Console.WriteLine("s = " + s);
}
}

OUTPUT :

PRACTICAL NO:11
11.1 Write a C# program to demonstrate the concept of
delegate int MyDelegtae(int x, int y). Create a class MyClass.
It has 2 methods Add and Multiply that match the signature of
the delegate.
Solution
public delegate int MyDelegate(int x, int y);
public class DelegateMethod
{
public static int Add(int x, int y)
{
return x + y;
}
public static int Multiply(int x, int y)
{
return x * y;
}
public static void Main()
{
MyDelegate del1 = new MyDelegate(Add);
int addResult = del1(5, 10);
System.Console.WriteLine(addResult);
MyDelegate del2 = new MyDelegate(Multiply);
int MultiplyResult = del2(5, 10);
System.Console.WriteLine(MultiplyResult);
}
}

OUTPUT :

11.2 Write a C# program to demonstrate the concept of Event


Handling.
Solution
using System;
public delegate void Edelegate(string str);
public class EventClass
{
public event Edelegate Status;
public void TriggerEvent()
{
if (Status != null)
Status("Event Triggered.");
}
}
class EventTest
{
public static void Main()
{
EventClass ec = new EventClass();
EventTest et = new EventTest();
ec.Status += new Edelegate(et.EventCatch);
ec.TriggerEvent();
}
public void EventCatch(string str)
{
Console.WriteLine(str);
}
}

OUTPUT :

You might also like