C#.NET Lab Manual PDF
C#.NET Lab Manual PDF
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();
}
}
}
}
OUTPUT:
SOURCE CODE:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Lab2
{
class Program
{
static void Main(string[] args)
{
OUTPUT:
SOURCE CODE:
using System;
namespace QuadRoots
{
class Program {
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();
}
}
}
OUTPUT:
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
{
}
catch (System.InvalidCastException e)
{
System.Console.WriteLine("Error: Incorrect unboxing."+e.Message);
}
}
}
}
OUTPUT:
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");
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;
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;
}
}
}
}
}
OUTPUT:
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);
}
}
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();
}
}
OUTPUT:
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++;
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:
SOURCE CODE:
using System;
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 :");
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++)
{
b[i, j] = int.Parse(Console.ReadLine());
}
}
} }
OUTPUT:
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();
}
}
}
OUTPUT:
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();
}
}
}
OUTPUT:
SOURCE CODE:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TryCatch
{
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;
}
}
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:
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.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;
}
}
}
}
}
OUTPUT:
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);
}
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
{
OUTPUT:
SOURCE CODE:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LinkList
{
class Program
{
static LinkedList<int> ll = new LinkedList<int>();
static LinkedListNode<int> node;
static void Main(string[] args)
{
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) {
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...!!!");
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;
}
else {
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:");
}
}
}
OUTPUT:
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() {
Console.WriteLine("Person :" + fname + " " + lname);
}
}
}
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:
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() {
}
class Porgram{
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:
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)
{
OUTPUT:
SOURCE CODE:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IntrDemo {
}
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();
}}
OUTPUT: