Professional Documents
Culture Documents
NET Lab Manual
NET Lab Manual
NET Lab Manual
***************************************
Enter the number
***************************************
12121
***************************************
The reverse of the number is: 12121
***************************************
***************************************
The number is Palindrome
***************************************
***************************************
Enter the number
***************************************
1234
***************************************
The reverse of the number is: 4321
***************************************
***************************************
The number is not a Palindrome
***************************************
2.
if (args.Length == 0)
{
Console.WriteLine("*********************************************");
Console.WriteLine("There is no Command Line Argument
defined");
Console.WriteLine("*********************************************");
Console.ReadLine();
return;
}
//Finding
argsValue
sqrtValue
//Display
Console.WriteLine("*********************************************");
Console.WriteLine("The Squareroot of command line argument is:
{0}",sqrtValue);
Console.WriteLine("*********************************************");
Console.ReadLine();
}
}
}
Open the Visual Studio 2008 Command Prompt
. Click the Start button & Navigate to All Programs.
. Navigate to Microsoft Visual Studio 2008.
. Navigate to Visual Studio Tools.
.Navigate to Visual Studio 2008 Command Prompt.
. Navigate to the folder u have created for storing C#.NET files.(Example folder: cd: MyExamples )
You will get Visual Studio 2008 Command Prompt.
Setting Environment for Microsoft Visual Studio 2008 x86 tools.
C:\Program Files\ Microsoft Visual Studio 9.0\VC>
.
C:\Program Files\ Microsoft Visual Studio 9.0\VC>cd MyExamples
C:\Program Files\ Microsoft Visual Studio 9.0\VC\MyExamples>
C:\Program Files\ Microsoft Visual Studio 9.0\VC\MyExamples>csc CommandLineArgs.cs
Microsoft <R> Visual C# 2008 Compiler version 3.5.31022.8
For Microsoft <R> .Net Framework version 3.5
Copyright <R> Microsoft Corporation . All rights reserved.
C:\Program Files\ Microsoft Visual Studio 9.0\VC\MyExamples> CommandLineArgs
***************************************
There is no Command Line Arguments defined
***************************************
C:\Program Files\ Microsoft Visual Studio 9.0\VC\MyExamples> CommandLineArgs 85
***************************************
The Squareroot of Command Line Argument is: 9.21954445729289
***************************************
3.
using System;
namespace LabProgram3
{
class QuadraticEquation
{
public static void Main(string[] args)
{
int A, B, C;
double disc, denom, X1, X2;
Console.WriteLine("Enter the value of A,B, & C");
A = int.Parse(Console.ReadLine());
B = int.Parse(Console.ReadLine());
C = int.Parse(Console.ReadLine());
disc = (B * B) - (4 * A * C);
denom = (2 * A);
if (disc > 0)
{
Console.WriteLine("The Roots are Real roots...");
X1 = (-B / denom) + (Math.Sqrt(disc) / denom);
X2 = (-B / denom) - (Math.Sqrt(disc) / denom);
Console.WriteLine("The Roots are ......:{0} and{1}", X1, X2);
Console.ReadLine();
}
else
{
if (disc == 0)
{
Console.WriteLine("The Roots are Repeated roots...");
X1 = -B / denom;
Console.WriteLine("The Root is.....:{0}", X1);
Console.ReadLine();
}
else
{
Console.WriteLine("The Roots are Imaginary roots...\n");
X1 = -B / denom;
X2 = ((Math.Sqrt((4 * A * C) - (B * B))) / denom);
Console.WriteLine("The Root one.......: {0} +i{1}", X1,
X2);
4.
using
using
using
using
namespace LabProgram4
{
class BoxUnbox
{
public static void Main(string[] args)
{
int num;
Console.WriteLine("Enter the number");
num = int.Parse(Console.ReadLine());
Object obj = num;
Console.WriteLine("Value in num is: {0}",num);
Console.WriteLine("Value in Object is: {0}",obj);
int n;
n = (int)obj;
Console.WriteLine("Value in n is: {0}", +n);
Console.ReadLine();
}
}
}
using System;
namespace LabProgram5
{
class StackOperation
{
public static void Main(string[] args)
{
int top = -1, num, choice, max, rpt = 1;
int [] stack = new int[20];
Console.WriteLine("Enter the Maximum Limit");
max = int.Parse(Console.ReadLine());
while (rpt != 0)
{
Console.WriteLine("Stack Operation:");
Console.WriteLine("1: PUSH");
Console.WriteLine("2: POP");
Console.WriteLine("3: DISPLAY");
Console.WriteLine("0: EXIT");
choice = int.Parse(Console.ReadLine());
switch (choice)
{
case 1:
if (top == (max - 1))
{
Console.WriteLine("Stack is full");
}
else
{
6.
using System.Linq;
using System.Text;
namespace LabProgram6
{
class OpertorOverlaoding
{
public struct Complex
{
public int real;
public int imaginary;
public Complex (int real, int imaginary)
{
this.real = real;
this.imaginary = imaginary;
}
//Declare which operator to overload (+) the types that can
be added (two Complex Objects) and the return type
// (Complex c1, Complex c2)
public static Complex operator +(Complex c1, Complex c2)
{
return new Complex(c1.real + c2.real, c1.imaginary +
c2.imaginary);
}
// Override the tostring method to display an complex number in
suitable format
public override string
ToString()
{
return(string.Format("{0} +{1}",real,imaginary));
}
}
public static void Main()
{
Complex num1 = new Complex(2,3);
Complex num2 = new Complex(3,4);
// Add the two complex objects ( num1 and num2) through the overloaded
plus operator
Complex sum = num1 + num2;
// Print the numbers & the sum using the overridden tostring method.
Console.WriteLine("First Complex number is: {0}", num1);
Console.WriteLine("Second Complex number is: {0}", num2);
Console.WriteLine("----------------------------------");
Console.WriteLine("The sum of the two number is: {0}", sum);
Console.ReadLine();
}
}
}
7.
Write a Program in C# to find the second largest element in a single dimensional array.
using System;
namespace Labprogram7a
{
class Biggest
{
int[] a = { 1, 2, 3, 4 };
public void ReadElement()
{
Console.WriteLine("\n Enter the elements");
Console.WriteLine("------------------------------");
for (int i = 0; i < a.Length; i++)
{
a[i] = int.Parse(Console.ReadLine());
}
Console.WriteLine("------------------------------");
}
public void PrintElement()
{
Console.WriteLine("\n The elements are:");
Console.WriteLine("------------------------------");
for (int i = 0; i < a.Length; i++)
{
Console.WriteLine(a[i]);
}
Console.WriteLine("------------------------------");
}
public void BigElement()
{
int big, secbig;
big = secbig = 0;
for (int i = 0; i < a.Length; i++)
{
if (big < a[i])
{
secbig = big;
big = a[i];
}
}
Console.WriteLine("first biggest element is: {0} and second
biggest element is: {1}", big, secbig);
Console.ReadLine();
}
}
}
namespace Labprogram7a
{
class Big
{
public static void Main(string[] args)
{
Biggest MM = new Biggest();
MM.ReadElement();
MM.PrintElement();
MM.BigElement();
}
}
}
8.
using System;
namespace LabProgram8
{
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 Matrix 1 :");
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++)
{
a[i, j] = int.Parse(Console.ReadLine());
}
}
Console.WriteLine("\n Size of Matrix 2 :");
Console.Write("\n Enter the number of rows in Matrix 2 :");
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++)
{
Console.Write("\t" + a[i, j]);
}
Console.WriteLine();
}
Console.WriteLine("\n Matrix 2:");
using System;
namespace LabProgram8
{
class Matrices
{
9.
Find the sum of all the elements present in a jagged array of 3 inner arrays.
using System;
namespace LabProgram9
{
class JagArray
{
public static void Main(string[] args)
{
int[][] myJagArray = new int[3][];
System;
System.Collections.Generic;
System.Linq;
System.Text;
namespace LabProgram12
{
class SimpleCalculator
{
public static void Main(string[] args)
{
double a, b, rpt = 1;
int choice;
while (rpt != 0)
{
Console.WriteLine("Select the operation:");
Console.WriteLine("1: Addition");
Console.WriteLine("2: Subtraction");
Console.WriteLine("3: Multiplication");
Console.WriteLine("4: Division");
Console.WriteLine("0: Exit");
Console.WriteLine("Enter your choice:");
Console.WriteLine("--------------------");
choice = int.Parse(Console.ReadLine());
switch (choice)
{
case 1:
Console.WriteLine("Enter the two numbers:");
a = double.Parse(Console.ReadLine());
b = double.Parse(Console.ReadLine());
Console.WriteLine("The result of addition is:" + (a +
b));
Console.WriteLine();
break;
case 2:
Console.WriteLine("Enter the two numbers:");
a = double.Parse(Console.ReadLine());
b = double.Parse(Console.ReadLine());
Console.WriteLine("The result of subtraction is:" +
(a - b));
break;
case 3:
Console.WriteLine("Enter the two numbers:");
a = double.Parse(Console.ReadLine());
b = double.Parse(Console.ReadLine());
Console.WriteLine("The result of multiplication is:"
+ (a * b));
break;
case 4:
Console.WriteLine("Enter the two numbers:");
a = double.Parse(Console.ReadLine());
b = double.Parse(Console.ReadLine());
if (b == 0)
{
Console.WriteLine("Division is not possible");
}
else
{
Console.WriteLine("The result of division is:" +
(a / b));
}
break;
case 0:
rpt = 0;
break;
default:
Console.WriteLine("Invalid selection:");
break;
}
}
Console.ReadLine();
}
}
}
2. Demonstrate Use of Virtual and override key words in C# with a simple program
using System;
using System.Collections.Generic;
namespace LabProgram13
{
public class Customer
{
public virtual void CustomerType()
{
Console.WriteLine("I am a Customer");
}
}
public class CorporateCustomer : Customer
{
public override void CustomerType()
{
Console.WriteLine("I am a Corporate Customer");
}
}
public class PersonalComputer : Customer
{
public override void CustomerType()
{
Console.WriteLine("I am a Personal Customer");
}
}
class Program
{
static void Main(string[] args)
{
Customer[] c = new Customer[3];
c[0] = new PersonalComputer();
c[1] = new CorporateCustomer();
c[2] = new Customer();
foreach (Customer CustomerObject in c)
{
CustomerObject.CustomerType();
}
Console.ReadLine();
}
}
}
3.
using
using
using
using
namespace LabProgram14
{
class Linkedlist
{
static void Main(string[] args)
{
LinkedList<int> obj = new LinkedList<int>();
LinkedListNode<int> f = null;
obj.AddFirst(10);
obj.AddLast(50);
Console.WriteLine("The elements in the linked kist are:");
foreach(int i in obj)
Console.WriteLine(i);
obj.RemoveFirst();
Console.WriteLine("The elements in the linked list after
deleting are:");
foreach (int i in obj)
Console.WriteLine(i);
Console.ReadLine();
}
}
}
System;
System.Collections.Generic;
System.Linq;
System.Text;
namespace LabProgram15
{
abstract class Shape
{
abstract public void show();
}
}
namespace LabProgram15
{
class Circle : Shape
{
public override void show()
{
Console.WriteLine("We are in Circle");
}
}
}
namespace LabProgram15
{
class Triangle : Shape
{
public override void show()
{
Console.WriteLine("We are in Triangle");
}
}
}
namespace LabProgram15
{
class Abstractclassmethod
{
static void Main(string[] args)
{
Circle c = new Circle();
Triangle t = new Triangle();
c.show();
t.show();
Console.ReadLine();
}
}
}
5. Write a program in C# to build a class which implements an interface which is already existing.
using
using
using
using
System;
System.Collections.Generic;
System.Linq;
System.Text;
namespace LabProgram16
{
interface Shape
{
void Show();
}
}
using
using
using
using
System;
System.Collections.Generic;
System.Linq;
System.Text;
namespace LabProgram16
{
class Triangle:Shape
{
#region Shape Members
void Shape.Show()
{
Console.WriteLine("I am printing from a Triangle");
}
#endregion
}
}
using
using
using
using
System;
System.Collections.Generic;
System.Linq;
System.Text;
namespace LabProgram16
{
class Circle:Shape
{
#region Shape Members
void Shape.Show()
{
Console.WriteLine("I am printing from Circle");
}
#endregion
}
}
using
using
using
using
System;
System.Collections.Generic;
System.Linq;
System.Text;
namespace LabProgram16
{
class InterfaceExample
{
static void Main(string[] args)
{
Circle c = new Circle();
Triangle t = new Triangle();
Shape s;
s = c;
s.Show();
s = t;
s.Show();
Console.ReadLine();
}
}
}
System;
System.Collections.Generic;
System.Linq;
System.Text;
namespace LabProgram17
{
class Employee
{
private string ename, eid;
int eage;
double esalary;
public Employee(string eid)
{
this.eid = eid;
}
public string EmployeeName
{
get
{
return ename;
}
set
{
ename = value;
}
}
public string Employeeid
{
get
{
return eid;
}
}
public int EmployeeAge
{
get
{
return eage;
}
set
{
eage = value;
}
}
public double EmployeeSalary
{
get
{
return esalary;
}
set
{
esalary = value;
}
}
}
}
System;
System.Collections.Generic;
System.Linq;
System.Text;
namespace LabProgram18
{
interface Inter
{
void info();
}
}
using
using
using
using
System;
System.Collections.Generic;
System.Linq;
System.Text;
namespace LabProgram18
{
class Student:Inter
{
private string name, usn;
public Student(string name, string usn)
{
this.name = name;
this.usn = usn;
}
public void info()
{
System;
System.Collections.Generic;
System.Linq;
System.Text;
namespace LabProgram18
{
class Emp:Inter
{
private string name, id;
public Emp(string name, string id)
{
this.name = name;
this.id = id;
}
public void info()
{
Console.WriteLine("Name is: {0}\n ID is:{1}", name, id);
}
}
}
using
using
using
using
System;
System.Collections.Generic;
System.Linq;
System.Text;
namespace LabProgram18
{
class ArrayInterfaceExample
{
static void Main(string[] args)
{
Inter[] obj = { new Student("Raju", "10MCA01"), new Emp("Rahul",
"e100"), new Student("Suresh", "10MCA02") };
foreach (Inter s in obj)
Console.WriteLine(s);
Console.ReadLine();
}
}
}