You are on page 1of 35

.

NET Laboratory[10MCA57]

Page no:

Program1: Write a program in C# to check whether a number is Palindrome or not.


using System;
using System.Collections.Generic;
using System.Text;
namespace q8
{
class Program
{
static void Main(string[] args)
{
int n,m, i=0;
Console.WriteLine(" enter the number");
m= n = int.Parse(Console.ReadLine());
while (n > 0)
{
int rem = n % 10;
i = i * 10 + rem;
n /= 10;
}
if (i == m)
Console.WriteLine("{0} is palindrom no ",m);
else
Console.WriteLine("{0} is not palindrom no ",m);
Console.ReadLine();
}
}
}

Output:

NAME: NAVEEN N
USN:1OX12MCA57

.NET Laboratory[10MCA57]

Page no:

Program 2a: Write a program in C# to demonstrate Command Line Arguments


Processing.
Steps to set command line arguments:
Step 1: go to project menu -> select project properties option
Step 2: In project property window select debug tab.
Step 3: In Debug tab set the argument list in Command line argument text
box which we seeing in following window:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace q2
{
class Program
{
static void Main(string[] args)
{
if (args.Length > 0)
foreach (string var in args)
NAME: NAVEEN N
USN:1OX12MCA57

.NET Laboratory[10MCA57]
Console.Write("\t " + var);
else
Console.WriteLine("argument is not passed !");
Console.Read();
}
}
}

Output:

NAME: NAVEEN N
USN:1OX12MCA57

Page no:

.NET Laboratory[10MCA57]

Page no:

Program 2b: Write a Program in C# to find the sum of arguments passed.


using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication8
{
class Program
{
static void Main(string[] args)
{
int sum=0;
if (args.Length > 0)
{
Console.WriteLine();
for (int i =0;i<args.Length;i++)
sum += int.Parse(args[i]);
Console.WriteLine("The Sum of Arguments passed is:{0}",sum);
}
else
Console.WriteLine("argument is not passed !");
Console.Read();
}
}
}

Output:

NAME: NAVEEN N
USN:1OX12MCA57

.NET Laboratory[10MCA57]

Page no:

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


using System;
using System.Collections.Generic;
using System.Text;
namespace lab3
{
class Program
{
static void Main(string[] args)
{
double a, b, c, d;
Console.WriteLine("enter 3 numbers \n");
a = double.Parse(Console.ReadLine());
b = double.Parse(Console.ReadLine());
c = double.Parse(Console.ReadLine());
if (a == 0 || a < 0)
Console.WriteLine("invalid values \n");
else
{
d = b * b - 4 * a * c;
if (d > 0)
{
Console.WriteLine("roots are real and distinct \n");
Console.WriteLine("roots : {0}", -b + Math.Sqrt(d) / (2 * a));
Console.WriteLine("roots : {0}", -b - Math.Sqrt(d) / (2 * 1));
}
else if (d == 0)
{
Console.WriteLine("roots are equal \n");
Console.WriteLine("roots :{0}", -b / (2 * a));
}
else
Console.WriteLine("roots are imaginary \n");
}
Console.ReadKey();
}
}
}

NAME: NAVEEN N
USN:1OX12MCA57

.NET Laboratory[10MCA57]

Output:

NAME: NAVEEN N
USN:1OX12MCA57

Page no:

.NET Laboratory[10MCA57]

Page no:

Pr
ogram 4: Write a Program in C# to demonstrate Boxing and UnBoxing.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace q4
{
class Program
{
static void Main(string[] args)
{
int a = 10;
object x ;
Console.WriteLine("value of a={0}", a);
x = a;
Console.WriteLine("value of object x={0}", x);
x = 100;
a =(int) x;
Console.WriteLine("value of object x={0}", x);
Console.WriteLine("value of a={0}", a);
Console.Read();
}
}
}

Output:

NAME: NAVEEN N
USN:1OX12MCA57

.NET Laboratory[10MCA57]

NAME: NAVEEN N
USN:1OX12MCA57

Page no:

.NET Laboratory[10MCA57]

Program 5: Write a Program in C# to implement Stack Operations.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace stack_operation
{
class Program
{
static void Main(string[] args)
{
stack_operation s1 = new stack_operation();
int ch;
while (true)
{
Console.WriteLine("\n 1.Push \n 2.pop \n 3.display \n 4.exit \n");
Console.WriteLine("enter yout choice \n");
ch = int.Parse(Console.ReadLine());
switch (ch)
{
case 1: s1.push();
break;
case 2: s1.pop();
break;
case 3: s1.display();
break;
case 4: System.Environment.Exit(0);
break;
default: Console.WriteLine("\n enter correct option \n");
break;
}
}
}
public class stack_operation
{
int top;
int[] stk = new int[20];
public stack_operation()
{
top=-1;
}
public void push()
{
Console.WriteLine("enter new element to push \n");
int ele=int.Parse(Console.ReadLine());
if(top==4)
Console.WriteLine("stack overflow \n");
else
NAME: NAVEEN N
USN:1OX12MCA57

Page no:

.NET Laboratory[10MCA57]
{
top=top+1;
stk[top]=ele;
}
}
public void pop()
{
if(top==-1)
Console.WriteLine("stack underflow \n");
else
{
Console.WriteLine("popped element is "+stk[top]);
top=top-1;
}
}
public void display()
{
if(top==-1)
Console.WriteLine("no element is in stack");
else
{
Console.WriteLine("elements in stack are \n");
for(int i=0;i<=top;i++)
Console.WriteLine(stk[i]);
}
}
}
}
}

NAME: NAVEEN N
USN:1OX12MCA57

Page no:

.NET Laboratory[10MCA57]

Output:

NAME: NAVEEN N
USN:1OX12MCA57

Page no:

.NET Laboratory[10MCA57]

Page no:

Program 6: Write a Program in C# to demonstrate Operator Overloading.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace q6
{
class abc
{
int a, b;
public abc(int x,int y)
{
a = x;
b = y;
}
public static abc operator +(abc x, abc y)
{
return new abc(x.a + y.a, x.b + y.b);
}
public void print()
{
Console.WriteLine("value of a={0},and values of b={1}", a, b);
}
}
class Program
{
static void Main(string[] args)
{
abc y = new abc(4, 9);
y.print();
abc yy = new abc(64, 16);
yy.print();
abc z = y + yy;
z.print();
Console.Read();
}
}
}

NAME: NAVEEN N
USN:1OX12MCA57

.NET Laboratory[10MCA57]

Output:

NAME: NAVEEN N
USN:1OX12MCA57

Page no:

.NET Laboratory[10MCA57]

Page no:

Program 7: Write a Program in C# to find the Second Largest Element in a Single


dimensional Array.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace q7
{
class Program
{
static void Main(string[] args)
{
{
int n,i;
Console.Write("ENTER LENGTH OF ARRAY : ");
n=int.Parse(Console.ReadLine());
int[] a = new int[n];
Console.WriteLine("** ENTER ARRAY ELEMENTS **");
for (i = 0; i < n; i++)
{
Console.Write("ENTER a[{0}] : ", i);
a[i]=int.Parse(Console.ReadLine());
}
int lar;
Array.Sort(a);
Array.Reverse(a);
lar = a[0];
for (i = 1; i < a.Length; i++)
{
if (lar != a[i])
{
lar = a[i];
break;
}
}
Console.WriteLine("THE SECOND LARGEST NUMBER IS :
{0}",lar);
Console.WriteLine("PRESS ENTER TO EXIT...!!!");
Console.Read();
}
}
}
NAME: NAVEEN N
USN:1OX12MCA57

.NET Laboratory[10MCA57]

Output:

NAME: NAVEEN N
USN:1OX12MCA57

Page no:

.NET Laboratory[10MCA57]

Page no:

Program 8: Write a Program in C# to multiply two matrices using Rectangular


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

namespace lab8
{
class MainClass
{
public static void Main (string[] args)
{
int [,] a=new int[10,10];
int [,] b=new int[10,10];
int [,] c=new int[10,10];
int m,n,p,q;

Console.WriteLine ("enter the order of 1st matrix \n");


m=int.Parse (Console.ReadLine ());
n=int.Parse (Console.ReadLine ());

Console.WriteLine ("enter the order of 2nd matrix \n");


p=int.Parse (Console.ReadLine ());
q=int.Parse (Console.ReadLine ());

if(n==p)
{
NAME: NAVEEN N
USN:1OX12MCA57

.NET Laboratory[10MCA57]

Page no:
Console.WriteLine ("enter the 1st matix elements \n");
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
a[i,j]=int.Parse (Console.ReadLine ());

Console.WriteLine ("enter the 2nd matix elements \n");


for(int i=0;i<p;i++)
for(int j=0;j<q;j++)
b[i,j]=int.Parse (Console.ReadLine ());

for(int i=0;i<m;i++)
for(int j=0;j<q;j++)
{
c[i,j]=0;
for(int k=0;k<m+q;k++)
c[i,j]=a[i,k]*b[k,j]+c[i,j];
}

for(int i=0;i<m;i++)
{
for(int j=0;j<q;j++)
Console.Write (c[i,j]+ "\t");
Console.WriteLine ();
}
}
else
Console.WriteLine ("incorrect matrix order \n");
Console.ReadKey ();
}
NAME: NAVEEN N
USN:1OX12MCA57

.NET Laboratory[10MCA57]
}
}

Output:

NAME: NAVEEN N
USN:1OX12MCA57

Page no:

.NET Laboratory[10MCA57]

Page no:

Program 9. Find the sum of all the elements present in a jagged array of 3 inner
arrays.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[][] a=new int[3][];
int p, q, r, sum = 0;
Console.WriteLine("enter the first array size \n");
p = int.Parse(Console.ReadLine());
Console.WriteLine("enter the second inner array size \n");
q = int.Parse(Console.ReadLine());
Console.WriteLine("enter the third inner array size \n");
r = int.Parse(Console.ReadLine());
a[0] = new int[p];
a[1] = new int[q];
a[2] = new int[r];
for (int i = 0; i < a.Length; i++)
{
Console.WriteLine("enter the array items of" + (i + 1) + "array \n");
for (int j = 0; j < a[i].Length; j++)
{
a[i][j] = int.Parse(Console.ReadLine());
sum = sum + a[i][j];
}
}
Console.WriteLine("sum is" + sum);
Console.ReadKey();
}
}
}

NAME: NAVEEN N
USN:1OX12MCA57

.NET Laboratory[10MCA57]

Output:

NAME: NAVEEN N
USN:1OX12MCA57

Page no:

.NET Laboratory[10MCA57]

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


using System;
using System.Collections.Generic;
using System.Text;
namespace q10
{
class Program
{
static void Main(string[] args)
{
string str,revstr="";
Console.WriteLine("enter the string ");
str = Console.ReadLine();
for (int i = str.Length-1; i >= 0; i--)
revstr += str[i];
Console.WriteLine("reverse of {0} is {1}",str,revstr);
Console.ReadLine();
}
}
}

Output:

NAME: NAVEEN N
USN:1OX12MCA57

Page no:

.NET Laboratory[10MCA57]

Page no:

Program 11: Using Try, Catch and finally Blocks write a program in c# to
demonstrate Error Handling. .
using System;
using System.Collections.Generic;
using System.Text;
namespace trycatch
{
class Program
{
static void Main(string[] args)
{
int a = 10, b = 5, c = 5;
int res;
try
{
res = a / (b - c);
Console.WriteLine("Result" + res);
}
catch(DivideByZeroException e)
{
Console.WriteLine("divide by zero");
}
finally
{
Console.WriteLine("End of the program");
Console.Read();
NAME: NAVEEN N
USN:1OX12MCA57

.NET Laboratory[10MCA57]

Page no:

}
}
}
}

Output:

Program 12: Design a simple calculator using Switch Statement in c#.


using System;
using System.Collections.Generic;
using System.Text;
namespace lab12
{
class Program
{
static void Main(string[] args)
{
double a, b, c;
int ch;
Console.WriteLine("enter 1st number");
a = double.Parse(Console.ReadLine());
Console.WriteLine("enter 2st number");
b = double.Parse(Console.ReadLine());
Console.WriteLine("\n 1.Add \n 2.Subtract\n 3.Multiply \n
4.Divide \n");
Console.WriteLine("enter your choice \n");
ch = int.Parse(Console.ReadLine());
switch (ch)
{
case 1: c = a + b;
Console.WriteLine("sum is " + c);
break;
case 2: c = a - b;
Console.WriteLine("difference is " + c);
break;
case 3: c = a * b;

NAME: NAVEEN N
USN:1OX12MCA57

.NET Laboratory[10MCA57]

Page no:

Console.WriteLine("product is " + c);


break;
case 4: c = a % b;
Console.WriteLine("remainder is" + c);
break;
default: Console.WriteLine("invalid choice \n");
break;
}
Console.ReadKey();
}

Output:

Program 13: Demonstrate use of virtual and override keywords in c# with a


simple program.
using System;
using System.Collections.Generic;
using System.Text;
namespace lab13
{
class Program
{
public virtual void display()
{
Console.WriteLine("Display is called by Base Class ");
}
}
class emp : Program
{
public override void display()
{
Console.WriteLine("Display Method Called By Derived Class");
}
}
class program1
{
static void Main(string[] args)
{
Program x = new Program();

NAME: NAVEEN N
USN:1OX12MCA57

.NET Laboratory[10MCA57]
x.display();
emp y = new emp();
y.display();
Console.ReadKey();
}

Output:

NAME: NAVEEN N
USN:1OX12MCA57

Page no:

.NET Laboratory[10MCA57]

Page no:

Program14: Implement linked lists in c# using the existing collections name space.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Lab14
{
class Program
{
static void Main(string[] args)
{
LinkedList<int> ll = new LinkedList<int>();
LinkedListNode<int> node;
int ch, x;
do
{
Console.WriteLine("\n1.Add First\n2.Add Last\n3.Remove First\n4.Remove
Last\n5.Display\n6.Exit");
Console.Write("Enter your choice : ");
ch = int.Parse(Console.ReadLine());
switch (ch)
{
case 1: Console.Write("Enter element to insert : ");
x = int.Parse(Console.ReadLine());
ll.AddFirst(x);
break;
case 2: Console.Write("Enter element to insert : ");
x = int.Parse(Console.ReadLine());
ll.AddLast(x);
break;
case 3: Console.WriteLine("First element removed");
ll.RemoveFirst();
break;
case 4: Console.WriteLine("Last element removed");
ll.RemoveLast();
break;
NAME: NAVEEN N
USN:1OX12MCA57

.NET Laboratory[10MCA57]

Page no:

case 5: Console.WriteLine("\nNumber of elements : " + ll.Count);


Console.WriteLine("Elements are");
for (node = ll.First; node != null; node = node.Next)
Console.Write(node.Value + " ");
break;
case 6: Environment.Exit(0);
break;
default: Console.WriteLine("Invalid choice !!");
break;
}
}
while (true);
}
}
}
Output.

NAME: NAVEEN N
USN:1OX12MCA57

.NET Laboratory[10MCA57]

NAME: NAVEEN N
USN:1OX12MCA57

Page no:

.NET Laboratory[10MCA57]

Page no:

Program 15: Write a program to demonstrate abstract class and abstract


methods in c#.
using System;
using System.Collections.Generic;
using System.Text;
namespace lab15
{
abstract class sample
{
public int a, b, c;
public abstract void sum();
public abstract void multy();
}
class sum1:sample
{
public void get(int aa, int bb)
{
a=aa;
b=bb;
}
public override void multy()
{
Console.WriteLine("Multyply of Two Numbers =:"+(a*b));
}
public override void sum()
{
Console.WriteLine("Sum of Two Numbers=;" + (a + b));
}
}

class Program
{
static void Main(string[] args)
{
sum1 x = new sum1();
x.get(5, 5);
x.multy();
x.sum();
Console.ReadKey();
}
}

NAME: NAVEEN N
USN:1OX12MCA57

.NET Laboratory[10MCA57]

Page no:

Output:

Program 16: Write a program to build a class which implements an


interface which already exists
using System;
using System.Collections.Generic;
using System.Text;
namespace lab16
{
interface ICloneable
{
void normal_break();
}
class newcar : ICloneable
{
public void normal_break()
{
Console.WriteLine("Normal Break Active");
}
}
class Program
{
static void Main(string[] args)
{
newcar aa = new newcar();
aa.normal_break();
Console.ReadKey();
}
}
}

Output:

NAME: NAVEEN N
USN:1OX12MCA57

.NET Laboratory[10MCA57]

Page no:

Program 17: Write a program to illustrate the use of different


properties of c#.
using System;
using System.Collections.Generic;
using System.Text;
namespace lab17
{
class student
{
int sno;
string sname;
public int stu_no
{
set
{
sno = value;
}
get
{
return sno;
}
}
public string stu_name
{
set
{
sname = value;
}
get
{
return sname;
}
}
}

NAME: NAVEEN N
USN:1OX12MCA57

.NET Laboratory[10MCA57]

Page no:

class Program
{
static void Main(string[] args)
{
student xx = new student();
xx.stu_no = 054;
xx.stu_name = "Shrikanth";
Console.WriteLine("student no={0} \n student name={1}",
xx.stu_no, xx.stu_name);
Console.Read();
}
}
}

Output:

NAME: NAVEEN N
USN:1OX12MCA57

.NET Laboratory[10MCA57]

Page no:

Program18: Demonstrate arrays of interface types with a C# program.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Lab18
{
interface shape
{
void draw();
double area();
}
class rectangle : shape
{
private double length, breadth;
public rectangle(double l, double b)
{
length = l;
breadth = b;
}
public void draw()
{
Console.WriteLine("Rectangle: Length:{0} Breadth:{1}", length, breadth);
Console.Write("Area : ");
}
public double area()
{
return length * breadth;
}
}
public class circle : shape
{
private double radius;
public circle(double r)
{
radius = r;
}
public void draw()
{
Console.WriteLine("\nCircle Radius: " + radius);
Console.Write("Area : ");
}
public double area()
{
return (3.14 * radius * radius);
}
}
NAME: NAVEEN N
USN:1OX12MCA57

.NET Laboratory[10MCA57]
class Program
{
static void Main(string[] args)
{
shape[] s1 = { new rectangle(15, 25), new circle(5) };
for (int i = 0; i < s1.Length; i++)
{
s1[i].draw();
Console.WriteLine(s1[i].area());
}
Console.ReadLine();
}
}
}
Output:

NAME: NAVEEN N
USN:1OX12MCA57

Page no:

.NET Laboratory[10MCA57]

NAME: NAVEEN N
USN:1OX12MCA57

Page no: