Sie sind auf Seite 1von 30

BRANCHING AND LOOPING

EX.NO:1

Date :

AIM:
To write a C# program using Branching and Looping statements.

ALGORITHM:
Steps:
1. Start the program.
2. Declare the required variables.
3. Using for loop print i values.
4. Check the condition using if.
5. If the condition is true exit the loop and print the result.
6. If the condition is false check the for loop using “continue”.
7. Stop the process.

PROGRAM:

using System;
using System.Collections.Generic;
using System.Text;

namespace branch
{
class Program
{
static void Main(string[] args)
{
int i;
for(i=1;i<10;i++)
{
Console.WriteLine ("i={0}",i);
if(i>=5)
goto loopst;
else
continue ;
loopst:{break;}
} Console.Read();
}
}
}
OUTPUT:

i=1
i=2
i=3
i=4
i=5

RESULT:
Thus the program is compiled and executed successfully and the results are
obtained.
ARRAY, STRINGS & METHODS

EX.NO:2

Date :

AIM:
To write a C# program using Arrays and Strings methods.

ALGORITHM:
Steps:
1. Start the program.
2. Declare the array and string variables.
3. Create a method named getnames()
4. Get the input from user .
5. Create another method named shownames()
6. Using for loop display all names.
7. Create an object.
8. Using the object call all the methods.
9. Print the result.
10. Stop the process.

PROGRAM:

using System;
using System.Collections.Generic;
using System.Text;

namespace array1
{
class Program
{
private string[] names = new string[5];

void getnames()
{
for (int i = 0; i < names.Length; i++)
{
Console.Write("enter name[{0}]:", i);
names[i] = Console.ReadLine();
}
}
void shownames()
{
for (int i = 0; i < names.Length; i++)
{
Console.WriteLine("names[{0}]={1}"
, i, names[i]);
}
}

static void Main(string[] args)


{
Program obj = new Program ();

obj.getnames();
obj.shownames();
Console.Read();
}
}
}

OUTPUT:

Enter name[0]:gopi
Enter name[1]:logu
Enter name[2]:ravi
Enter name[3]:ragu
Enter name[4]:aravindhan

names[0]:gopi
names[1]:logu
names[2]:ravi
names[3]:ragu
names[4]:aravindhan

RESULT:
Thus the program is compiled and executed successfully and the results are
obtained.
STRUCTURES & ENUMERATIONS

EX.NO:3

Date :

AIM:
To write a C# program using Structures and enumerations

ALGORITHM:
Steps:
1. Start the program.
2. Declare the required data types and declare enumeration as months.
3. Declare the constructor.
4. Create an object for class.
5. Create the structure and call the enumeration.
6. Display the result.
7. Stop the process

PROGRAM:
using System;
using System.Collections.Generic;
using System.Text;

namespace mns
{
class ex3
{
int classvar;
int anothervar=20;
enum months { January=31,february=28,March=31 };

public ex3()
{
classvar=28;
}
public static void Main()
{
ex3 e=new ex3();
examplestruct strct=new examplestruct(20);
Console.WriteLine(strct.i);
strct.i=10;
Console.WriteLine(e.classvar);
Console.WriteLine(strct.i);
strct.trial();
months m=(mns.ex3.months)28;
Console.WriteLine("{0}",m);
Console.Read();
}
}
struct examplestruct
{
public int i;
public examplestruct(int j)
{
i=j;
}
public void trial()
{
Console.WriteLine("Inside Trial Method");
}
}
}

OUTPUT:

20
28
10
Inside Trial Method
february

RESULT:
Thus the program is compiled and executed successfully and the results are
obtained.
INHERITANCE

EX.NO:4

Date :

AIM:
To write a C# program using inheritance concepts.

ALGORITHM:
Steps:
1. Start the program.
2. Create a class named room and declare the required variables and
methods.
3. Create a sub class named bed and declare the required variables .
4. Create an object for the sub class.
5. Using the object call the methods and functions.
6. Display the result.
7. Stop the program

PROGRAM:
using System;
using System.Collections.Generic;
using System.Text;

class room
{
public int length;
public int breadth;
public room(int x,int y)
{
length=x;
breadth=y;
}
public int area()
{
return(length*breadth);
}
}
class br:room
{
int height;
public br(int x,int y,int z):base(x,y)
{
height=z;
}
public int volume()
{
return(length*breadth*height);
}
}
class ex4
{
public static void Main()
{
br r1=new br(14,12,10);
int area1=r1.area();
int volume1=r1.volume();
Console.WriteLine("Area : "+area1);
Console.WriteLine("Volume : "+volume1);
}
}

OUTPUT:
Area:168
Volume:1680

RESULT:
Thus the program is compiled and executed successfully and the results are
obtained.
POLYMORPHISM

EX.NO:5

Date :

AIM:
To write a C# program using Polymorphism.

ALGORITHM:
Steps:
1. Start the program.
2. Create a base class named vehicle and declare the variables.
3. Create a derived class named automobile and perform the calculations.
4. Create a class and create an object for the derived class.
5. Call the methods using objects.
6. Display the result.
7. Stop the program.

PROGRAM:
using System;
using System.Collections.Generic;
using System.Text;
namespace polymorphismexample
{
class ex5
{
static void Main(string[] args)
{
Automobile a= new Automobile();
a.PowerSource="100 H.P";
a.IncreaseVelocity(60);
BMW car=new BMW();
car.Color("Black");
Console.WriteLine("press enter to exit...");
Console.ReadLine();
}
}
class vehicle
{
string VehiclePower=" ";
public string PowerSource
{
set
{
VehiclePower=value;
Console.WriteLine("power of engine is set to \"{0}\"
",val ue.ToString());
}
get
{
return VehiclePower;
}
}
public virtual void IncreaseVelocity(int i)
{
Console.WriteLine("The Speed Increases To " "+i.ToString()
+"m.p.h");
}
}
class Automobile:vehicle
{
public override void IncreaseVelocity(int i)
{
if(i>120)
{
i=120;
}
Console.WriteLine("Increasing Its Speed To "+i.ToString()+ " m.p.h");
}
public virtual void Color(string col)
{
col="Blue";
Console.WriteLine("The Color Of BMW is: ");
Console.WriteLine(col);
}
}
class BMW:Automobile
{
public override void Color(string col)
{
col="navy";
Console.WriteLine("The Color Of BMW is: ");
Console.WriteLine(col);
}
}
}

OUTPUT:
Power of engine is set to “100 H.P”
Increasing Its Speed To 60 m.p.h
The color of BMW is:
Navy
Press enter to exit..

RESULT:
Thus the program is compiled and executed successfully and the results
are obtained.
INTERFACES

EX.NO:6

Date :

AIM:
To write a c# program using interfaces.

ALGORITHM:
Steps:

1. Start the program


2. Create two interfaces and declare the required variables.
3. Create the class and implement the two interfaces.
4. Create objects for both class and interfaces.
5. Call the methods using objects.
6. Display the result.
7. Stop the program.

PROGRAM:
Using System;
namespace box
{
interface IEnglishDimension
{
float length();
float width();
}
interface IMetricDimension
{
float length();
float width();
}
class ex06:IEnglishDimension,IMetricDimension
{
float lengthinches;
float widthinches;
public ex06(float length,float width)
{
lengthinches=length;
widthinches=width;
}
float IEnglishDimension.length()
{
return lengthinches;
}
float IEnglishDimension.width()
{
return widthinches;
}
float IMetricDimension.length()
{
return lengthinches*2.54f;
}
float IMetricDimension.width()
{
return widthinches*2.54f;
}
public static void Main()
{
ex06 mybox=new ex06(30.0f,20.0f);
IEnglishDimension ed=(IEnglishDimension)mybox;
IMetricDimension md=(IMetricDimension)mybox;
Console.WriteLine("Length(in):{0}",ed.length());
Console.WriteLine("Width(in) :{0}",ed.width());
Console.WriteLine("Length(cm):{0}",md.length());
Console.WriteLine("Width(cm) :{0}",md.width());
Console.Read();
}
}
}

OUTPUT:

Length(in)=30
Width(in)=20
Length(cm)=76.2
Width(cm)=50.8

RESULT:
Thus the program is compiled and executed successfully and the results are
obtained.
OPERATOR OVERLOADING

EX.NO:7

Date :

AIM:
To write a C# program by using operator overloading.

ALGORITHM:
Steps:

1. Start the program.


2. Create a class named complex and declare the variables.
3. Create a constructor and assign the values to variables.
4. Write a function to implement operator overloading concept.
5. Create a class and create an object for the class complex.
6. Perform the calculations.
7. Display the result.
8. Stop the program.

PROGRAM:
using System;
using System.Collections.Generic;
using System.Text;

namespace overload1
{
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.Write(x);
Console.Write(" " + y);
Console.WriteLine(" ");
}
}

class Program
{
static void Main(string[] args)
{
complex a, b, c;
a = new complex(2.5, 3.5);
b = new complex(1.6, 2.7);
c = a + b;
Console.Write("a =");
a.display();
Console.Write("b=");
b.display();
Console.Write("c=");
c.display();
Console.Read();
}
}
}

OUTPUT:

a=2.5 3.5
b=1.6 2.7
c=4.1 6.2

RESULT:
Thus the program is compiled and executed successfully and the results are
obtained.
DELEGATES, EVENTS, ERRORS AND EXCEPTIONS

EX.NO:8(a)

Date :

AIM:
To write a C# program using delegates, events, errors and exceptions.

ALGORITHM:
Steps:
1. Start the program.
2. Create a namespace and declare a delegate function.
3. Create a class named event class and declare the variables.
4. Create a function and check the status.
5. Create a class named event test and create object for both classes.
6. Call the methods using that objects.
7. Display the result.
8. Stop the program.

PROGRAM:
using System;
using System.Collections.Generic;
using System.Text;

namespace dele2
{
public delegate void EDelegate(String str);
class eventclass
{
public event EDelegate status;
public void TriggerEvent()
{
if(status!=null)
status("event triggered");
}
}
class ex8
{
public static void Main()
{
eventclass ec=new eventclass();
ex8 et=new ex8();
ec.status+=new EDelegate(et.EventCatch);
ec.TriggerEvent();
Console.Read();
}
public void EventCatch(string str)
{
Console.WriteLine(str);
}
}
}

OUTPUT:

Event triggered

EX.NO:8. (b)

AIM:

To write a C# program using Errors and Exceptions.

ALGORITHM:

Steps:

1. Start the program.


2. Create a namespace and declare Exception function.
3. Create a class name Exception class and declare the variables.
4. Create a try, catch, finally blocks functions.
5. Display the result.
6. Stop the program.

PROGRAM:
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
try
{
Decimal dresult = Decimal.Divide(5, 0);
Console.WriteLine("Result is :{0}", dresult);
}
//catch (ArithmeticException exarith)
//{
// Console.WriteLine("Caught Arithmetic Exception : {0}",
// exarith.Message);
//}
catch (DivideByZeroException exdiv)
{
Console.WriteLine("Caught Divide By Zero Exception : {0}",
exdiv.Message);
}
catch (Exception ex)
{
Console.WriteLine("Caught Exception : {0}", ex.Message);
}
finally
{
Console.WriteLine("In Finally");
}
}
}
}

OUTPUT:

Caught Divide By Zero Exception: Attempted to divide by zero.


In Finally
Press any key to continue . . .

RESULT:
Thus the program is compiled and executed successfully and the results are
obtained.
CALCULATOR WIDGET

EX.NO:9

Date :

AIM:
To build a calculator widget in windows application using C#.

ALGORITHM:
Steps:
1. Create a windows application using C#.
2. Design the form with buttons and textbox like a calculator.
3. Name the buttons using property window.
3. Write the code for each button.
4. Build the application.
5. Display the result.
6. Stop the program

PROGRAM:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
public partial class frmcalc : Form
{
private string s1, s2, oper;
private bool isoper;

public frmcalc()
{
InitializeComponent();
s1 = s2 = oper = " ";
isoper = true;
}

private void button17_Click(object sender, EventArgs e)


{
s1 = s2 = oper = " ";
isoper = true;
txtResult.Text = s1;
this.dot.Enabled = false;
}

private void zero_Click(object sender, EventArgs e)


{
s1 += "0";
txtResult.Text = s1;
}

private void one_Click(object sender, EventArgs e)


{
s1 += "1";
txtResult.Text = s1;
}

private void two_Click(object sender, EventArgs e)


{
s1 += "2";
txtResult.Text = s1;
}

private void three_Click(object sender, EventArgs e)


{
s1 += "3";
txtResult.Text = s1;
}

private void four_Click(object sender, EventArgs e)


{
s1 += "4";
txtResult.Text = s1;
}

private void five_Click(object sender, EventArgs e)


{
s1 += "5";
txtResult.Text = s1;
}

private void six_Click(object sender, EventArgs e)


{
s1 += "6";
txtResult.Text = s1;
}

private void seven_Click(object sender, EventArgs e)


{
s1 += "7";
txtResult.Text = s1;
}

private void eight_Click(object sender, EventArgs e)


{
s1 += "8";
txtResult.Text = s1;
}

private void div_Click(object sender, EventArgs e)


{
oper = "/";
s2 = s1;
s1 = null;
txtResult.Text = oper;
isoper = true;
this.dot.Enabled = true;
}

private void mul_Click(object sender, EventArgs e)


{
oper = "*";
s2 = s1;
s1 = null;
txtResult.Text = oper;
isoper = true;
this.dot.Enabled = true;
}

private void sub_Click(object sender, EventArgs e)


{
oper = "-";
s2 = s1;
s1 = null;
txtResult.Text = oper;
isoper = true;
this.dot.Enabled = true;
}

private void nine_Click(object sender, EventArgs e)


{
s1 += "9";
txtResult.Text = s1;
}

private void add_Click(object sender, EventArgs e)


{
oper = "+";
s2 = s1;
s1 = null;
txtResult.Text = oper;
isoper = true;
this.dot.Enabled = true;
}

private void dot_Click(object sender, EventArgs e)


{
if (isoper)
{
s1 += ".";
this.dot.Enabled = false;
isoper = false;
txtResult.Text = s1;
}
}

private void equal_Click(object sender, EventArgs e)


{
float n1, n2, res = 0.0f;
n1 = float.Parse(s2);
n2 = float.Parse(s1);
switch (oper)
{
case "+":
res = n1 + n2;
break;
case "-":
res = n1 - n2;
break;
case "*":
res = n1 * n2;
break;
case "/":
res = n1 / n2;
break;
}
this.txtResult.Text = res.ToString();
isoper = true;
}

private void QUIT_Click(object sender, EventArgs e)


{
MessageBox.Show("See You Again", "Thank You");
this.Close();
}
}
}
OUTPUT:

RESULT:
Thus the program is compiled and executed successfully and the results are
obtained.
MULTI MODULE ASSEMBLY

EX.NO:10

Date :

AIM:
To write a C# program using multi module Assembly.

ALGORITHM:

Step 1 — Compiling Files with Namespaces Referenced by Other Files

csc /t:module Stringer.cs

Specifying the module parameter with the /t: compiler option indicates
that the file should be compiled as a module rather than as an assembly. The
compiler produces a module called Stringer.netmodule, which can be added to
an assembly

Step 2 — Compiling Modules with References to Other Modules

csc /addmodule:Stringer.netmodule /t:module Client.cs

Specify the /t:module option because this module will be added to an


assembly in a future step. Specify the /addmodule option because the code in
Client references a namespace created by the code in Stringer.netmodule.
The compiler produces a module called Client.netmodule that contains a
reference to another module, Stringer.netmodule.

Step 3 — Creating a Multifile Assembly Using the Assembly Linker

al Client.netmodule Stringer.netmodule
/main:MainClientApp.Main /out:myAssembly.exe /target:exe

You can use the MSIL Disassembler (Ildasm.exe) to examine the contents
of an assembly, or determine whether a file is an assembly or a module.

PROGRAM:

Stringer.cs
using System;
namespace myStringer
{
public class Stringer
{
public void StringerMethod()
{
System.Console.WriteLine("This is a line from
StringerMethod.");
}
}
}
Client.cs
using System;
using myStringer;
class MainClientApp
{
public static void Main()
{
Stringer myStringInstance = new Stringer();
Console.WriteLine("Client code executes");
myStringInstance.StringerMethod();
}
}

Compile:
C:\WINNT>cd microsoft.net

C:\WINNT\Microsoft.NET>cd framework

C:\WINNT\Microsoft.NET\Framework>cd v2.0.50727

C:\WINNT\Microsoft.NET\Framework\v2.0.50727>edit Stringer.cs

C:\WINNT\MICROS~1.NET\FRAMEW~1\V20~1.507>csc /t:module Stringer.cs

C:\WINNT\MICROS~1.NET\FRAMEW~1\V20~1.507>csc
/addmodule:Stringer.netmodule /t:module Client.cs

C:\WINNT\MICROS~1.NET\FRAMEW~1\V20~1.507>al Client.netmodule
Stringer.netmodule /main:MainClientApp.Main /out:myAssembly.exe
/target:exe

C:\WINNT\MICROS~1.NET\FRAMEW~1\V20~1.507>

OUTPUT:

RESULT:
Thus the program is compiled and executed successfully and the results are
obtained.
APPLICATION DEVELOPMENT ON .NET

EX.NO:11

Date :

AIM:
To write a application using Windows Forms .

ALGORITHM:
Steps:
1. Create a windows application using C#.
2. Design the form with buttons and textbox like a calculation forms.
3. Name the buttons using property window.
3. Write the code for each button.
4. Build the application.
5. Display the result.
6. Stop the program

PROGRAM:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WAArith
{
public partial class frmArith : Form
{
public frmArith()
{
InitializeComponent();
}

private void optAdd_CheckedChanged(object sender, EventArgs e)


{
try
{
txtResult.Text =
Convert.ToString(Convert.ToSingle(txtN1.Text) +
Convert.ToSingle(txtN2.Text));
}
catch (Exception er)
{
MessageBox.Show(er.Message);
}
}
private void optSub_CheckedChanged(object sender, EventArgs e)
{
txtResult.Text = Convert.ToString(Convert.ToSingle(txtN1.Text) -
Convert.ToSingle(txtN2.Text));
}

private void optMul_CheckedChanged(object sender, EventArgs e)


{
txtResult.Text = Convert.ToString(Convert.ToSingle(txtN1.Text)
* Convert.ToSingle(txtN2.Text));
}

private void optDiv_CheckedChanged(object sender, EventArgs e)


{
txtResult.Text = Convert.ToString(Convert.ToSingle(txtN1.Text) /
Convert.ToSingle(txtN2.Text));
}

private void cmdRepeat_Click(object sender, EventArgs e)


{
txtN1.Text = "";
txtN2.Text = "";
txtResult.Text = "";
txtN1.Focus();
}

private void btnExit_Click(object sender, EventArgs e)


{
MessageBox.Show("Bye to you");
this.Close();
}

private void tmrTime_Tick(object sender, EventArgs e)


{
string st;
st = Convert.ToString((DateTime.Now).Hour) +
":";
st += Convert.ToString((DateTime.Now).Minute) +
":";
st += Convert.ToString ((DateTime.Now).Second);
this.Text = st;
}

private void button1_Click(object sender, EventArgs e)


{
ofdlg.ShowDialog();
picStart.ImageLocation = ofdlg.FileName;
cdlg.ShowDialog();
groupBox1.BackColor = Color.Red;

}
}
}

OUTPUT:

RESULT:
Thus the program is compiled and executed successfully and the results are
obtained.
WEB APPLICATIONS

EX.NO:12

Date :

AIM:

ALGORITHM:
Steps:

1. Enter into the Microsoft Visual studio 2005.


2. Create a new project based on ASP.NET website.[File->New-
>Website->ASP.NET Website].
3. Enter into the design of the page.
4. Type the text in the design page.
5. Double click on “access data source” icon under the data toolbox.
6. Click on configure data source.
7. Type the specified database path and finally click finish.
8. Place the control data grid view from the data toolbox.
9. Set accessdatasource1 to the data source property of the data grid view.
10. Save and run the website.

PROGRAM:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page


{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button2_Click(object sender, EventArgs e)
{
if (this.txtFruit.Text != "")
{
lstFruits.Items.Add(txtFruit.Text);
txtFruit.Text = "";
txtFruit.Focus();

protected void Button1_Click(object sender, EventArgs e)


{
if (this.lstFruits.Items.Count != 0 && lstFruits.SelectedIndex != -1)
lstFruits.Items.Remove(lstFruits.SelectedItem);
}
protected void txtselFruit_TextChanged(object sender, EventArgs e)
{

}
protected void lstFruits_SelectedIndexChanged(object sender, EventArgs
e)
{
txtselFruit.Text = lstFruits.SelectedItem.Text;
}
}

OUTPUT:

RESULT:
Thus the program is compiled and executed successfully and the results are
obtained.