Upload
ashwini-sathyanathan
View
258
Download
0
Embed Size (px)
Citation preview
7/29/2019 NET Lab Manual.doc
1/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{
classPalindrome{
publicstaticvoid Main(string[] args){
//Declaring the variablesint 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 inputintial_num = int.Parse(Console.ReadLine());num = intial_num;do{
//set digit to the users number mod 10digit = 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 numberConsole.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 notif(num == reverse_num)
{Console.WriteLine("***************************************");Console.WriteLine(" The number is palindrome");Console.WriteLine("***************************************");Console.WriteLine("\r\n");Console.ReadLine();}else{Console.WriteLine("***************************************");
7/29/2019 NET Lab Manual.doc
2/32
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{
classCommandLineArgs{
publicstaticvoid Main(string[] args){
//Declare variablesdouble argsValue = 0.0;double sqrtValue = 0.0;//Check the length of Command Line Argument
7/29/2019 NET Lab Manual.doc
3/32
if (args.Length == 0){
Console.WriteLine("*********************************************");Console.WriteLine("There is no Command Line Argument
defined");
Console.WriteLine("*********************************************");Console.ReadLine();return;
}//Finding squareroot of a number using Math objectargsValue = double.Parse(args[0].ToString()); sqrtValue = Math.Sqrt(argsValue);//Display 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 Visual C# 2008 Compiler version 3.5.31022.8For Microsoft .Net Framework version 3.5
Copyright Microsoft Corporation . All rights reserved.
C:\Program Files\ Microsoft Visual Studio 9.0\VC\MyExamples> CommandLineArgs
7/29/2019 NET Lab Manual.doc
4/32
*************************************** 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{
classQuadraticEquation{
publicstaticvoid 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);
7/29/2019 NET Lab Manual.doc
5/32
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, & C10
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. Write a Program in C# to demonstrate boxing and unBoxing.using System;using System.Collections.Generic; using System.Linq;using System.Text;
namespace LabProgram4{
classBoxUnbox{
publicstaticvoid 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);
7/29/2019 NET Lab Manual.doc
6/32
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: 10Value in n is: 10
5. Write a Program in C# to implement Stack operations. using System;
namespace LabProgram5{
classStackOperation{
publicstaticvoid Main(string[] args)
{int top = -1, num, choice, max, rpt = 1; int [] stack = newint[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{
7/29/2019 NET Lab Manual.doc
7/32
Console.WriteLine("Enter the element to beinserted");
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();}
}}
7/29/2019 NET Lab Manual.doc
8/32
7/29/2019 NET Lab Manual.doc
9/32
6. Write a program to demonstrate Operator overloading.using System;using System.Collections.Generic;
7/29/2019 NET Lab Manual.doc
10/32
using System.Linq;using System.Text;
namespace LabProgram6{
classOpertorOverlaoding{
publicstructComplex{
publicint real;publicint imaginary;public Complex (int real, int imaginary){
this.real = real;this.imaginary = imaginary;
}
//Declare which operator to overload (+) the types that canbe added (two Complex Objects) and the return type
// (Complex c1, Complex c2)
publicstaticComplexoperator +(Complex c1, Complex c2){
returnnewComplex(c1.real + c2.real, c1.imaginary +c2.imaginary);
}
// Override the tostring method to display an complex number insuitable format
publicoverridestring ToString(){
return(string.Format("{0} +{1}",real,imaginary));}
}
publicstaticvoid Main()
{Complex num1 = newComplex(2,3);Complex num2 = newComplex(3,4);
// Add the two complex objects ( num1 and num2) through the overloadedplus operatorComplex 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/29/2019 NET Lab Manual.doc
11/32
7. Write a Program in C# to find the second largest element in a single dimensional array.
using System;namespace Labprogram7a{
classBiggest{
int[] a = { 1, 2, 3, 4 };publicvoid 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("------------------------------");
}
publicvoid PrintElement()
{Console.WriteLine("\n The elements are:");Console.WriteLine("------------------------------");for (int i = 0; i < a.Length; i++) {
Console.WriteLine(a[i]);}Console.WriteLine("------------------------------");
}publicvoid BigElement()
7/29/2019 NET Lab Manual.doc
12/32
{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 secondbiggest element is: {1}", big, secbig);
Console.ReadLine();}
}}
namespace Labprogram7a{
classBig{
publicstaticvoid Main(string[] args){
Biggest MM = newBiggest();MM.ReadElement();MM.PrintElement();MM.BigElement();
}}
}
7/29/2019 NET Lab Manual.doc
13/32
8. Write a Program in C# to multiply to matrices using Rectangular arrays.using System;namespace LabProgram8{
classMatrixMultiplication{
int[,] a;int[,] b;int[,] c;
publicvoid 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 = newint[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 = newint[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());}
}}
publicvoid 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:");
7/29/2019 NET Lab Manual.doc
14/32
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();
}publicvoid MultiplyMatrix(){
if (a.GetLength(1) == b.GetLength(0)){
c = newint[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
7/29/2019 NET Lab Manual.doc
15/32
publicstaticvoid Main(string[] args){
MatrixMultiplication MM = newMatrixMultiplication();MM.ReadMatrix();MM.MultiplyMatrix();MM.PrintMatrix();
}}
}
7/29/2019 NET Lab Manual.doc
16/32
9.
Find the sum of all the elements present in a jagged array of 3 inner arrays.
using System;namespace LabProgram9{
classJagArray{
publicstaticvoid Main(string[] args){
int[][] myJagArray = newint[3][];
7/29/2019 NET Lab Manual.doc
17/32
for (int i = 0; i < myJagArray.Length; i++){
myJagArray[i] = newint[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();
}}
}}
7/29/2019 NET Lab Manual.doc
18/32
1. Write a program to reverse a given string using C#.using System;namespace LabProgram10{
classRevStr{
publicstaticstring Reverse(string str){
int len = str.Length;char[] arr = newchar[len];for (int i = 0; i < len; i++){
arr[i] = str[len - 1 - i]; }returnnewstring(arr);
}publicstaticvoid 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();
}}
}
7/29/2019 NET Lab Manual.doc
19/32
2. Using Try, Catch and Finally blocks write a program in C# to demonstrateerror handling.
using System;namespace LabProgram11{
classTryCatch{publicstaticvoid 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();
}}
}
7/29/2019 NET Lab Manual.doc
20/32
1. Design a simple calculator using Switch Statement in C#.using System;using System.Collections.Generic; using System.Linq;using System.Text;
namespace LabProgram12{
classSimpleCalculator{
publicstaticvoid 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());
7/29/2019 NET Lab Manual.doc
21/32
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();
}}
}
7/29/2019 NET Lab Manual.doc
22/32
2.Demonstrate Use of Virtual and override key words in C# with a simple program
using System;using System.Collections.Generic;
namespace LabProgram13{
publicclassCustomer{
publicvirtualvoid CustomerType()
7/29/2019 NET Lab Manual.doc
23/32
{Console.WriteLine("I am a Customer");
}}
publicclassCorporateCustomer : Customer{
publicoverridevoid CustomerType(){
Console.WriteLine("I am a Corporate Customer");}
}
publicclassPersonalComputer : Customer{
publicoverridevoid CustomerType(){
Console.WriteLine("I am a Personal Customer");}
}
classProgram{
staticvoid Main(string[] args){
Customer[] c = newCustomer[3];c[0] = newPersonalComputer();c[1] = newCorporateCustomer();c[2] = newCustomer();foreach (Customer CustomerObject in c){
CustomerObject.CustomerType(); }Console.ReadLine();
}}
}
7/29/2019 NET Lab Manual.doc
24/32
3. Implement linked lists in C# using the existing collections name space.using System;using System.Collections.Generic; using System.Linq;using System.Text;
namespace LabProgram14{
classLinkedlist{
staticvoid Main(string[] args){
LinkedList obj = newLinkedList();LinkedListNode 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();
}}
}
7/29/2019 NET Lab Manual.doc
25/32
4.Write a program to demonstrate abstract class and abstract methods in C#.using System;using System.Collections.Generic; using System.Linq;using System.Text;
namespace LabProgram15{
abstractclassShape
{abstractpublicvoid show();
}}
namespace LabProgram15{
classCircle : Shape{
publicoverridevoid show(){
Console.WriteLine("We are in Circle");}
}}
namespace LabProgram15{
classTriangle : Shape{
publicoverridevoid show(){
Console.WriteLine("We are in Triangle");
7/29/2019 NET Lab Manual.doc
26/32
}}
}
namespace LabProgram15{
classAbstractclassmethod{
staticvoid Main(string[] args){
Circle c = newCircle();Triangle t = newTriangle();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 System;
using System.Collections.Generic; using System.Linq;using System.Text;
namespace LabProgram16{
interfaceShape{
void Show();}
7/29/2019 NET Lab Manual.doc
27/32
}
using System;using System.Collections.Generic; using System.Linq;using System.Text;
namespace LabProgram16{
classTriangle:Shape{
#region Shape Members
voidShape.Show(){
Console.WriteLine("I am printing from a Triangle");}
#endregion}
}
using System;using System.Collections.Generic; using System.Linq;using System.Text;
namespace LabProgram16{
classCircle:Shape{
#region Shape Members
voidShape.Show(){
Console.WriteLine("I am printing from Circle");}
#endregion}
}
using System;using System.Collections.Generic; using System.Linq;
using System.Text;
namespace LabProgram16{
classInterfaceExample{
staticvoid Main(string[] args){
Circle c = newCircle();Triangle t = newTriangle();
7/29/2019 NET Lab Manual.doc
28/32
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 System;using System.Collections.Generic; using System.Linq;using System.Text;
namespace LabProgram17{
classEmployee
{ privatestring ename, eid;int eage;double esalary;public Employee(string eid){
this.eid = eid;
}publicstring EmployeeName
7/29/2019 NET Lab Manual.doc
29/32
{get{
return ename;}set{
ename = value;}
}
publicstring Employeeid{
get{
return eid;}
}
publicint EmployeeAge
{get{
return eage;}set{
eage = value;}
}
publicdouble EmployeeSalary{
get{
return esalary;}set{
esalary = value;}
}}
}
7/29/2019 NET Lab Manual.doc
30/32
7. Demonstrate arrays of interface types with a C# program.
using System;using System.Collections.Generic; using System.Linq;using System.Text;
namespace LabProgram18{
interfaceInter{
void info();}
}
using System;using System.Collections.Generic; using System.Linq;using System.Text;
namespace LabProgram18{
classStudent:Inter{privatestring name, usn;public Student(string name, string usn){
this.name = name;this.usn = usn;
}publicvoid info(){
7/29/2019 NET Lab Manual.doc
31/32
Console.WriteLine("Name is: {0}\n ID is:{1}", name, usn);}
publicoverridestring ToString(){
return (name + "" + usn);}
}}
using System;using System.Collections.Generic; using System.Linq;using System.Text;
namespace LabProgram18{
classEmp:Inter{
privatestring name, id;
public Emp(string name, string id){
this.name = name;this.id = id;
}
publicvoid info(){
Console.WriteLine("Name is: {0}\n ID is:{1}", name, id);}
}}
using System;using System.Collections.Generic; using System.Linq;using System.Text;
namespace LabProgram18{
classArrayInterfaceExample{
staticvoid Main(string[] args){
Inter[] obj = { newStudent("Raju", "10MCA01"), newEmp("Rahul",
"e100"), newStudent("Suresh", "10MCA02") };foreach (Inter s in obj)
Console.WriteLine(s);Console.ReadLine();
}}
}
7/29/2019 NET Lab Manual.doc
32/32