3. .NET PRECTICALS
3
List of Programs
Serial
Number
Name of the program Remark
1 Write a program to display a hello/welcome message
2 WAP for Demonstrating Data Type Conversion
3 WAP for Demonstrating String Handling
4 Given the radius of the circle, WAP to compute the area of
the circle, circumference of the circle and display their value.
5 Admission to a professional course is subject to the
following cond
Mks in maths>=80;Mks in Physics>=75;Mks in chem.>=60;
Total Mks all three>=250 or total in maths and physics>=
180
Given the details about the mks, WAP to process the
applications to list the eligible candidates.
6 WAP that will read the value of x and evaluate the following
function
Y= { 1 for x > 0; 0 for x = 0; -1 for x < 0 }
Using a) nested if statements b) else if statements and c)
conditional operators?
7 shown below is the Floyd’s triangle
1
23
456
78910
.
.
.
79………….91
WAP to print this triangle.
8 WAP to produce the following patterns
* $$$$ 1
** $$$ 22
*** $$ 333
**** $ 4444
9 WAP to generate and then sum the Fibonacci series
10 Demonstrate the typical use of the following jump
statements:
break
continue
4. .NET PRECTICALS
4
In a single program
11 WAP for Declaring Class and invokeing a method.
12 WAP to explain the concept of boxing and unboxing
13 WAP to demonstrate the addition of byte type of variables
14 WAP for using System.Collection Namespace
15 WAP for Implementing Jagged Arrays
16 Given the two one-dimensional arrays A and B which are
sorted in ascending order. WAP to merge them into a single
sorted array C that contains every items from arrays A and
B in ascending order
17 WAP for Using Reflection Class
18 WAP for Using GDI+ Classess
19 WAP for Displaying Constructors and Destructors
20 WAP to implement Single inheritance
21 WAP to implement MultiLevel inheritance
22 WAP to implement Polymorphism
23 WAP to implement Operator Overloading
24 WAP to deal with Exception Handling
25 WAP fo displaying Randomly Generated button and Click
Event
26 WAP to Demonstrate Multi Threading Example
27 WAP to implement delegates
28 WAP to implement multicast delegates
29 WAP for displaying Data Handling Application
30 WAP to use Window Form Application
31 Design and develop application as of your own choice
5. .NET PRECTICALS
5
1.Write a program to display a hello/welcome message
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Console.Write("Welcome MCA in UIM");
Console.ReadKey();
}
}
}
6. .NET PRECTICALS
6
2. WAP for Demonstrating Data Type Conversion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
string str1 = "123.45";
Single sngl1 = Convert.ToSingle(str1);
int x = Convert.ToInt16(sngl1);
Console.WriteLine("Single 1 = {0}", sngl1);
Console.WriteLine("Integer 1 = {0}", x);
double y = Convert.ToDouble(str1);
Console.WriteLine("Double = {0}", y);
Int16 z = Convert.ToInt16(y);
Console.WriteLine("Int16 = {0}", z);
Console.ReadLine();
}
}
}
7. .NET PRECTICALS
7
3.. WAP for Demonstrating String Handling
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace String_Handling
{
class Program
{
static void Main(string[] args)
{
String s1 = "preeti shahi";
String s2 = s1.Insert(2, "i");
Console.WriteLine(s2);
String s3 = "preeti shahi";
Boolean b = s3.Equals(s1);
Console.WriteLine(b.ToString());
Console.WriteLine(s1.ToLower());
Console.WriteLine(s1.ToUpper());
String s4 = s1.Substring(5);
Console.WriteLine(s4);
Console.ReadKey();
}
}
}
8. .NET PRECTICALS
8
4 . Given the radius of the circle, WAP to compute the area of the circle, circumference
of the circle and display their value.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Circle
{
class Program
{
static void Main(string[] args)
{
const double pi = 3.14;
Console.WriteLine("Enter the radius of the circle");
double r = Convert.ToDouble(Console.ReadLine());
double area = pi * r * r;
Console.WriteLine("Area is:" + area.ToString());
double curc = 2 * pi * r;
Console.WriteLine("Circumference is:" + curc.ToString());
Console.ReadKey();
}
}
9. .NET PRECTICALS
9
5.Admission to a professional course is subject to the following cond
Mks in maths>=80;Mks in Physics>=75;Mks in chem.>=60; Total Mks all three>=250
or total in maths and physics>= 180
Given the details about the mks, WAP to process the applications to list the eligible
candidates.
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the marks of Maths,Physics and
Chemistry");
int Math = int.Parse(Console.ReadLine());
int Phys = int.Parse(Console.ReadLine());
int Chem = int.Parse(Console.ReadLine());
int total = Math + Phys + Chem;
int MP = Math + Phys;
if (total >= 250 || MP >= 180)
{
if (Math >= 80 && Phys >= 75 && Chem >= 60)
{
Console.WriteLine("Candidates is Eligible");
}
else
{
Console.WriteLine("Candidates is not Eligible");
}
}
else
{
Console.WriteLine("Candidates is not Eligible");
}
Console.ReadKey();
}
}
11. .NET PRECTICALS
11
6.WAP that will read the value of x and evaluate the following function
Y= { 1 for x > 0; 0 for x = 0; -1 for x < 0 }
Using a) nested if statements b) else if statements and c) conditional operators?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Evaluate_x
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the value of X");
int x = int.Parse(Console.ReadLine());
int y = 0;
//Using Nested If Statement
if (x > 0)
{
y = 1;
}
else if (x == 0)
{
y = 0;
}
else if (x < 0)
{
y = -1;
}
Console.WriteLine("Using else-if Statement:" + y.ToString());
y = (x == 0) ? 0 : (x > 0) ? 1 : (x < 0) ? -1 : 0;
Console.WriteLine("Using Conditional Operation:" + y.ToString());
Console.ReadKey();
}
}
}
13. .NET PRECTICALS
13
7. shown below is WAP to show the Floyd’s triangle on the basis of rows
using System;
class Program
{
static void Main(string[] args)
{
int n, i, c, a = 1;
Console.WriteLine("Enter the number of rows till you want to
print");
n = int.Parse(Console.ReadLine());
for (i = 1; i <= n; i++)
{
for (c = 1; c <= i; c++)
{
Console.Write(a.ToString());
a++;
}
Console.WriteLine();
}
Console.ReadKey();
}
}
14. .NET PRECTICALS
14
8. shown below is the Floyd’s triangle
1
23
456
78910
.
.
.
79………….91
WAP to print this triangle.
using System;
class Program
{
static void Main(string[] args)
{
int n, i, c, a = 1;
bool k = true;
Console.WriteLine("Enter the number till you want to print");
n = int.Parse(Console.ReadLine());
for (i = 1; i <= n; i++)
{
for (c = 1; c <= i; c++)
{
if(a==n)
{
k=false;
break;
}
Console.Write( a.ToString());
a++;
}
if (k == false)
{
break;
}
Console.WriteLine();
}
Console.ReadKey();
}
}
16. .NET PRECTICALS
16
9.WAP to produce the following patterns
* $$$$ 1
** $$$ 22 .
*** $$ 333
**** $ 4444
using System;
class Program
{
static void Main(string[] args)
{
int n, c, k;
Console.WriteLine("Enter The Number Of Rows");
n = int.Parse(Console.ReadLine());
//Number Tirangle
for (c = 1; c <= n; c++)
{
for (k = 1; k <= c; k++)
{
Console.Write(c);
}
Console.WriteLine();
}
Console.WriteLine();
//Dollar Tirangle
for (c = 1; c <= n; c++)
{
for (k = 1; k <= c; k++)
{
Console.Write("$");
}
Console.WriteLine();
}
Console.WriteLine();
// Star Traingle
for (c = n; c >= 1; c--)
{
for (k = 1; k <= c; k++)
{
Console.Write("*");
}
Console.WriteLine();
}
Console.ReadKey();
}
}
18. .NET PRECTICALS
18
10..WAP to generate and then sum the fibonacci series:-
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the terms in Fibonacci series:-");
int n = int.Parse(Console.ReadLine());
int a = -1, b = 1, c = 0, sum = 0;
for (int i = 1; i <= n; i++)
{
c = a + b;
sum = sum + c;
a = b;
b = c;
}
Console.WriteLine(sum.ToString());
Console.ReadKey();
}
}
19. .NET PRECTICALS
19
11. Demonstrate the typical use of the following jump statements:
break
continue
In a single program
using System;
class Program
{
static void Main(string[] args)
{
//Continue Statement
for (int i = 0; i <= 15; i++)
{
if (i == 5)
{
continue;
}
Console.Write(i.ToString() + " ");
}
Console.WriteLine();
//Break Statement
for (int i = 0; i <= 15; i++)
{
if (i == 10)
{
break;
}
Console.Write(i.ToString() + " ");
}
Console.ReadKey();
}
}
21. .NET PRECTICALS
21
12.WAP for Declaring Class and invoking a method.
using System;
class Rect
{
int l, b;
public Rect(int l, int b)
{
this.l = l;
this.b = b;
}
public int getArea()
{
return l * b;
}
}
class Program
{
static void Main(string[] args)
{
Rect a = new Rect(28, 15);
Console.WriteLine("Area of rectangle is : " +
a.getArea().ToString());
Console.ReadKey();
}
}
22. .NET PRECTICALS
22
13.WAP to explain the concept of boxing and un boxing
using System;
class Program
{
static void Main(string[] args)
{
int i = 123;
object o = i;//Boxing
Console.WriteLine("Boxing OK");
int j = (int)o; //UnBoxing
System.Console.WriteLine("Unboxing OK");
try
{
int k = (short)o;//InvalidCastException Generated
System.Console.WriteLine("Unboxing OK");
}
catch (System.InvalidCastException e)
{
System.Console.WriteLine("Incorrect Unboxing.", e.Message);
}
Console.ReadKey();
}
}
23. .NET PRECTICALS
23
14.WAP to demonstrate the addition of byte type of variables:-
using System;
class Program
{
static void Main(string[] args)
{
byte b1 = 88;
byte b2 = 79;
//byte sum = b1 + b2; error cannot implicitly convert int to byte
int sum = b1 + b2; //No error
Console.WriteLine(sum.ToString());
Console.ReadKey();
}
}
24. .NET PRECTICALS
24
15. WAP for using System.Collection Namespace
using System;
using System.Collections;
namespace system.collection_array_list
{
class Program
{
static void Main(string[] args)
{
ArrayList n = new ArrayList();
n.Add("Preeti Shahi");
n.Add("Yamini Bisht");
n.Add("Gaurav Sharma");
n.Add("Sachin Sisodia");
n.Add("Abhishek Sharma");
n.Add("Abhishek Pathak");
Console.WriteLine("Capacity=" + n.Capacity);
Console.WriteLine("Element present=" + n.Count);
for (int i = 0; i < n.Count; i++)
{
Console.Write(n[i] + " ");
}
Console.WriteLine();
//Sorting Array List
n.Sort();
for (int i = 0; i < n.Count; i++)
{
Console.Write(n[i] + " ");
}
Console.WriteLine();
//Removing An element
n.RemoveAt(2);
for (int i = 0; i < n.Count; i++)
{
Console.Write(n[i] + " ");
}
Console.ReadKey();
}
}
}
28. .NET PRECTICALS
28
17. Given the two one-dimensional arrays A and B which are sorted in ascending order.
WAP to merge them into a single sorted array C that contains every items from arrays A
and B in ascending order
using System;
class Program
{
static void Main(string[] args)
{
int[] a = { 10, 20, 30, 40, 50, 60, 70, 80, 90 };
int[] b = { 18, 36, 54, 72, 90, 108, 126, 144, 120 };
int[] c = new int[a.Length + b.Length];
//Mearging To New Array C
a.CopyTo(c, 0);
b.CopyTo(c, a.Length);
//Sorting Array C
for (int i = 0; i < c.Length; i++)
{
for (int j = i + 1; j < c.Length; j++)
{
if (c[i] > c[j])
{
int temp = c[i];
c[i] = c[j];
c[j] = temp;
}
}
}
for (int i = 0; i < c.Length; i++)
{
Console.WriteLine(c[i]);
}
Console.ReadKey();
}
}
30. .NET PRECTICALS
30
18. WAP for Using Reflection Class
using System;
using System.Reflection;
namespace Wap_To_Reflection
{
class Program
{
static void Main(string[] args)
{
Type t = typeof(System.String);
ConstructorInfo[] ci = t.GetConstructors();
Console.WriteLine("Constructors are");
foreach (ConstructorInfo ctemp in ci)
{
Console.WriteLine(ctemp.ToString());
}
Console.WriteLine();
Console.WriteLine("Methods are");
MethodInfo[] mifo = t.GetMethods();
foreach (MethodInfo mifot in mifo)
{
Console.WriteLine(mifot.ToString());
}
Console.ReadKey();
}
}
}
31. .NET PRECTICALS
31
19. WAP for Using GDI+ Classes
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Graphics g = CreateGraphics();
Color forecolor = Color.Black;
Color backcolor = Color.White;
Font font = new Font("Algerian", 26);
g.FillRectangle(new SolidBrush(backcolor), ClientRectangle);
g.DrawString("Hello friends … m VICKY", font, new
SolidBrush(forecolor), 15, 15);
Pen myPen = new Pen(new SolidBrush(forecolor), 2);
Point p1 = new Point(40);
Point p2 = new Point(90);
g.DrawRectangle(myPen, 50, 150, 100, 50);
g.DrawEllipse(myPen, 100, 50, 80, 40);
g.FillEllipse(Brushes.Black, 100, 50, 80, 40);
}
}
}
33. .NET PRECTICALS
33
20. WAP for Displaying Constructors and Destructors
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Constructor_and_Distructor
{
class Rect
{
private int l, b;
public Rect(int l, int b)
{
Console.WriteLine("Constructor Call");
this.l = l;
this.b = b;
}
public int area()
{
return l * b;
}
Rect()
{
Console.WriteLine("Destructor call");
}
}
class Program
{
static void Main(string[] args)
{
Rect r = new Rect(32, 24);
Console.WriteLine(r.area().ToString());
Console.ReadKey();
}
}
}
35. .NET PRECTICALS
35
21. WAP to implement Single inheritance
using System;
class Item
{
public void x()
{
Console.WriteLine("Item Code=***");
}
}
class Fruit : Item
{
public void type()
{
Console.WriteLine("Fruit type = Mango");
}
}
class SingleInheritance
{
static void Main(string[] args)
{
Item item = new Item();
Fruit fruit = new Fruit();
item.x();
fruit.type();
Console.ReadKey();
}
}
36. .NET PRECTICALS
36
22. WAP to implement MultiLevel inheritance
using System;
class Student
{
public string name;
public int age;
public Student(string name, int age)
{
this.name = name;
this.age = age;
}
}
class Player : Student
{
public string sport;
public string team;
public Player(string sport, string team, string name, int age)
: base(name, age)
{
this.sport = sport;
this.team = team;
}
}
class CricketPlayer : Player
{
public int runs;
public CricketPlayer(int runs, string sport, string team, string
name, int age)
: base(sport, team, name, age)
{
this.runs = runs;
}
public void Show()
{
Console.WriteLine("Player:" + name);
Console.WriteLine("Age:" + age);
Console.WriteLine("Sport:" + sport);
Console.WriteLine("Team:" + team);
Console.WriteLine("Runs:" + runs);
}
}
class Program
{
static void Main(string[] args)
{
CricketPlayer u = new CricketPlayer(210, "Cricket", "India",
"Sachin Tendulkar", 43);
u.Show();
Console.ReadKey();
}
}
38. .NET PRECTICALS
38
23. WAP to implement Polymorphism
using System;
class Fruit
{
public virtual void display()
{
Console.WriteLine("Seasonal Fruits");
}
}
class SeasonalFruit : Fruit
{
public override void display()
{
Console.WriteLine("Seasonal Fruit:Mango");
}
}
class NonSeasonalFruit : Fruit
{
public override void display()
{
Console.WriteLine("NonSeasonal Fruit:Grapes");
}
}
class InclusionPolymorphism
{
static void Main(string[] args)
{
Fruit m = new Fruit();
m = new SeasonalFruit();//Upcasting
m.display();
m = new NonSeasonalFruit();//Upcasting
m.display();
Console.ReadKey();
}
}
40. .NET PRECTICALS
40
24. WAP to implement Operator Overloading
using System;
class A
{
int a, b;
public void input(int x, int y)
{
a = x;
b = y;
}
public void output()
{
Console.WriteLine(a + " " + b);
}
public static A operator +(A a1, A a2)//Binary Operator OverLoad(+)
{
A a3 = new A();
a3.a = a1.a + a2.a;
a3.b = a1.b + a2.b;
return a3;
}
}
class B
{
static void Main()
{
A a1 = new A();
A a2 = new A();
A a3 = new A();
a1.input(38, 46);
a1.output();
a2.input(40, 59);
a2.output();
a3 = a1 + a2;
a3.output();
Console.Read();
}
}
42. .NET PRECTICALS
42
25. WAP to deal with Exception Handling
using System;
class A
{
static void Main()
{
try
{
int a, b, c;
Console.WriteLine("Enter a and b:");
a = int.Parse(Console.ReadLine());
b = int.Parse(Console.ReadLine());
c = a / b;
Console.WriteLine("Output is" + c);
}
catch (Exception e)
{
Console.WriteLine("Divide by zero is an error");
}
Console.Read();
}
}
43. .NET PRECTICALS
43
26. WAP for displaying Randomly Generated button and Click Event
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Randomly_Generated_Button
{
public partial class RandomGenBt : Form
{
private Button b1;
private int counter1,counter2;
public RandomGenBt()
{
counter1 = 25;
counter2 = 25;
InitializeComponent();
}
private void cmdGen_Click(object sender, EventArgs e)
{
b1 = new Button();
b1.Location = new Point(counter1, counter2);
b1.Size = new Size(50, 25);
b1.Text = counter1.ToString() + " " + counter2.ToString();
b1.Click+=new EventHandler(x);
this.Controls.Add(b1);
counter1 = counter1 + 52;
counter2 = counter2 + 27;
}
private void x(object sender,EventArgs e)
{
MessageBox.Show(b1.Text.ToString());
}
}
}
45. .NET PRECTICALS
45
27. WAP to Demonstrate Multi Threading Example
using System;
using System.Collections.Generic;
using System.Threading;
class Program
{
static void Main(string[] args)
{
// creating more than one thread in the application
Thread FirstThread = new Thread(FirstFunction);
Thread SecondThread = new Thread(SecondFunction);
Thread ThirdThread = new Thread(ThirdFunction);
Console.WriteLine("nThreading Starts...n");
//Starting more than one thread in the application
FirstThread.Start();
SecondThread.Start();
ThirdThread.Start();
}
public static void FirstFunction()
{
for (int number = 1; number <= 5; number++)
{
Console.WriteLine("First Thread Displays: " + number);
Thread.Sleep(200);
}
}
public static void SecondFunction()
{
for (int number = 1; number <= 5; number++)
{
Console.WriteLine("Second Thread Displays: " + number);
Thread.Sleep(500);
}
}
public static void ThirdFunction()
{
for (int number = 1; number <= 5; number++)
{
Console.WriteLine("Third Thread Displays: " + number);
Thread.Sleep(700);
}
Console.Write("nPress ENTER to quit...");
Console.ReadLine();
}
}
47. .NET PRECTICALS
47
28. WAP to implement delegates
using System;
delegate void MCA();
class A
{
public void f1()
{
Console.WriteLine("f1 Method");
}
public void f2()
{
Console.WriteLine("f2 Method");
}
}
class B
{
static void Main()
{
A a1 = new A();
MCA x;
x = a1.f1;
x += a1.f2;
x();
Console.Read();
}
}
48. .NET PRECTICALS
48
29. WAP to implement multicast delegates
using System;
public delegate void MCA();
class Deltest
{
public void a()
{
Console.WriteLine("Single Delegate");
}
public void b()
{
Console.WriteLine("Multicast Delegate");
}
}
class Program
{
static void Main(string[] args)
{
Deltest dt = new Deltest();
MCA d = new MCA(dt.a);
d += new MCA(dt.b);
d();
Console.ReadKey();
}
}
49. .NET PRECTICALS
49
30. WAP for displaying Data Handling Application
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;
public partial class DataHandling : Form
{
OdbcConnection con;
OdbcDataAdapter da;
DataSet ds;
OdbcDataReader dr;
OdbcCommand cmd;
private int ID;
public DataHandling()
{
con = new OdbcConnection("DSN=Icon;");
con.Open();
da = new OdbcDataAdapter("Select * From tblItem", con);
ds = new DataSet();
da.Fill(ds);
InitializeComponent();
}
private void cmdShow_Click(object sender, EventArgs e)
{
dgItems.DataSource = ds.Tables[0];
}
private void dgItems_Click(object sender, EventArgs e)
{
txtName.Text = dgItems.CurrentRow.Cells[1].Value.ToString();
txtID.Text = dgItems.CurrentRow.Cells[2].Value.ToString();
ID = (int)dgItems.CurrentRow.Cells[0].Value;
}
private void cmdUpdate_Click(object sender, EventArgs e)
{
cmd = new OdbcCommand("Update tblItem Set Item_Name='" +
txtName.Text.ToString() + "',Item_Code='" + txtID.Text.ToString() + "' Where
Item_ID=" + ID, con);
cmd.ExecuteNonQuery();
}
}