NET Lab Manual

You might also like

You are on page 1of 32

1.

Write a Program in C# to Check whether a number is Palindrome or not.

//Program file name is: Palindrome.cs


using System;
namespace LabProgram1
{
class Palindrome
{
public static void Main(string[] args)
{
//Declaring the variables
int num;
int intial_num;
int digit;
int reverse_num = 0;
//Prompt the user for an input
Console.WriteLine("***************************************");
Console.WriteLine("Enter the number");
Console.WriteLine("***************************************");
Console.WriteLine("\r\n");
//Read the user input
intial_num = int.Parse(Console.ReadLine());
num = intial_num;
do
{
//set digit to the users number mod 10
digit = intial_num % 10;
// reverse the number by multiplying it by 10 & then add
digit value to it
reverse_num = reverse_num * 10 + digit;
intial_num = intial_num / 10;
}
while (intial_num != 0);
// Display the reversed number
Console.WriteLine("***************************************");
Console.WriteLine(" The reverse of the number is: {0}",
reverse_num);
Console.WriteLine("***************************************");
Console.WriteLine("\r\n");
//Check whether the number is palindrome or not
if(num == reverse_num)
{
Console.WriteLine("***************************************");
Console.WriteLine(" The number is palindrome");
Console.WriteLine("***************************************");
Console.WriteLine("\r\n");
Console.ReadLine();
}
else
{
Console.WriteLine("***************************************");

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


Console.WriteLine("***************************************");
Console.ReadLine();
}
}
}
}

***************************************
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.

Write a Program in C# to demonstrate Command line arguments Processing.

//Program file name is CommandLineArgs.cs


using System;
namespace LabProgram2
{
class CommandLineArgs
{
public static void Main(string[] args)
{
//Declare variables
double argsValue = 0.0;
double sqrtValue = 0.0;
//Check the length of Command Line Argument

if (args.Length == 0)
{
Console.WriteLine("*********************************************");
Console.WriteLine("There is no Command Line Argument
defined");
Console.WriteLine("*********************************************");
Console.ReadLine();
return;
}
//Finding
argsValue
sqrtValue
//Display

squareroot of a number using Math object


= double.Parse(args[0].ToString());
= Math.Sqrt(argsValue);
the squareroot value at the command prompt

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.

Write a Program in C# to find the roots of Quadratic Equation.

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

Console.WriteLine("The Roots are.......: {0} -i{1}", X1,


X2);
Console.ReadLine();
}
}
}
}
}

C:\Program Files\Microsoft Visual Studio 9.0\VC>


Enter the value of A,B, & C
4
10
2
The Roots are Real roots...
The Roots are ......:-0.219223593595585 and-2.28077640640442

Enter the value of A,B, & C


10
4
6
The Roots are Imaginary roots...
The Root one.......: -0.2 +i0.748331477354788
The Roots are.......: -0.2 -i0.748331477354788
Enter the value of A,B, & C
2
8
8
The Roots are Repeated roots...
The Root is.....:-2

4.
using
using
using
using

Write a Program in C# to demonstrate boxing and unBoxing.


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

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

Enter the number


5
Value in num is: 5
Value in Object is: 5
Value in n is: 5
Enter the number
10
Value in num is: 10
Value in Object is: 10
Value in n is: 10
5.

Write a Program in C# to implement Stack operations.

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
{

Console.WriteLine("Enter the element to be


inserted");
num = int.Parse(Console.ReadLine());
stack[++top] = num;
Console.WriteLine("Element is successfully
inserted.");
}
break;
case 2:
if (top == -1)
{
Console.WriteLine("Stack is empty");
}
else
{
Console.WriteLine("Deleted element is: " +
stack[top--]);
}
break;
case 3:
if (top == -1)
{
Console.WriteLine("Stack is empty");
}
else
{
Console.WriteLine("Elements in the Stack:");
Console.WriteLine("----------------------------------------");
for (int i = top; i >= 0; i--)
{
Console.WriteLine(stack[i]);
}
}
break;
case 0:
Environment.Exit(0);
break;
default:
Console.WriteLine("Wrong entry");
break;
}
}
Console.ReadLine();
}
}
}

6.

Write a program to demonstrate Operator overloading.


using System;
using System.Collections.Generic;

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.

Write a Program in C# to multiply to matrices using Rectangular arrays.

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:");

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();
}
Console.ReadLine();
}
public void MultiplyMatrix()
{
if (a.GetLength(1) == b.GetLength(0))
{
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++) // OR
k<b.GetLength(0)
c[i, j] = c[i, j] + a[i, k] * b[k, j];
}
}
}
else
{
Console.WriteLine("\n Number of columns in Matrix1 is not
equal to Number of rows in Matrix2.");
Console.WriteLine("\n Therefore Multiplication of Matrix1
with Matrix2 is not possible");
Environment.Exit(-1);
Console.ReadLine();
}
}
}
}

using System;
namespace LabProgram8
{
class Matrices
{

public static void Main(string[] args)


{
MatrixMultiplication MM = new MatrixMultiplication();
MM.ReadMatrix();
MM.MultiplyMatrix();
MM.PrintMatrix();
}
}
}

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][];

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


{
myJagArray[i] = new int[i + 3];
}
for (int i = 0; i < 3; i++)
{
Console.WriteLine("Enter {1} elements of row {0}
",i,myJagArray[i].Length);
for (int j = 0; j < myJagArray[i].Length; j++)
{
myJagArray[i][j] = int.Parse(Console.ReadLine());
}
Console.WriteLine();
}
int sum = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < myJagArray[i].Length; j++)
{
sum += myJagArray[i][j];
}
Console.WriteLine("The sum of jagged array is {0}", sum);
Console.ReadLine();
}
}
}
}

1. Write a program to reverse a given string using C#.


using System;
namespace LabProgram10
{
class RevStr
{
public static string Reverse(string str)
{
int len = str.Length;
char[] arr = new char[len];
for (int i = 0; i < len; i++)
{
arr[i] = str[len - 1 - i];
}
return new string(arr);
}
public static void Main(string[] args)
{
string str;
string revstr;
Console.WriteLine("Enter the string to be reversed");
str = Console.ReadLine();
revstr = Reverse(str);
Console.WriteLine("--------------------------------------");
Console.WriteLine("The reverse of a string is {0}:",revstr);
Console.ReadLine();
}
}
}

2. Using Try, Catch and Finally blocks write a program in C# to demonstrate


error handling.
using System;
namespace LabProgram11
{
class TryCatch
{
public static void Main(string[] args)
{
int a, b = 0;
Console.WriteLine("My program starts");
try
{
a = 10 / b;
}
catch (InvalidCastException e)
{
Console.WriteLine(e);
}
catch (DivideByZeroException e)
{
Console.WriteLine(e);
}
finally
{
Console.WriteLine("finally");
}
Console.WriteLine("Remaining program");
Console.ReadLine();
}
}
}

1. Design a simple calculator using Switch Statement in C#.


using
using
using
using

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

Implement linked lists in C# using the existing collections name space.


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

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

4. Write a program to demonstrate abstract class and abstract methods in C#.


using
using
using
using

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

6. Write a program to illustrate the use of different properties in C#.


using
using
using
using

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

7. Demonstrate arrays of interface types with a C# program.


using
using
using
using

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()
{

Console.WriteLine("Name is: {0}\n ID is:{1}", name, usn);


}
public override string ToString()
{
return (name + "" + usn);
}
}
}
using
using
using
using

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

You might also like