C# Practical Solution
C# Practical Solution
KIRTI.M.DOONGURSEE COLLEGE,
DADAR
MUMBAI
(NAAC Accreditation A Grade)
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;
OUTPUT :
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 :
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 :
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
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 :
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:
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();
/*
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
Solution
using System;
class TwoDMulti
{
public static void Main()
{
int [,]a=new int[3,3];
OUTPUT :
PRACTICAL NO:4
4.1
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 :
PRACTICAL NO:5
5.1
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
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 :
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());
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 :
OUTPUT :