C# Lab Manual
C# Lab Manual
C# Lab Manual
using System;
namespace palindrome
{
class pali
{
public static void Main (string[] args)
{
int n,r,temp,sum=0;
Console.WriteLine("Enter the number:");
n=int.Parse(Console.ReadLine());
temp=n;
while(n>0)
{
r=n%10;
sum=(sum*10)+ r;
n=n/10;
}
if (temp==sum)
{
Console.WriteLine(" Same");
}
else
{
Console.WriteLine("not");
}
}
}
}
2
using System;
namespace SDC
{
class cmdLnArgs
{
static void Main(string[] args)
{
int oprnd1, oprnd2;
oprnd1 = Convert.ToInt32(args[0]);
oprnd2 = Convert.ToInt32(args[1]);
Console.WriteLine("Sum = " + (oprnd1 + oprnd2));
Console.ReadKey();
}
}
}
3
using System;
namespace QuadraticEqn
{
class Program
{
static void Main(string[] args)
{
int a, b, c;
double d, x1, x2;
Console.Write("\n\n");
Console.Write("Calculate root of Quadratic Equation :\n");
Console.Write("----------------------------------------");
Console.Write("\n\n");
Console.Write("Input the value of a : ");
a = Convert.ToInt32(Console.ReadLine());
Console.Write("Input the value of b : ");
b = Convert.ToInt32(Console.ReadLine());
Console.Write("Input the value of c : ");
c = Convert.ToInt32(Console.ReadLine());
d = b * b - 4 * a * c;
if (d == 0)
{
Console.Write("Both roots are equal.\n");
x1 = -b / (2.0 * a);
x2 = x1;
Console.Write("First Root Root1= {0}\n", x1);
Console.Write("Second Root Root2= {0}\n", x2);
}
else if (d > 0)
{
Console.Write("Both roots are real and diff-2\n");
x1 = (-b + Math.Sqrt(d)) / (2 * a);
x2 = (-b - Math.Sqrt(d)) / (2 * a);
Console.Write("First Root Root1= {0}\n", x1);
Console.Write("Second Root root2= {0}\n", x2);
}
else
Console.Write("Root are imeainary;\nNo Solution. \n\n");
Console.ReadKey();
}
}
}
4
using System;
namespace boxingUnboxing
{
class Program
{
static void Main(string[] args)
{
// Boxing
int a = 10;
object obj =null;
Console.WriteLine("Boxing");
Console.WriteLine("Before boxing object value " + obj);
// converting value type to reference type
obj = a;
Console.WriteLine("After boxing object value " + obj);
// Unboxing
int x=20;
Console.WriteLine("\nUnboxing");
Console.WriteLine("Before unboxing value is " + x);
// converting refernce type to value type
x =(int) obj;
Console.WriteLine("After unboxing value is " + x);
Console.ReadKey();
}
}
}
5
using System;
using System.Collections;
namespace StackOperation
{
class Program
{
static void Main(string[] args)
{
// Creating a Stack
int selected,length;
Stack myStack = new Stack();
while (true)
{
Console.WriteLine("\n\nStack operations");
Console.WriteLine("1. Push");
Console.WriteLine("2. Display");
Console.WriteLine("3. Pop");
Console.WriteLine("4. Peek");
Console.WriteLine("5. Count");
Console.WriteLine("6. Exit\n\n");
selected = Convert.ToInt32(Console.ReadLine());
switch (selected)
{
case 1:
Console.WriteLine("Enter how many elements");
length = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < length; i++)
{
myStack.Push(Console.ReadLine());
}
break;
case 2:
if (myStack.Count == 0)
Console.WriteLine("stack is empty");
else
foreach (var item in myStack)
{
Console.WriteLine(item);
}
break;
case 3:
if (myStack.Count == 0)
Console.WriteLine("Stack is empty");
else
myStack.Pop();
break;
case 4:
if (myStack.Count == 0)
Console.WriteLine("Stack is empty");
else
Console.WriteLine(myStack.Peek());
break;
case 5:
Console.WriteLine("number of elements in stack is "+myStack.Count);
break;
default:
goto exit;
}
}
exit:
Console.WriteLine("");
}
}
}
6
using System;
namespace OperatorOverloading
{
class Line
{
public int Length;
public static Line operator +(Line l1, Line l2)
{
Line objLine = new Line();
objLine.Length = l1.Length + l2.Length;
return objLine;
}
}
class Program
{
static void Main(string[] args)
{
Line l1 = new Line();
Line l2 = new Line();
Console.WriteLine("Enter line1 length");
l1.Length =Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter line2 length");
l2.Length =Convert.ToInt32(Console.ReadLine());
Line l3 = l1 + l2;
Console.WriteLine("sum is " + l3.Length );
Console.ReadKey();
}
}
using System;
namespace SecondLargest
{
class Program
{
static void Main(string[] args)
{
int[] arr = new int[10];
int i, first, second,n;
Console.WriteLine("Enter how many elments");
n = Convert.ToInt32(Console.ReadLine());
//There should be atleast two elements
if (n < 2)
{
Console.WriteLine("There should be atleast two elements ");
}
else
{
// Read the elements from keyboard
Console.WriteLine("Enter {0} elements", n);
for (i = 0; i < n; i++)
{
arr[i] = Convert.ToInt32(Console.ReadLine());
}
first = second = 0;
for (i = 0; i < arr.Length; i++)
{
// If current element is smaller than
// first then update both first and second
if (arr[i] > first)
{
second = first;
first = arr[i];
}
// If arr[i] is in between first
// and second then update second
else if (arr[i] > second && arr[i] != first)
second = arr[i];
}
if (second == 0)
Console.Write("There is no second largest element\n");
else
Console.Write("The second largest element " + second);
}
Console.ReadKey();
}
}
}
using System;
namespace stringReverse
{
class Program
{
static void Main(string[] args)
{
string str = "", reverse = "";
int Length = 0;
Console.WriteLine("Enter a Word");
//Getting String(word) from Console
str = Console.ReadLine();
//Calculate length of string str
Length = str.Length - 1;
while (Length >= 0)
{
reverse = reverse + str[Length];
Length--;
}
//Displaying the reverse word
Console.WriteLine("Reverse word is {0}", reverse);
Console.ReadLine();
}
}
}
9
using System;
namespace test
{
public class Program
{
// Main Method
static public void Main()
{
int a = 10, b = 0, c;
try
{
c = a / b;
}
catch (Exception e)
{
Console.WriteLine(e);
}
finally
{
Console.WriteLine("finally : exception raises or not but i will be
executed");
}
Console.WriteLine("out of exception scope");
Console.ReadKey();
}
}
}
10
using System;
namespace Calculator
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the action to be performed");
Console.WriteLine("Press 1 for Addition");
Console.WriteLine("Press 2 for Subtraction");
Console.WriteLine("Press 3 for Multiplication");
Console.WriteLine("Press 4 for Division \n");
int action = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter 1st input");
int opr1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter 2nd input");
int opr2 = Convert.ToInt32(Console.ReadLine());
int result = 0;
switch (action)
{
case 1:
{
result = opr1 + opr2;
break;
}
case 2:
{
result = opr1 - opr2;
break;
}
case 3:
{
result = opr1 * opr2;
break;
}
case 4:
{
result = opr1 / opr2;
break;
}
default:
Console.WriteLine("Wrong action!! try again");
break;
}
Console.WriteLine("The result is {0}", result);
Console.ReadKey();
}
}
}
11
using System;
namespace VirtualOveride
{
class Program
{
static void Main(string[] args)
{
// Base class, method draw is virtual
Shape objShape = new Shape();
objShape.draw();
// Derived class, method draw is overidden
Square objSquare = new Square();
objSquare.draw();
Console.ReadKey();
}
}
// Base class
class Shape
{
public virtual void draw()
{
Console.WriteLine("From base class : Shape");
}
}
// Derived class
class Square : Shape
{
public override void draw()
{
Console.WriteLine("From derived class, Method(draw) is overidden : Square");
}
}
13
using System;
namespace AbstractClsMthd
{
class Program
{
static void Main(string[] args)
{
int a, b;
Console.WriteLine("Enter first and second values");
a = Convert.ToInt32(Console.ReadLine());
b = Convert.ToInt32(Console.ReadLine());
// Creating an object
MathsOprtion objMathsOprtion = new MathsOprtion();
Console.WriteLine("sum = {0}", objMathsOprtion.addition(a, b));
Console.WriteLine("substraction = {0}",
objMathsOprtion.substraction(a, b));
Console.ReadKey();
}
}
// Abstract class having prototype.
abstract class Arithmatic
{
// Abstract method : doen't have implementation
public abstract int addition(int a, int b);
// Class is implemented.
class MathsOprtion : Arithmatic
{
public override int addition(int a, int b)
{
return a + b;
}
}
}
15
using System;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
student obj = new student();
Console.WriteLine("Student Info");
obj.acceptp();
Console.WriteLine("S-Course Info");
obj.acceptc();
Console.WriteLine("Student Details:-");
obj.displayp();
obj.displayc();
Console.ReadKey();
}
}
public interface Iperson
{
void acceptp();
void displayp();
}
public interface Icourse
{
void acceptc();
void displayc();
}
class student: Iperson, Icourse
{
string name, fname, course;
int sem;