Sei sulla pagina 1di 47

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

using System;
using System.Collections.Generic;
using System.Text;

namespace LabEx1

class PalindromeProgram

static void Main(string|| args)

Console.Write(" Enter any number: ");
int num int.Parse(Console.ReadLine());
int rev 0, n, rem;
n num;
while (num ! 0)

rem num 10;
rev rev * 10 rem;
num num / 10;
}
Console.WriteLine(" \n Original number is " n);
Console.WriteLine(" \n Reversed number is " rev);
iI (n rev)

Console.WriteLine("\n Original number Reversed number.");
Console.WriteLine("\n So, 0} is Palindorme ", n);
Console.ReadLine();
}
else

Console.WriteLine("\n Original number ! Reversed number.");
Console.WriteLine("\n So, 0} is not a Palindorme ", n);
Console.ReadLine();
}
}
}
}




// 2. To find the sum and product of numbers entered as command line parameters

using System;

namespace LabEx2

class commandLineParameters

static void Main(string|| args)

iI (args.Length 0)

Console.WriteLine("\n This program Iinds the SUM & PRODUCT oI two numbers
entered as command line parameters.");
Console.WriteLine("\n So Please enter two numbers as command line parameters.");
Console.ReadLine();
}
else

Console.WriteLine("\n This program Iinds the SUM & PRODUCT oI two numbers
entered as command line parameters.");
Console.WriteLine("\n Command line parameters recieved are: ");
Ioreach (string str in args)
Console.WriteLine(str);
int sum 0, prod 1, data;
Ioreach (string str in args)

data int.Parse(str);
sum sum data;
prod prod * data;
}
Console.WriteLine(" \n Sum oI command line parameters " sum);
Console.WriteLine(" \n Product oI command line parameters " prod);
Console.ReadLine();
}
}
}
}





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

using System;

namespace LabEx3

class Quadratic

double a, b, c;

public void read()

Console.WriteLine(" \n To Iind the roots oI a quadratic equation oI the Iorm a*x*x b*x
c 0");
Console.Write("\n Enter value Ior a : ");
a double.Parse(Console.ReadLine());
Console.Write("\n Enter value Ior b : ");
b double.Parse(Console.ReadLine());
Console.Write("\n Enter value Ior c : ");
c double.Parse(Console.ReadLine());
}
public void compute()

int m;
double r1, r2, disc;
disc b * b - 4 * a * c;
iI (a 0)
m 1;
else iI (disc ~ 0)
m 2;
else iI (disc 0)
m 3;
else
m 4;
switch (m)

case 1: Console.WriteLine("\n Not a Quadratic equation, Linear equation !!!");
Console.ReadLine();
break;
case 2: Console.WriteLine("\n Roots are Real and Distinct");
r1 (-b Math.Sqrt(disc)) / (2 * a);
r2 (-b - Math.Sqrt(disc)) / (2 * a);
Console.WriteLine("\n First root is 0:#.##}", r1);
Console.WriteLine("\n Second root is 0:#.##}", r2);
Console.ReadLine();
break;
case 3: Console.WriteLine("\n Roots are Real and Equal");
r1 r2 (-b) / (2 * a);
Console.WriteLine("\n First root is 0:#.##}", r1);
Console.WriteLine("\n Second root is 0:#.##}", r2);
Console.ReadLine();
break;
case 4: Console.WriteLine("\n Roots are Imaginary");
r1 (-b) / (2 * a);
r2 Math.Sqrt(-disc) / (2 * a);
Console.WriteLine("\n First root is 0:#.##} i 1:#.##}", r1, r2);
Console.WriteLine("\n Second root is 0:#.##} - i 1:#.##}", r1, r2);
Console.ReadLine();
break;
}
}
}

class Roots

public static void Main()

Quadratic qd new Quadratic();
qd.read();
qd.compute();
}
}
}




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

using System;

namespace LabEx4

class TestBoxing

public static void Main()
1.
int i 123;
object obj i; // Implicit boxing

Console.WriteLine("\nThe value-type value 0}", i);
Console.WriteLine("\nThe object-type value 0}", obj);
Console.ReadLine();

i 456; // Change the contents oI i
Console.WriteLine("\nThe value-type value 0}", i);
Console.WriteLine("\nThe object-type value 0}", obj);
Console.ReadLine();

int j (int)obj;
Console.WriteLine("\nThe value-type value 0}", j);
Console.ReadLine();
}
}
}


// 5. Write a Program in C# to implement Stack operations.

using System;
using System.Collections;

namespace LabEx5

class cstack

int index;
ArrayList list;
public cstack()

index -1;
list new ArrayList();
}

public void push()

Console.Write("\n Enter an item to push: ");
string str Console.ReadLine();
list.Add(str);
index;
Console.WriteLine("\n 0} is pushed onto the Stack.", str);
}
public void pop()

iI (index -1)
Console.WriteLine("\n Sorry ! Stack is empty.");
else

object obj list|index|;
list.RemoveAt(index);
index--;
Console.WriteLine("\n Poped item is 0}", obj);
}
}
public void status()

iI (index -1)
Console.WriteLine("\n Sorry ! Stack is empty.");
else

Console.WriteLine("\n Contents oI the STACK: \n");
Ioreach (object obj in list)
Console.WriteLine(obj);
}

}
public void clear()

iI (index -1)
Console.WriteLine("\n Sorry ! Stack is empty.");
else

list.Clear();
index -1;
Console.WriteLine("\n The stack is emptied. ");
}
}
}

class StackProgram

static void Main(string|| args)

cstack st new cstack();
int i1, ch;
while(i1)

Console.Clear();
Console.WriteLine("\n ********************************");
Console.WriteLine("\n Welcome to STACK Operations.");
Console.WriteLine("\n ********************************");
Console.WriteLine("\n 1-----~ PUSH");
Console.WriteLine("\n 2-----~ POP");
Console.WriteLine("\n 3-----~ STATUS");
Console.WriteLine("\n 4-----~ CLEAR");
Console.WriteLine("\n 5-----~ EXIT");
Console.WriteLine("\n ********************************");
Console.Write("\n\n Enter your choice: ");
ch int.Parse(Console.ReadLine());

switch (ch)

case 1: st.push();
break;
case 2: st.pop();
break;
case 3: st.status();
break;
case 4: st.clear();
break;
case 5: Environment.Exit(-1);
break;
deIault: Console.WriteLine(" Sorry !!! Wrong choice.");
break;
}
Console.Write("\n Press ENTER to Continue: ");
Console.ReadLine();
}
Console.WriteLine("\n End oI STACK operations... Bye");
}
}
}


// 6. Write a program to demonstrate Operator overloading.

using System;

namespace LabEx6

class Complex

double x; //real part
double y; //imaginary part

public void read()

Console.Write("\n Enter real part : ");
x double.Parse(Console.ReadLine());
Console.Write("\n Enter imaginary part : ");
y double.Parse(Console.ReadLine());
}
public static Complex operator (Complex c1, Complex c2)

Complex c new Complex();
c.x c1.x c2.x;
c.y c1.y c2.y;
return (c);
}
public static Complex operator -(Complex c1, Complex c2)

Complex c new Complex();
c.x c1.x - c2.x;
c.y c1.y - c2.y;
return (c);
}
public void Display()

Console.WriteLine("("x")" " i" "("y")");
}
}
class ComplexTest

public static void Main()

Complex a new Complex();
Console.WriteLine("\n a (Complex number1) : ");
a.read();
Complex b new Complex();
Console.WriteLine("\n\n b (Complex number2) : ");
b.read();
Complex c a b;
Complex d b - a;
Console.Write("\n\n a ");
a.Display();
Console.Write("\n\n b ");
b.Display();
Console.Write("\n\n c a b : ");
c.Display();
Console.Write("\n\n d b - a : ");
d.Display();
Console.ReadLine();
}
}

}


// 7. Write a Program in C# to find the second largest element in a single dimensional
array.

using System;

namespace LabEx7

class SecondLargest

public static void Main()

int|| a;
Console.Write(" Enter the size oI the array : ");
a new int|int.Parse(Console.ReadLine())|;
Console.WriteLine(" \n Enter the array elements ");
Ior (byte i 0; i a.Length; i)
a|i| int.Parse(Console.ReadLine());
int max1, max2;
max1 max2 a|0|;
Ior (byte i 1; i a.Length; i)

iI (a|i| ~ max1)

max2 max1;
max1 a|i|;
}
else
iI ((a|i| ~ max2 && a|i| max1) ,, max1 max2)

max2 a|i|;
}
}
Console.WriteLine(" \n First largest element is: " max1);
Console.WriteLine(" \n Second largest element is: " max2);
Console.ReadLine();
}
}
}


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

using System;

namespace LabEx8

class MatrixMultiplication

int|,| a;
int|,| b;
int|,| c;

public void ReadMatrix()

Console.WriteLine("\n Size oI Matrix 1:");
Console.Write("\n Enter the number oI rows : ");
int m int.Parse(Console.ReadLine());
Console.Write("\n Enter the number oI columns : ");
int n int.Parse(Console.ReadLine());
a new int|m, n|;
Console.WriteLine("\n Enter the elements : ");
Ior (int i 0; i a.GetLength(0); i)

Ior (int j 0; j a.GetLength(1); j)

a|i, j| int.Parse(Console.ReadLine());
}
}

Console.WriteLine("\n Size oI Matrix 2 :");
Console.Write("\n Enter the number oI rows : ");
m int.Parse(Console.ReadLine());
Console.Write("\n Enter the number oI columns : ");
n int.Parse(Console.ReadLine());
b new int|m, n|;
Console.WriteLine("\n Enter the elements : ");
Ior (int i 0; i b.GetLength(0); i)

Ior (int j 0; j b.GetLength(1); j)

b|i, j| int.Parse(Console.ReadLine());
}
}
}

public void PrintMatrix()

Console.WriteLine("\n Matrix 1:");
Ior (int i 0; i a.GetLength(0); i)

Ior (int j 0; j a.GetLength(1); j)

Console.Write("\t" a|i, j|);
}
Console.WriteLine();
}
Console.WriteLine("\n Matrix 2:");
Ior (int i 0; i b.GetLength(0); i)

Ior (int j 0; j b.GetLength(1); j)

Console.Write("\t" b|i, j|);
}
Console.WriteLine();
}
Console.WriteLine("\n Resultant Matrix aIter multiplying Matrix 1 & Matrix 2:");
Ior (int i 0; i c.GetLength(0); i)

Ior (int j 0; j c.GetLength(1); j)

Console.Write("\t" c|i, j|);
}
Console.WriteLine();
}
Console.ReadLine();
}
public void MultiplyMatrix()

iI (a.GetLength(1) b.GetLength(0))

c new int|a.GetLength(0), b.GetLength(1)|;
Ior (int i 0; i c.GetLength(0); i)

Ior (int j 0; j c.GetLength(1); j)

c|i, j| 0;
Ior (int k 0; k a.GetLength(1); k) // OR kb.getlength~
c|i, j| c|i, j| a|i, k| * b|k, j|;
}
}
}
else

Console.WriteLine("\n Number oI columns in Matrix1 is not equal to Number oI rows
in Matrix2.");
Console.WriteLine("\n ThereIore Multiplication oI Matrix1 with Matrix2 is not
possible");
Console.ReadLine();
Environment.Exit(-1);
}
}
}
class Matrices

public static void Main()

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


// 9. Find the sum oI all the elements present in a jagged array oI 3 inner arrays.

using System;

namespace LabEx9

class JaggedArrays

int|||| jagged new int|3|||;

public void ReadArrays()

Console.Write("\n Enter the size oI First inner array: ");
jagged|0| new int|int.Parse(Console.ReadLine())|;
Console.WriteLine("\n Enter the elements oI First inner array: ");
Ior (int i 0; i jagged|0|.Length; i)
jagged|0||i| int.Parse(Console.ReadLine());
Console.Write("\n Enter the size oI Second inner array: ");
jagged|1| new int|int.Parse(Console.ReadLine())|;
Console.WriteLine("\n Enter the elements oI Second inner array: ");
Ior (int i 0; i jagged|1|.Length; i)
jagged|1||i| int.Parse(Console.ReadLine());
Console.Write("\n Enter the size oI Third inner array: ");
jagged|2| new int|int.Parse(Console.ReadLine())|;
Console.WriteLine("\n Enter the elements oI Third inner array: ");
Ior (int i 0; i jagged|2|.Length; i)
jagged|2||i| int.Parse(Console.ReadLine());
}
public void FindSum()

int sum 0;
Ior (int i 0; i jagged|0|.Length; i)
sum sum jagged|0||i|;
Ior (int i 0; i jagged|1|.Length; i)
sum sum jagged|1||i|;
Ior (int i 0; i jagged|2|.Length; i)
sum sum jagged|2||i|;
Console.WriteLine("\n\n\n Sum oI all the three inner arrays is 0}", sum);
}
public void PrintArrays()

Console.Write("\n\n Elements oI First inner array: ");
Ior (int i 0; i jagged|0|.Length; i)
Console.Write(jagged|0||i| "\t");
Console.Write("\n\n Elements oI Second inner array: ");
Ior (int i 0; i jagged|1|.Length; i)
Console.Write(jagged|1||i| "\t");
Console.Write("\n\n Elements oI Third inner array: ");
Ior (int i 0; i jagged|2|.Length; i)
Console.Write(jagged|2||i| "\t");
}

}

class ArrayTest

public static void Main()

JaggedArrays JA new JaggedArrays();
JA.ReadArrays();
JA.PrintArrays();
JA.FindSum();
Console.ReadLine();
}
}

}


// 10. Write a program to reverse a given string using C#.

using System;
using System.Collections;

namespace LabEx10

class String1

string str;
public void ReadString()

Console.Write("\n Enter a string: ");
str Console.ReadLine();
}
public void Reverse()

char|| c str.ToCharArray();
string ts string.Empty; // string ts"";
Ior (int i c.Length - 1; i ~ 0; i--)
ts c|i|.ToString();
Console.WriteLine("\n Reversed string without using STACK is : 0}", ts);
}
}
class String2

int index;
ArrayList list;
public String2()

index -1;
list new ArrayList();
}
public void Reverse()

Console.Write("\n Enter a string: ");
string str Console.ReadLine();
Ior (int i 0; i str.Length; i)

list.Add(str|i|);
index;
}
Console.Write("\n Reversed string using STACK is : " );
while(index~0)

object obj list|index|;
list.RemoveAt(index);
index--;
Console.Write(obj);
}
}
}
class ReverseString

public static void Main()

String1 S1 new String1();
String2 S2 new String2();
int ch, i1;
while(i1)

Console.Clear();
Console.WriteLine("\n ************************************");
Console.WriteLine("\n Welcome to ReverseString Operations.");
Console.WriteLine("\n ************************************");
Console.WriteLine("\n 1-----~ Reverse without using STACK");
Console.WriteLine("\n 2-----~ Reverse using STACK");
Console.WriteLine("\n 3-----~ EXIT");
Console.WriteLine("\n ************************************");
Console.Write("\n\n Enter your choice: ");
ch int.Parse(Console.ReadLine());

switch (ch)

case 1: S1.ReadString();
S1.Reverse();
break;
case 2: S2.Reverse();
break;
case 3: Environment.Exit(-1);
break;
deIault: Console.WriteLine(" Sorry !!! Wrong choice.");
break;
}
Console.Write("\n\n Press ENTER to Continue: ");
Console.ReadLine();
}
}
}


}


// 11. Using Try, Catch and Finally blocks write a program in C# to demonstrate error
handling.

using System;

namespace LabEx11

class NestedTry

int m 10;
int n 0;
public void Division()

try

int k m / n;
}
catch (ArgumentException e)

Console.WriteLine("\n Caught an exception in Division method");
Console.WriteLine("\n" e);
}
catch (ArrayTypeMismatchException e)

Console.WriteLine("\n Caught an exception in Division method");
Console.WriteLine("\n" e);
}
Iinally

Console.WriteLine("\n Inside Division method Iinally");
}
Console.WriteLine("\n Outside Division method Iinally"); // Skipped
}
}
class ErrorHandling

public static void Main()

NestedTry NT new NestedTry();
Console.Clear();
try

NT.Division();
}
catch (ArgumentException e)

Console.WriteLine("\n Array index error");
Console.WriteLine("\n" e);
}
catch (DivideByZeroException e)

Console.WriteLine("\n Caught an exception in Main");
Console.WriteLine("\n" e);
}
Console.Write("\n Inside Main method ");
Console.ReadLine();
}
}

}


// 12. Design a simple calculator using Switch Statement in C#.

using System;

namespace LabEx12

class SimpleCalculator

double num1, num2;
public void read()

Console.WriteLine("\n Enter any two numbers:");
Console.Write("\n Number1 : ");
num1 double.Parse(Console.ReadLine());
Console.Write("\n Number2 : ");
num2 double.Parse(Console.ReadLine());
}
public void add()

double sum num1 num2;
Console.WriteLine("\n Result : (0}) (1}) 2}", num1, num2, sum);
}
public void subtract()

double diII num1 - num2;
Console.WriteLine("\n Result : (0}) - (1}) 2}", num1, num2, diII);
}
public void multiply()

double prod num1 * num2;
Console.WriteLine("\n Result : (0}) X (1}) 2}", num1, num2, prod);
}
public void divide()

double qt num1 / num2;
Console.WriteLine("\n Result : (0}) / (1}) 2}", num1, num2, qt);
}


}
class ArithmeticOperations

public static void Main()

SimpleCalculator SC new SimpleCalculator();
int ch, i1;
while(i1)

Console.Clear();
Console.WriteLine("\n *************************");
Console.WriteLine("\n SIMPLE CALCULATOR.");
Console.WriteLine("\n *************************");
Console.WriteLine("\n 1-----~ ADDITION");
Console.WriteLine("\n 2-----~ SUBTRACTION");
Console.WriteLine("\n 3-----~ MULTIPLICATION");
Console.WriteLine("\n 4-----~ DIVISION");
Console.WriteLine("\n 5-----~ EXIT");
Console.WriteLine("\n *************************");
Console.Write("\n\n Enter your choice: ");
ch int.Parse(Console.ReadLine());
switch (ch)

case 1: SC.read();
SC.add();
break;
case 2: SC.read();
SC.subtract();
break;
case 3: SC.read();
SC.multiply();
break;
case 4: SC.read();
SC.divide();
break;
case 5: Environment.Exit(-1);
break;
deIault: Console.WriteLine(" Sorry !!! Wrong choice.");
break;
}
Console.Write("\n Press ENTER to Continue. ");
Console.ReadLine();
}
Console.WriteLine("\n Cannot continue... Bye");
}
}
}


// 13. Demonstrate Use of Virtual and override key words in C# with a simple program

using System;

namespace LabEx13

class super

protected int x;
public super(int x)

this.x x;
}
public virtual void display()

Console.WriteLine("\n Super x " x);
}
}
class sub : super

int y;
public sub(int x, int y) : base(x)

this.y y;
}
public override void display()

Console.WriteLine("\n Sub x " x); // OR base.display();
Console.WriteLine("\n Sub y " y);
}
}
class overridetest

public static void Main()

sub s new sub(100, 200);
s.display();
Console.ReadLine();
}
}


}



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

using System;
using System.Collections.Generic;
using System.Text;

namespace LinkedList

class LinkedList

Listobject~ lst;/object~
public LinkedList()

lst new Listobject~(); // Create a list./object~
Console.WriteLine("\n Initial number oI elements in the LL : " lst.Count);
}
public void insert()

Console.Clear();
Console.WriteLine("\n *********************************************");
Console.WriteLine("\n 1-----~ INSERTION AT THE BEGINNING");
Console.WriteLine("\n 2-----~ INSERTION AT THE ENDING");
Console.WriteLine("\n 3-----~ INSERTION AFTER CERTAIN NODES");
Console.WriteLine("\n *********************************************");
Console.Write("\n\n Enter your choice: ");
int ch int.Parse(Console.ReadLine());
string str;
switch (ch)

case 1: Console.Write("\n Enter the data: ");
strConsole.ReadLine();
lst.Insert(0,str);
Console.WriteLine("\n 0} is inserted at the beginning.",str);
break;
case 2: Console.Write("\n Enter the data: ");
str Console.ReadLine();
lst.Add(str);
Console.WriteLine("\n 0} is inserted at the ending.",str);
break;
case 3: iI (lst.Count 0)

Console.WriteLine("\n List is not built.");
}
else

Console.Write("\n AIter how many nodes? : ");
int n int.Parse(Console.ReadLine());
iI(n~1&nlst.count~

Console.Write("\n Enter the data: ");
str Console.ReadLine();
lst.Insert(n,str);
Console.WriteLine("\n 0} is inserted aIter 1} nodes.", str, n);
}
else
Console.WriteLine("\n AIter 0} nodes, insertion is not possible !",n);
}
break;
deIault: Console.WriteLine("\n Sorry !!! Wrong choice.");
break;
}
}
public void delete()

iI (lst.Count 0)

Console.WriteLine("\n LL is empty !");
}

else


Console.Clear();
Console.WriteLine("\n *********************************************");
Console.WriteLine("\n 1-----~ DELETION (FIRST OCCURENCE)");
Console.WriteLine("\n 2-----~ DELETION AT A SPECIFIED POSITION");
Console.WriteLine("\n 3-----~ DELETION OF ALL THE ELEMENTS");
Console.WriteLine("\n *********************************************");
Console.Write("\n\n Enter your choice: ");
int ch int.Parse(Console.ReadLine());
switch (ch)

case 1: Console.Write("\n Enter the data: ");
string str Console.ReadLine();
iI (lst.Contains(str))

lst.Remove(str);
Console.WriteLine("\n First occurence oI 0} is deleted.", str);
}
else
Console.WriteLine("\n The data '0}' doesn't exist.", str);
break;
case 2: Console.Write("\n Enter the position : ");
int n int.Parse(Console.ReadLine());
iI (n ~ 1 & n

lst.RemoveAt(n - 1);
Console.WriteLine("\n Element at the position 0} is deleted.", n);
}
else
Console.WriteLine("\n Not a valid position to delete !");
break;
case 3: lst.Clear();
Console.WriteLine("\n All the elements are deleted");
break;
deIault: Console.WriteLine("\n Sorry !!! Wrong choice.");
break;
}
}
}
public void status()

iI (lst.Count 0)

Console.WriteLine("\n LL is empty !");
}
else

Console.WriteLine("\n LL contains 0} elements.", lst.Count);
Console.Write("\n The elements are : ");
Ior (int i 0; i lst.Count; i)
Console.Write(lst|i| "\t");
}
}
public void update()

iI (lst.Count 0)

Console.WriteLine("\n LL is empty !");
}
else

Console.Write("\n Enter the position oI the element to update : ");
int i int.Parse(Console.ReadLine());
iI (i ~ 1 & i

Console.Write("\n Enter the data to update at the position 0} : ", i);
string str Console.ReadLine();
lst|i-1| str;
Console.WriteLine("\n Data at position 0} is changed to 1}", i, str);
}
else
Console.WriteLine("\n Invalid position ! ");
}
}
}
class GenListDemo

static void Main(string|| args)

LinkedList LL new LinkedList();
int i1, ch;
while(i1)

Console.Clear();
Console.WriteLine("\n ************************************");
Console.WriteLine("\n Welcome to LINKED LIST Operations.");
Console.WriteLine("\n ************************************");
Console.WriteLine("\n 1-----~ INSERTION");
Console.WriteLine("\n 2-----~ DELETION");
Console.WriteLine("\n 3-----~ UPDATE");
Console.WriteLine("\n 4-----~ STATUS");
Console.WriteLine("\n 5-----~ EXIT");
Console.WriteLine("\n ************************************");
Console.Write("\n\n Enter your choice: ");
ch int.Parse(Console.ReadLine());
switch (ch)

case 1: LL.insert();
break;
case 2: LL.delete();
break;
case 3: LL.update();
break;
case 4: LL.status();
break;
case 5: Environment.Exit(-1);
break;
deIault: Console.WriteLine(" Sorry !!! Wrong choice.");
break;
}
Console.Write("\n\n Press ENTER to Continue: ");
Console.ReadLine();
}
Console.WriteLine("\n End oI LINKED LIST operations... Bye");
}
}
}

Potrebbero piacerti anche