Anda di halaman 1dari 37

.

NET LABORATORY 10MCA57


1. WRITE A PROGRAM IN C# TO CHECK WHETHER A NUMBER IS
PALINDROME OR NOT.
SOURCE CODE:
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;
}
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();
}
}
}
}

Page 1

.NET LABORATORY 10MCA57


OUTPUT

Page 2

.NET LABORATORY 10MCA57


2. WRITE A PROGRAM IN C# TO DEMONSTRATE COMMAND LINE
ARGUMENTS PROCESSING
SOURCE CODE:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Lab2
{
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();
}
}
}

Page 3

.NET LABORATORY 10MCA57


OUTPUT

Page 4

.NET LABORATORY 10MCA57


3. WRITE A PROGRAM IN C# TO FIND THE ROOTS OF QUADRATIC
EQUATION.
SOURCE CODE:
using System;
namespace QuadRoots
{
class Program {
public static void Main() {
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
{
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();
}
}
}

Page 5

.NET LABORATORY 10MCA57


OUTPUT

Page 6

.NET LABORATORY 10MCA57


4. WRITE A PROGRAM IN C# TO DEMONSTRATE BOXING AND UNBOXING.
SOURCE CODE:
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("Boxing and unboxing done.");
Console.ReadLine();
}
catch (System.InvalidCastException e)
{
System.Console.WriteLine("Error: Incorrect unboxing."+e.Message);
}
}
}

OUTPUT

Page 7

.NET LABORATORY 10MCA57


5. WRITE A PROGRAM IN C# TO IMPLEMENT STACK OPERATIONS
SOURCE CODE:
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)
{
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;

Page 8

.NET LABORATORY 10MCA57


case 3:
if (top == -1)
Console.WriteLine("... Stack underflow ...");
else
{
Console.WriteLine("Elements in the stack"); for
(int i = top; i >= 0; i--)
Console.WriteLine(s[i]);
}

break;
case 4:
return;
default:
Console.WriteLine("Wrong Choice");
break;

}
}

Page 9

.NET LABORATORY 10MCA57


OUTPUT

Page 10

.NET LABORATORY 10MCA57


6. A PROGRAM TO DEMONSTRATE OPERATOR OVERLOADING.
SOURCE CODE:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Overload
{
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
{
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();
}
}
}

Page 11

.NET LABORATORY 10MCA57


OUTPUT

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


IN A SINGLE DIMENSIONAL ARRAY.
SOURCE CODE:
using System;
using System.Collections.Generic;
using System.Text;
namespace secondLarge
{
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++)
{
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++;

Page 12

.NET LABORATORY 10MCA57


if (i >= n)
{
Console.WriteLine("Second Largest Element Does Not Exist");
Console.ReadLine();
}
else {
Console.WriteLine("Second Largest Element is "+a[i]);
Console.ReadLine();
}
}
}
}

OUTPUT

Page 13

.NET LABORATORY 10MCA57


8. WRITE A PROGRAM IN C# TO MULTIPLY TO MATRICES USING
RECTANGULAR ARRAYS
SOURCE CODE:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplicationavi8
{
class MatrixMultiplication
{
int[,] a;
int[,] b;
int[,] c;
public void ReadMatrix()
{
Console.WriteLine("\n Size of Matrix 1:");
Console.Write("\n Enter the number of rows in Matrix 1 :");
int m = int.Parse(Console.ReadLine());
Console.Write("\n Enter the number of columns in Matrix1:");
int n = int.Parse(Console.ReadLine());
a = new int[m, n];
Console.WriteLine("\n Enter the elements of Matrix 1:");
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)

Page 14

.NET LABORATORY 10MCA57


{
a[i, j] = int.Parse(Console.ReadLine());
}
}
Console.WriteLine("\n Size of Matrix 2 :");
Console.Write("\n Enter the number of rows in Matrix2:");
m = int.Parse(Console.ReadLine());
Console.Write("\n Enter the number of columns in Matrix 2 :");
n = int.Parse(Console.ReadLine());
b = new int[m, n];
Console.WriteLine("\n Enter the elements of Matrix 2:");
for (int i = 0; i < b.GetLength(0); i++)
{
for (int j = 0; j < b.GetLength(1); j++)
{
b[i, j] = int.Parse(Console.ReadLine());
}
}
}

public void PrintMatrix()


{
Console.WriteLine("\n Matrix 1:");
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{

Page 15

.NET LABORATORY 10MCA57


Console.Write("\t" + a[i, j]);
}
Console.WriteLine();
}
Console.WriteLine("\n Matrix 2:");
for (int i = 0; i < b.GetLength(0); i++)
{
for (int j = 0; j < b.GetLength(1); j++)
{
Console.Write("\t" + b[i, j]);
}
Console.WriteLine();
}
Console.WriteLine("\n Resultant Matrix after multiplying Matrix 1 & Matrix 2:");
for (int i = 0; i < c.GetLength(0); i++)
{
for (int j = 0; j < c.GetLength(1); j++)
{
Console.Write("\t" + c[i, j]);
}
Console.WriteLine();
}
}
public void MultiplyMatrix()
{
if (a.GetLength(1) == b.GetLength(0))
{

Page 16

.NET LABORATORY 10MCA57


c = new int[a.GetLength(0), b.GetLength(1)];
for (int i = 0; i < c.GetLength(0); i++)
{
for (int j = 0; j < c.GetLength(1); j++)
{
c[i, j] = 0;
for (int k = 0; k < a.GetLength(1); k++)
c[i, j] = c[i, j] + a[i, k] * b[k, j];
}
}
}
else
{
Console.WriteLine("\n Number of columns in Matrix1 is not
rows in Matrix2.");

equal to Number of

Console.WriteLine("\n Therefore Multiplication of Matrix1 with Matrix2 is not


possible");
Console.ReadLine();
Environment.Exit(-1);
}
}
}
class Matrices
{
public static void Main()
{
MatrixMultiplication MM = new MatrixMultiplication();
MM.ReadMatrix();

Page 17

.NET LABORATORY 10MCA57


MM.MultiplyMatrix();
MM.PrintMatrix();
Console.ReadLine();
}
}
}

OUTPUT

Page 18

.NET LABORATORY 10MCA57


9. FIND THE SUM OF ALL THE ELEMENTS PRESENT IN A JAGGED
ARRAY OF 3 INNER ARRAYS
SOURCE CODE:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace JagArray
{
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];
}
n = n + 0;

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

Page 19

.NET LABORATORY 10MCA57

OUTPUT

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


SOURCE CODE:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ReverseStr
{
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();
}
}
}

Page 20

.NET LABORATORY 10MCA57


OUTPUT

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


DEMONSTRATE ERROR HANDLING.
SOURCE CODE:
using
using
using
using

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

namespace ConsoleApplicationavi11
{
class Lab11
{
public static void Main()
{
try
{
int zero = 0;
Console.WriteLine("In try block: attempting division by zero");
int myInt = 1 / zero;
Console.WriteLine("You never see this message!");
}
catch
{
Console.WriteLine("In catch block: an exception was thrown");
}
finally
{
Console.WriteLine("In finally block: do any cleaning up here");
}
Console.ReadLine();
}
}
}

Page 21

.NET LABORATORY 10MCA57


OUTPUT

Page 22

.NET LABORATORY 10MCA57


12. DESIGN A SIMPLE CALCULATOR USING SWITCH STATEMENT IN C#.
SOURCE CODE:
using System;
using System.Collections.Generic;
using System.Text;
namespace SimpleCalc {
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 : ");
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;
}
}
}
}
}

Page 23

.NET LABORATORY 10MCA57


OUTPUT

Page 24

.NET LABORATORY 10MCA57


13. DEMONSTRATE USE OF VIRTUAL AND OVERRIDE KEY WORDS IN C#
WITH A SIMPLE PROGRAM
SOURCE CODE:
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
{
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;
}

Page 25

.NET LABORATORY 10MCA57


public override void display()
{
Console.WriteLine("Worker :" + fname + " " + lname + " " +company);
}

}
class Program
{
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

Page 26

.NET LABORATORY 10MCA57


14. IMPLEMENT LINKED LISTS IN C# USING THE EXISTING COLLECTIONS
NAME SPACE.
SOURCE CODE:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace p8
{
class LinkLst
{
public static void Main()
{
LinkedList<int> list = new LinkedList<int>();
LinkedListNode<int> node;
int opt, n, ele;

Console.WriteLine("...........Implement the Link List..........");


for (; ; )
{
Console.WriteLine("\n1.Inser ferant \n2.Insert rear\n");
Console.WriteLine("3.Delete ferent \n4.Delete Rear\n");
Console.WriteLine("5.insert Before a value \n6.Insert After the value\n");
Console.WriteLine("\n7.Delete the given value \n8.disp \n9.exit\n");
Console.WriteLine("\n enter the option u want:- ");
opt = int.Parse(Console.ReadLine());
switch (opt)
{
case 1: Console.WriteLine("Enter the element ");
n = int.Parse(Console.ReadLine());
list.AddFirst(n);
Page 27

.NET LABORATORY 10MCA57


break;
case 2: Console.WriteLine("\n Enter the element");
n = int.Parse(Console.ReadLine());
list.AddLast(n);
break;
case 3: list.RemoveFirst();
break;
case 4: list.RemoveLast();
break;
case 5: Console.WriteLine("\\n Enter the existing element");
ele = int.Parse(Console.ReadLine());
node = list.Find(ele);
if (node != null)
{
Console.WriteLine("enter the element");
n = int.Parse(Console.ReadLine());
list.AddBefore(node, n);
}
else
Console.WriteLine("\n Invalid operation");
break;
case 6: Console.WriteLine("\n Enter the exiisting elemment");
ele = int.Parse(Console.ReadLine());
node = list.Find(ele);
if (node != null)
{
Console.WriteLine("\n Enter the element");
n = int.Parse(Console.ReadLine());
list.AddAfter(node, n);
}
else
Page 28

.NET LABORATORY 10MCA57


Console.WriteLine("\n Invalid operation");
break;
case 7: Console.WriteLine("\n enter the element to be deleted");
ele = int.Parse(Console.ReadLine());
node = list.Find(ele);
if (node == null)
list.Remove(node);
else
Console.WriteLine("\n element does not exist");
break;
case 8: disp(list);
break;
case 9:
default: return;
}
}
}
public static void disp(LinkedList<int> lst)
{
if (lst.Count == 0)
{
Console.WriteLine("\n empty");
return;
}
else
Console.WriteLine("\n the elements is");
foreach (int i in lst)
Console.WriteLine("\n {0}", i);
}
}
}

Page 29

.NET LABORATORY 10MCA57


OUTPUT

Page 30

.NET LABORATORY 10MCA57


15. WRITE A PROGRAM TO DEMONSTRATE ABSTRACT CLASS AND
ABSTRACT METHODS IN C#.

SOURCE CODE:
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() ;
}

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);
}
}
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();
}
}

OUTPUT
Page 31

.NET LABORATORY 10MCA57

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


INTERFACE WHICH ALREADY EXISTS.
SOURCE CODE:
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{
static void Main(string[] args)
{
Point p1 =new Point(10,10);
Point p2 =(Point)p1.Clone();
p2.x = 20;

Page 32

.NET LABORATORY 10MCA57


Console.WriteLine(p1);
Console.WriteLine(p2);
Console.Read();
}
}
}

OUTPUT

Page 33

.NET LABORATORY 10MCA57


17. WRITE A PROGRAM TO ILLUSTRATE THE USE OF DIFFERENT
PROPERTIES IN C#.
SOURCE CODE:
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
{
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();

}
}

Page 34

.NET LABORATORY 10MCA57


OUTPUT

Page 35

.NET LABORATORY 10MCA57


18. DEMONSTRATE ARRAYS OF INTERFACE TYPES WITH A C#
PROGRAM.
SOURCE CODE:
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));
}
}
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();
}
}
}}

Page 36

.NET LABORATORY 10MCA57


OUTPUT

Page 37

Anda mungkin juga menyukai