Branching and Looping EX - NO:1 Date: Aim:: Program
Branching and Looping EX - NO:1 Date: Aim:: Program
Branching and Looping EX - NO:1 Date: Aim:: Program
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]);
}
}
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:
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:
PROGRAM:
using System;
using System.Collections.Generic;
using System.Text;
namespace overload1
{
class complex
{
double x;
double y;
public complex()
{
}
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:
ALGORITHM:
Steps:
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:
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;
}
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:
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
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
/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();
}
}
}
}
OUTPUT:
RESULT:
Thus the program is compiled and executed successfully and the results are
obtained.
WEB APPLICATIONS
EX.NO:12
Date :
AIM:
ALGORITHM:
Steps:
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;
}
protected void Button2_Click(object sender, EventArgs e)
{
if (this.txtFruit.Text != "")
{
lstFruits.Items.Add(txtFruit.Text);
txtFruit.Text = "";
txtFruit.Focus();
}
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.