0% found this document useful (0 votes)
7 views

C#Lab Programs (2)

Uploaded by

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

C#Lab Programs (2)

Uploaded by

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

C# &.

Net Framework Lab

Davan Institute of Advanced Management Studies


Bachelor of Computer Applications
C# &.Net Lab Manual

Ramadevi A, BCA Dept.., DIAMS Page 1


C# &.Net Framework Lab

Part-A
1. Develop a C#.Net console application to demonstrate conditional statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Conditional_statement
{
class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Enter first integer value:");
int a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter second integer value:");
int b = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(" you have entered :"+ a);
Console.WriteLine("you have entered :" + b);
if (a > b)
Console.WriteLine("Greater value is:" + a);
else
Console.WriteLine("Greater value is:" + b);

}
}

2. Develop a C#.Net console application to demonstrate control statements


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

namespace Controlstatement
{
class Program
{
static void Main(string[] args){
int num;
Console.WriteLine("Enter the integer number:");
num = Convert.ToInt32(Console.ReadLine());

Ramadevi A, BCA Dept.., DIAMS Page 2


C# &.Net Framework Lab
int count = 0;
int n = num;
while (num > 0)
{
++count;
num = num / 10;
}
Console.WriteLine("Number {0} contains {1} digits", n, count);

}
}
}

3. Develop a C#program to demonstrate the windows control


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 Windowcontrol
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
if (textBox1.Text == "Davan" && textBox2.Text == "davan@123")
{
MessageBox.Show("Login Successful");
}
else
{
MessageBox.Show("Login Failed");
}
}
}
}

Ramadevi A, BCA Dept.., DIAMS Page 3


C# &.Net Framework Lab

4. Develop a C#program to demonstrate the Multithreading concept


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Multithreading
{
class Program
{
public static void A()
{
for (int i = 1; i <= 5; i++)
{
Console.WriteLine("Thread A=" + i);
if (i == 3)
Thread.Sleep(6000);
}
}
public static void B()
{
for (int j = 6; j <= 10; j++)
{
Console.WriteLine("Thread B=" + j);
}
}

static void Main(string[] args)


{
Thread t1 = new Thread(A);
Thread t2 = new Thread(B);
t1.Start();
t2.Start();
}
}
}

5. Demonstrate the Subroutines and functions in C#.Net


using System;
using System.Collections.Generic;
using System.Linq;

Ramadevi A, BCA Dept.., DIAMS Page 4


C# &.Net Framework Lab
using System.Text;

namespace Subroutines
{
class Program
{
public void Swap(int a, int b)
{
a = a + b;
b = a - b;
a = a - b;
Console.WriteLine("values after swapping{0} and {1}", a, b);
}
static void Main(string[] args)
{
Program p1 = new Program();
Console.WriteLine("Values before Swapping 10 and 20");
p1.Swap(10, 20);
Console.ReadKey();
}
}
}

6. Develop an application for deploying various builtin functions in C#.Net


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

namespace BuiltInFunction
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter 2 INTEGER vALUES:");
int x = Convert.ToInt32(Console.ReadLine());
int y = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Maximum number is:" + Math.Max(x, y));
Console.WriteLine("Minimum number is:" + Math.Min(x, y));
Console.WriteLine("Enter a string:");
string str = Console.ReadLine();
Console.WriteLine("Lower case String:" + str.ToLower());
Console.WriteLine("Upper case String:" + str.ToUpper());
}

Ramadevi A, BCA Dept.., DIAMS Page 5


C# &.Net Framework Lab
}
}

7. Construct a console application to demonstrate the OOPs concepts in C#.net

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

namespace objectconcepts
{
class Program
{
private string name;
private string dept;
public void SetName(string n)
{
name = n;
}
public void SetDepartment(string d)
{
dept = d;
}
public void Display()
{
Console.WriteLine("Encapsulation output:");
Console.WriteLine("Name:{0}", name);
Console.WriteLine("Department:{0}", dept);
}
}
class Vehicle
{
public string brand="ford";
public void Honk()
{
Console.WriteLine("Tuut Tuut!");
}
}
class Car:Vehicle
{
public string modelName="Benz";
}
class Animal
{
public virtual void animalSound()

Ramadevi A, BCA Dept.., DIAMS Page 6


C# &.Net Framework Lab
{
Console.WriteLine("The animal makes a sound");
}
}
class Pig:Animal
{
public override void animalSound()
{
Console.WriteLine("The pig says : Wee Wee");
}
}
class Dog:Animal
{
public override void animalSound()
{
Console.WriteLine("The Dog says : Bow Bow");
}
}
abstract class Showroom
{
public abstract void carshowroom();
}
class FourWheel:Showroom
{
public override void carshowroom()
{
Console.WriteLine("The showroomis :Bangalore motors");
}
}
class Demo
{

static void Main(string[] args)


{
Program p = new Program();
p.SetName("Laasya");
p.SetDepartment("Davan");
p.Display();
Console.ReadLine();
Console.WriteLine("Inheritance output:");
Car myCar = new Car();
myCar.Honk();
Console.WriteLine(myCar.brand + " \t" + myCar.modelName);
Console.Write("\n");
Console.WriteLine("Polymorphism output:");
Animal myAnimal = new Animal();

Ramadevi A, BCA Dept.., DIAMS Page 7


C# &.Net Framework Lab
Animal myPig = new Pig();
Animal myDog = new Dog();
myAnimal.animalSound();
myPig.animalSound();
myDog.animalSound();
Console.Write("\n");
Console.WriteLine("Abstraction output");
FourWheel fCar = new FourWheel();
fCar.carshowroom();

}
}
}

8. Develop a program to sorting an One dimensional array by accepting 5 elements from


user and arrange them in ascending order.

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

namespace ascending
{
class Program
{
static void Main(string[] args)
{
int i, j;
int[] a = new int[5];
Console.WriteLine("Enter 5 elements of an array:");
for (i = 0; i < 5; i++)
{
a[i] = Convert.ToInt32(Console.ReadLine());
}
for (i = 0; i < 5; i++)
{
for (j = i + 1; j < 5; j++)
{
if (a[i] > a[j])
{
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}

Ramadevi A, BCA Dept.., DIAMS Page 8


C# &.Net Framework Lab
}
}
Console.WriteLine("Sorted array is:");
for (i = 0; i < 5; i++)
{
Console.WriteLine(a[i]);
}
Console.ReadKey();

}
}
}

9. Develop a program to perform sum of 2 compatible matrix of two dimensional array.

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

namespace Matrix
{
class Program
{
static void Main(string[] args)
{
int[,] a = new int[5, 5];
int[,] b = new int[5, 5];
int[,] c = new int[5, 5];
int i, j, n, m;
Console.WriteLine("Enter rows and columns of 2 matrices:");
m = Convert.ToInt32(Console.ReadLine());
n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the elements of matrix A:");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
a[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
Console.WriteLine("Enter the elements of matrix B:");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{

Ramadevi A, BCA Dept.., DIAMS Page 9


C# &.Net Framework Lab
b[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
c[i, j] = a[i, j] + b[i, j];
}

}
Console.WriteLine("Addition matrix:");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
Console.Write("{0}\t", c[i, j]);
}
Console.Write("\n");
}
Console.ReadKey();

}
}
}

Ramadevi A, BCA Dept.., DIAMS Page 10


C# &.Net Framework Lab

Part-B
1. Demonstarte abstract class and abstract methods in C#

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

namespace Abstract
{
abstract class Arith
{
public const float pi = 3.41F;
public abstract void Area(float r);
public abstract void Circum(float r);
}
class Operation : Arith
{
public override void Area(float r)
{
float a = 2 * pi * r;
Console.WriteLine("Area of Circle:" + a);
}
public override void Circum(float r)
{
float c = pi * r * r;
Console.WriteLine("Circumference of Circle:" + c);
}
}

class Program
{
static void Main(string[] args)
{
Operation op = new Operation();
Console.WriteLine("Enter the radius of Circle:");
float radius = Convert.ToInt32(Console.ReadLine());
op.Area(radius);
op.Circum(radius);
Console.ReadKey();
}
}
}

Ramadevi A, BCA Dept.., DIAMS Page 11


C# &.Net Framework Lab
2. Develop a program to perform single level inheritance in c#

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

namespace SingleInheritance
{
class College
{
public void Cname()
{
Console.WriteLine("We are studying in Davan college");
}
}
class Department : College
{
public void Dname()
{
Console.WriteLine("We are studying in BCA Department");
}
}

class Program
{
static void Main(string[] args)
{
Department d = new Department();
d.Cname();
d.Dname();
}
}
}

3. Develop a program to perform switch statement to display percentage of a student

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

namespace Switchpercentage
{
class Program
Ramadevi A, BCA Dept.., DIAMS Page 12
C# &.Net Framework Lab
{
static void Main(string[] args)
{
int a=0;
Console.WriteLine("Enter percentage of Student");
double p = Convert.ToDouble(Console.ReadLine());
if (p > 0 && p < 40)
a = 1;
else if (p >= 40 && p < 70)
a = 2;
else if (p >= 70 && p < 100)
a = 3;
switch(a)
{
case (1): Console.WriteLine("Fail");
break;
case (2): Console.WriteLine("Second Class");
break;
case (3): Console.WriteLine("First Class");
break;
default: Console.WriteLine("Invalid input");
break;
}
Console.ReadKey();

}
}
}

4. Write a program in c# to find the sumof each rows of a jagged array of 3 inner arrays

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

namespace JaggedArray
{
class Program
{
static void Main(string[] args)
{
int sum = 0;
int[][] jagged_arr = new int[4][];

Ramadevi A, BCA Dept.., DIAMS Page 13


C# &.Net Framework Lab
jagged_arr[0] = new int[] { 1, 2, 3, 4 };
jagged_arr[1] = new int[] { 11, 34,67 };
jagged_arr[2] = new int[] { 89,23 };
jagged_arr[3] = new int[] { 0, 45, 78, 53,99 };
for(int i=0;i<jagged_arr.Length;i++)
{
Console.Write("Row({0})elements are:",i);
for(int j=0;j<jagged_arr[i].Length;j++)
{
Console.WriteLine("{0}",jagged_arr[i][j]);
sum += jagged_arr[i][j];
}
Console.WriteLine("Sum of elements of:{0}\n",sum);
}
Console.ReadKey();
}
}
}

5. Write a program to perform Timer control properties Start(), Stop().


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 timer1_Tick(object sender, EventArgs e)


{
label2.Text = DateTime.Now.ToLongTimeString();
}
private Timer tm = new Timer();
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
tm.Tick += new EventHandler(timer1_Tick);
tm.Interval = 1000;
MessageBox.Show("Timer has started");
}

Ramadevi A, BCA Dept.., DIAMS Page 14


C# &.Net Framework Lab

private void button2_Click(object sender, EventArgs e)


{

timer1.Stop();
MessageBox.Show("Timer has stoped");
}

}
}

6. Write a program in C# implement a delegate for any two arithmetic operations.

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

namespace Delegates
{
delegate int MyDel(int num);
public class Mathclass
{
public static int Qube(int num)
{
return num * num * num;
}
public static int Square(int num)
{
return num * num;
}
}
class Program
{
static void Main(string[] args)
{
MyDel[] Opers = { Mathclass.Qube, Mathclass.Square };
int result = 0;
for (int i = 0; i < Opers.Length; i++)
{
result = Opers[i](10);
Console.WriteLine("Operation[{0}]:", i);
Console.WriteLine("\t Result:" + result);
}
Console.ReadKey();
}
}
}

Ramadevi A, BCA Dept.., DIAMS Page 15


C# &.Net Framework Lab

7. Design an applications to build various string operations such as reversing, case conversion,
length and concatenation.

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 WindowsFormsApplication7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{

int length = textBox1.Text.Length - 1;


String reverse = "";
while (length >= 0)
{
reverse = reverse + textBox1.Text[length];
length--;
}
label3.Text = reverse;
}

private void button2_Click(object sender, EventArgs e)


{
label3.Text = textBox1.Text.ToLower();
}

private void button3_Click(object sender, EventArgs e)


{
label3.Text = textBox1.Text.ToUpper();
}

private void button4_Click(object sender, EventArgs e)

Ramadevi A, BCA Dept.., DIAMS Page 16


C# &.Net Framework Lab
{
label3.Text = Convert.ToString(textBox1.Text.Length);
}

}
}

8. To demonstrate a) Boxing and unboxing b) Invalid boxing

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

namespace Typeconversion
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Boxing");
int num = 2020;
Object obj = num;
num = 100;
Console.WriteLine("Value of num is:{0}", num);
Console.WriteLine("Value of Object is:{0}", obj);
Console.WriteLine("Unboxing");
int n = 23;
Object obj1 = n;
int i = (int)obj1;
Console.WriteLine("Value of Object is:{0}", obj1);
Console.WriteLine("Value of num is:{0}", i);
Console.WriteLine("Invalid unboxing");
int a = 20;
Object p = a;
double d = (double)p;
Console.ReadKey();
}

}
}

Ramadevi A, BCA Dept.., DIAMS Page 17


C# &.Net Framework Lab

9. Write a program to demonstrate the use virtual and override keywords in C#

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

namespace Virtualmethod
{
class Company
{
public string c_name = "Microsoft";
public virtual void Display()
{
Console.WriteLine("Company name is:" + c_name);
}
}
class Department : Company
{
public string d_name = "Maintanence";
public override void Display()
{
Console.WriteLine("I am working in {0} department" , d_name);
}
}
class Program
{
static void Main(string[] args)
{
Department d = new Department();
d.Display();
Company c = new Company();
c.Display();
Console.ReadKey();

}
}
}

Ramadevi A, BCA Dept.., DIAMS Page 18

You might also like