Vous êtes sur la page 1sur 50

Department of Computer Science

C# PROGRAMMING LAB

(UCS15603)

LAB MANUAL

For

B.Sc Degree Programme – 2015 Regulation

Academic Year (2018 – 2019) - Sixth Semester

Prepared By Approved By

S. NITHYA HOD
U. UDAYAKUMAR

1
Index
Sno Program Name
WRITE A PROGRAM IN C# TO CHECK WHETHER A NUMBER IS
1
PALINDROME OR NOT.
WRITE A PROGRAM IN C# TO DEMONSTRATE COMMAND LINE
2
ARGUMENTS PROCESSING
WRITE A PROGRAM IN C# TO FIND THE ROOTS OF QUADRATIC
3
EQUATION.
WRITE A PROGRAM IN C# TO DEMONSTRATE BOXING AND
4
UNBOXING.

5 WRITE A PROGRAM IN C# TO IMPLEMENT STACK OPERATIONS

6 WRITE A PROGRAM TO DEMONSTRATE OPERATOR OVERLOADING.


WRITE A PROGRAM IN C# TO FIND THE SECOND LARGEST
7
ELEMENT IN A SINGLE DIMENSIONAL ARRAY.
WRITE A PROGRAM IN C# TO MULTIPLY TO MATRICES USING
8
RECTANGULAR ARRAYS
FIND THE SUM OF ALL THE ELEMENTS PRESENT IN A JAGGED
9
ARRAY OF 3 INNER ARRAYS
10 WRITE A PROGRAM TO REVERSE A GIVEN STRING USING C#.
USING TRY, CATCH AND FINALLY BLOCKS WRITE A PROGRAM IN
11
C# TO DEMONSTRATE ERROR HANDLING.
12 DESIGN A SIMPLE CALCULATOR USING SWITCH STATEMENT IN C#.
DEMONSTRATE USE OF VIRTUAL AND OVERRIDE KEY WORDS IN
13
C# WITH A SIMPLE PROGRAM
IMPLEMENT LINKED LISTS IN C# USING THE EXISTING
14
COLLECTIONS NAME SPACE.
WRITE A PROGRAM TO DEMONSTRATE ABSTRACT CLASS AND
15
ABSTRACT METHODS IN C#.
WRITE A PROGRAM IN C# TO BUILD A CLASS WHICH IMPLEMENTS
16
AN INTERFACE WHICH ALREADY EXISTS.
WRITE A PROGRAM TO ILLUSTRATE THE USE OF DIFFERENT
17
PROPERTIES IN C#.
DEMONSTRATE ARRAYS OF INTERFACE TYPES WITH A C#
18
PROGRAM.

2
Exno: 1

WRITE A PROGRAM IN C# TO CHECK WHETHER A NUMBER IS PALINDROME


OR NOT.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Palindrome

class Program

static void Main(string[] args)

int num,temp;

int digit;

int reverse = 0;

Console.WriteLine("Enter a number");

num = int.Parse(Console.ReadLine());

temp=num;

while(num!=0)

digit = num % 10;

reverse = reverse * 10 + digit;

num=num /= 10;

3
Console.WriteLine("The reverse of the number is: {0}",reverse);

if (temp == reverse)

Console.WriteLine("This number is a palindrome!");

Console.ReadLine();

else

Console.WriteLine("This number is not a palindrome");

Console.ReadLine();

Output

4
Exno: 2

WRITE A PROGRAM IN C# TO DEMONSTRATE COMMAND LINE ARGUMENTS


PROCESSING

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Commandline1

class Program

static void Main(string[] args)

Console.WriteLine("\nNumber of CommadLine Arguments :" + args.Length);

Console.Write("\nCommandline Arguments Are :\t");

for (int i = 0; i < args.Length; i++)

Console.Write(args[i] + "\t");

Console.ReadLine();

Output

5
6
Exno: 3

WRITE A PROGRAM IN C# TO FIND THE ROOTS OF QUADRATIC EQUATION.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Quadratic

class Program

static void Main(string[] args)

float a, b, c;

double disc, deno, x1, x2;

Console.WriteLine("ENTER THE VALUES OF A,B,C...");

a = float.Parse(Console.ReadLine());

b = float.Parse(Console.ReadLine());

c = float.Parse(Console.ReadLine());

if (a == 0)

x1 = -c / b;

Console.WriteLine("The roots are Linear:", x1);

else

7
disc = (b * b) - (4 * a * c);

deno = 2 * a;

if (disc > 0)

Console.WriteLine("THE ROOTS ARE REAL AND DISTINCT ROOTS");

x1 = (-b / deno) + (Math.Sqrt(disc) / deno);

x2 = (-b / deno) - (Math.Sqrt(disc) / deno);

Console.WriteLine("THE ROOTS ARE... " + x1 + " and " + x2);

else if (disc == 0)

Console.WriteLine("THE ROOTS ARE REPEATED ROOTS");

x1 = -b / deno;

Console.WriteLine("THE ROOT IS...: " + x1);

else

Console.WriteLine("THE ROOTS ARE IMAGINARY ROOTS\n");

x1 = -b / deno;

x2 = ((Math.Sqrt((4 * a * c) - (b * b))) / deno);

Console.WriteLine("THE ROOT 1: " + x1 + "+i" + x2);

Console.WriteLine("THE ROOT 2:" + x1 + "-i" + x2);

Console.ReadLine();

8
}

Output

9
Exno:4

WRITE A PROGRAM IN C# TO DEMONSTRATE BOXING AND UNBOXING.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Boxing

class Program

static void Main(string[] args)

int m = 10;

object a = m; // boxing

try

Console.WriteLine("Value of m is:" + a);

object n = 20;

int b = (int)n; // attempt to unbox

Console.WriteLine("Value of n is:" + b);

System.Console.WriteLine("Unboxing OK.");

Console.ReadLine();

catch (System.InvalidCastException e)

10
System.Console.WriteLine("Error: Incorrect unboxing." + e.Message);

Output

11
Exno:5

WRITE A PROGRAM IN C# TO IMPLEMENT STACK OPERATIONS

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace stack

class Program

static void Main(string[] args)

int top = -1;

int[] s = new int[10];

Console.WriteLine("Enter The Size of The Stack");

int MAX = int.Parse(Console.ReadLine());

while (true)

Console.WriteLine("1.Push");

Console.WriteLine("2.Pop");

Console.WriteLine("3.Display");

Console.WriteLine("4.Exit");

Console.WriteLine("Enter your choice :");

int ch = int.Parse(Console.ReadLine());

switch (ch)

12
{

case 1:

if (top > MAX - 1)

Console.WriteLine("... Stack Overflow ...");

else

Console.WriteLine("Enter the item :");

int n = int.Parse(Console.ReadLine());

s[++top] = n;

break;

case 2:

if (top == -1)

Console.WriteLine(" ... Stack Underflow ...");

else

Console.WriteLine("Popped item :" + s[top--]);

break;

case 3:

if (top == -1)

Console.WriteLine("... Stack underflow ...");

else

Console.WriteLine("Elements in the stack");

13
for (int i = top; i >= 0; i--)

Console.WriteLine(s[i]);

break;

case 4:

return;

default:

Console.WriteLine("Wrong Choice");

break;

Output

14
Exno:6

WRITE A PROGRAM TO DEMONSTRATE OPERATOR OVERLOADING.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace OperatorOverloading

public struct addOpp

private double a;

public addOpp(double a)

this.a = a;

public override string ToString()

return string.Format("{0}", a);

static public addOpp operator +(addOpp lhs, addOpp rhs)

return new addOpp(lhs.a + rhs.a);

class Program

15
{

static void Main(string[] args)

Console.WriteLine("Enter Two Numbers");

addOpp c1 = new addOpp(double.Parse(Console.ReadLine()));

addOpp c2 = new addOpp(double.Parse(Console.ReadLine()));

addOpp c3 = c1 + c2;

Console.WriteLine("First Value is {0}", c1);

Console.WriteLine("Second Value is {0}", c2);

Console.WriteLine("Addition is {0}", c3);

Console.ReadLine();

Output

16
Exno:7

WRITE A PROGRAM IN C# TO FIND THE SECOND LARGEST ELEMENT IN A


SINGLE DIMENSIONAL ARRAY.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Largest

class Program

static void Main(string[] args)

int[] a = new int[10];

int i, j;

Console.WriteLine("Enter the No. of Elements");

int n = int.Parse(Console.ReadLine());

Console.WriteLine("Enter the Elements");

for (i = 0; i < n; i++)

a[i] = int.Parse(Console.ReadLine());

for (i = 0; i < n; i++)

for (j = 0; j < n - 1; j++)

17
if (a[j] < a[j + 1])

int temp = a[j];

a[j] = a[j + 1];

a[j + 1] = temp;

i = 1;

while (a[i] == a[0])

i++;

if (i >= n)

Console.WriteLine("Second Largest Element Does Not Exist");

Console.ReadLine();

else

Console.WriteLine("Second Largest Element is " + a[i]);

Console.ReadLine();

18
Output

19
Exno:8

WRITE A PROGRAM IN C# TO MULTIPLY TO MATRICES USING RECTANGULAR


ARRAYS

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Matrixmul

class MatrixMultiplication

int[,] a;

int[,] b;

int[,] c;

public int m1, n1, m2, n2;

public void ReadMatrix()

Console.WriteLine("\n Size of Matrix 1:");

Console.Write("\n Enter the number of rows in Matrix 1 :");

m1 = int.Parse(Console.ReadLine());

Console.Write("\n Enter the number of columns in Matrix 1 :");

n1 = int.Parse(Console.ReadLine());

a = new int[m1, n1];

Console.WriteLine("\n Size of Matrix 2 :");

Console.Write("\n Enter the number of rows in Matrix 2 :");

20
m2 = int.Parse(Console.ReadLine());

Console.Write("\n Enter the number of columns in Matrix 2 :");

n2 = int.Parse(Console.ReadLine());

b = new int[m2, n2];

if (n1 != m2)

Console.WriteLine("columns of A & Rows of B matrix are not equal");

Console.ReadLine();

Environment.Exit(0);

else

Console.WriteLine("\n Enter the elements of Matrix 1:");

for (int i = 0; i < m1; i++)

for (int j = 0; j < n1; j++)

a[i, j] = int.Parse(Console.ReadLine());

Console.WriteLine("\n Enter the elements of Matrix 2:");

for (int i = 0; i < m2; i++)

for (int j = 0; j < n2; j++)

21
b[i, j] = int.Parse(Console.ReadLine());

}}

public void PrintMatrix() {

Console.WriteLine("\n Matrix 1:");

for (int i = 0; i < m1; i++)

for (int j = 0; j < n1; j++)

Console.Write("\t" + a[i, j]);

Console.WriteLine();

Console.WriteLine("\n Matrix 2:");

for (int i = 0; i < m2; i++)

for (int j = 0; j < n2; j++)

Console.Write("\t" + b[i, j]);

Console.WriteLine();

Console.WriteLine("\n Resultant Matrix after multiplying:");

for (int i = 0; i < m1; i++)

22
{

for (int j = 0; j < n2; j++)

Console.Write("\t" + c[i, j]);

Console.WriteLine();

Console.ReadLine();

public void MultiplyMatrix()

c = new int[m1, n2];

for (int i = 0; i < m1; i++)

for (int j = 0; j < n2; j++)

c[i, j] = 0;

for (int k = 0; k < n1; k++)

c[i, j] = c[i, j] + a[i, k] * b[k, j];

class Program

23
static void Main(string[] args)

MatrixMultiplication MM = new MatrixMultiplication();

MM.ReadMatrix();

MM.MultiplyMatrix();

MM.PrintMatrix();

Output

24
Exno:9

FIND THE SUM OF ALL THE ELEMENTS PRESENT IN 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[][] jarr = new int[3][];

int s = 0;

for (int i = 0; i < 3; i++)

Console.WriteLine("Enter the Size of the Array" +(i + 1));

int n = int.Parse(Console.ReadLine());

jarr[i] = new int[n];

Console.WriteLine("Enter the Values of Array " +(i + 1));

for (int j = 0; j < n; j++)

jarr[i][j] = int.Parse(Console.ReadLine());

s = s + jarr[i][j];

25
}

n = n + 0;

Console.WriteLine("Sum= " + s);

Console.ReadLine();

Output

26
Exno:10

WRITE A PROGRAM TO REVERSE A GIVEN STRING USING C#.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Reverse

class Program

static void Main(string[] args)

Console.WriteLine("Enter the String :");

string a = Console.ReadLine();

int len = a.Length;

Console.Write("The Reverse of String is :");

for (int i = len - 1; i >= 0; i--)

Console.Write(a[i]);

Console.WriteLine();

Console.ReadLine();

27
Output

28
Exno:11

USING TRY, CATCH AND FINALLY BLOCKS WRITE A PROGRAM IN C# TO


DEMONSTRATE ERROR HANDLING.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace errorhandling

class Program

static void Main(string[] args)

int[] a = new int[3];

int n = args.Length;

try

if (n == 0)

int d = 10 / (n);

if (n == 1)

a[4] =6;

29
}

catch (IndexOutOfRangeException e)

Console.WriteLine("Exception"+e);

catch (DivideByZeroException e)

Console.WriteLine("DivideByZeroException"+e);

finally

Console.WriteLine("finally block :: End of Program");

Console.ReadLine();

Output

30
Exno:12

DESIGN A SIMPLE CALCULATOR USING SWITCH STATEMENT IN C#.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Simplecalculator

class Program

static void Main(string[] args)

float a, b;

int ch;

Console.Write("Enter The First No.: ");

a = float.Parse(Console.ReadLine());

Console.Write("\nEnter the Second No.: ");

b = float.Parse(Console.ReadLine());

while (true)

Console.WriteLine("===============================");

Console.WriteLine("1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n5.Moduler
Division\n6.Square\n7.Square Root\n8.Exit");

Console.WriteLine("===============================");

Console.Write("Enter your Choice : ");

31
ch = int.Parse(Console.ReadLine());

switch (ch)

case 1: Console.WriteLine("Addition :" + a + "+" + b + "=" + (a + b));

break;

case 2: Console.WriteLine("Subtraction :" + a + "-" + b + "=" + (a - b));

break;

case 3: Console.WriteLine("Multiplication :" + a + "*" + b + "=" + (a * b));

break;

case 4: Console.WriteLine("Division :" + a + "/" + b + "=" + (a / b));

break;

case 5: Console.WriteLine("Moduler Division:" + a + "%" + b + "=" + (a % b));

break;

case 6: Console.WriteLine("Square(" + a + ") =" + (a * a));

break;

case 7: Console.WriteLine("SquareRoot(" + a + ") =" + Math.Sqrt(a));

break;

default: Console.WriteLine("Invalid Input");

Environment.Exit(0);

break;

32
Output

33
Exno:13

DEMONSTRATE USE OF VIRTUAL AND OVERRIDE KEY WORDS IN C# WITH A


SIMPLE PROGRAM

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace VirtualKey

class person

protected string fname;

protected string lname;

public person(string fn, string ln)

fname = fn;

lname = ln;

public virtual void display()

Console.WriteLine("Person :" + fname + " " + lname);

class emp : person

34
public ushort year;

public emp(string fn, string ln, ushort yr)

: base(fn, ln)

year = yr;

public override void display()

Console.WriteLine("Employee :"+fname+" "+lname+" "+year);

class worker : person

public String company;

public worker(string fn, string ln, string c):base(fn, ln)

company=c;

public override void display()

Console.WriteLine("Worker :" + fname + " " + lname + " " +company);

class Program

35
static void Main(string[] args)

Console.WriteLine("\n\n*** VIRTUAL AND OVERRIDE KEYWORDS DEMO ***");

person p1 = new person("RAM", "KUMAR");

person p2 = new emp("RAM", "KUMAR",2012);

person p3 = new worker("RAM", "KUMAR","ABC TECH SOLS");

p1.display();

p2.display();

p3.display();

Console.ReadLine();

Output

36
Exno:14

IMPLEMENT LINKED LISTS IN C# USING THE EXISTING COLLECTIONS NAME


SPACE.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace linkedlist

class Program

static LinkedList<int> ll = new LinkedList<int>();

static LinkedListNode<int> node;

static void Main(string[] args)

Console.WriteLine(" LINKED LIST DEMO");

int ch, x;

Console.WriteLine("Linked List Operations");

Console.WriteLine("1.AddFirst\n2.AddLast\n3.RemoveFirst\n4.RemoveLast\n5.Remove
Specified\n6.Display\n7..Exit");

while (true)

Console.Write("Enter your Choice : ");

ch = int.Parse(Console.ReadLine());

switch (ch) {

case 1: Console.Write("Enter the Element to AddFirst : ");

37
x = int.Parse(Console.ReadLine());

ll.AddFirst(x);

display();

break;

case 2: Console.WriteLine("Enter the Element to AddLast : ");

x = int.Parse(Console.ReadLine());

ll.AddLast(x);

display();

break;

case 3:

if (ll.Count == 0)

Console.WriteLine("Nothing to Delete...!!!");

break;

else

Console.WriteLine("First Element Removed Successfully");

ll.RemoveFirst();

display();

break;

case 4: if (ll.Count == 0)

Console.WriteLine("Nothing to Delete...!!!");

38
break;

else

Console.WriteLine("Last Element Removed Successfully");

ll.RemoveLast();

display();

break;

case 5: if (ll.Count == 0)

Console.WriteLine("Nothing to Delete...!!!");

break;

else

Console.WriteLine("Enter the Element to Remove");

x = int.Parse(Console.ReadLine());

bool b=ll.Remove(x);

if (b == true)

Console.WriteLine("Element Removed Successfully");

display();

break;

39
else {

Console.WriteLine("Specified Node Does not Exist");

break;

case 6:

display();

break;

default: Environment.Exit(0);

break;

public static void display()

if (ll.Count == 0)

Console.WriteLine("Nothing to Display...!!!");

else

Console.Write("Elements in the List are:");

for (node = ll.First; node != null; node=node.Next)

Console.Write(node.Value+" ");

Console.WriteLine();

40
}

Output

41
Exno:15

WRITE A PROGRAM TO DEMONSTRATE ABSTRACT CLASS AND ABSTRACT


METHODS IN C#.

using System;

namespace TryCatch {

abstract class person {

protected string fname;

protected string lname;

public person(string fn, string ln) {

fname = fn;

lname = ln;

public abstract void display() {

Console.WriteLine("Person :" + fname + " " + lname);

class emp : person

public ushort year;

public emp(string fn, string ln, ushort yr)

: base(fn, ln) {

year = yr;

public override void display() {

Console.WriteLine("Employee :" + fname + " " + lname + " " + year);

42
}

class worker : person {

public String company;

public worker(string fn, string ln, string c)

: base(fn, ln)

company = c;

public override void display() {

Console.WriteLine("Worker :" + fname + " " + lname + " " + company);

class Program

static void Main(string[] args) {

Console.WriteLine("**ABSTRACT CLASS AND ABSTRACT METHODS DEMO **");

person p2 = new emp("RAM", "KUMAR", 2012);

person p3 = new worker("RAM", "KUMAR", "ABC TECH SOLS");

p2.display();

p3.display();

Console.ReadLine();

43
Output

44
Exno:16

WRITE A PROGRAM IN C# TO BUILD A CLASS WHICH IMPLEMENTS AN


INTERFACE WHICH ALREADY EXISTS.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Infc

class Point:ICloneable

public int x, y;

public Point(int x, int y) {

this.x = x;

this.y = y;

public object Clone() {

return new Point(this.x, this.y);

public override string ToString()

return string.Format("X= {0}; y={1}", x, y);

class Porgram{

45
static void Main(string[] args)

Point p1 =new Point(10,10);

Point p2 =(Point)p1.Clone();

p2.x = 20;

Console.WriteLine(p1);

Console.WriteLine(p2);

Console.Read();

Output

46
Exno:17

WRITE A PROGRAM TO ILLUSTRATE THE USE OF DIFFERENT PROPERTIES IN


C#.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Properties

class point

int getx, gety;

public int x

get { return getx; }

set { getx = value; }

public int y

get { return gety; }

set { gety = value; }

class Program

47
static void Main(string[] args)

point start = new point();

point end = new point();

start.x = 10;

start.y = 20;

end.x = 100;

end.y = 200;

Console.Write("\npoint 1 :" + start.x + " " + end.x);

Console.Write("\npoint 2 :" + start.y + " " + end.y);

Console.ReadLine();

Output

48
Exno:18

DEMONSTRATE ARRAYS OF INTERFACE TYPES WITH A C# PROGRAM.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace IntrDemo {

public interface Shape {

void area();

public class Circle : Shape {

public void area() {

Console.WriteLine("*** Calculating Area of Circle ***");

Console.Write("Enter the Radius:");

float r = float.Parse(Console.ReadLine());

Console.WriteLine("Area of Circle = " + (3.142 * r * r));

public class Square : Shape {

public void area() {

Console.WriteLine("*** Calculating Area of Square ***");

Console.Write("Enter the Length:");

float side = float.Parse(Console.ReadLine());

Console.WriteLine("Area of Square = " + (side * side));

49
}

class Program

static void Main(string[] args)

Console.WriteLine("*** Arrays of Inerface Demo ***");

Shape[] s = new Shape[2];

s[0] = new Circle();

s[1] = new Square();

for (int i = 0; i < s.Length; i++)

s[i].area();

Console.ReadLine();

Output

50

Vous aimerez peut-être aussi