.
NET Laboratory[10MCA57]
Page no:
Program1: Write a program in C# to check whether a number is Palindrome or not.
using System;
using [Link];
using [Link];
namespace q8
{
class Program
{
static void Main(string[] args)
{
int n,m, i=0;
[Link](" enter the number");
m= n = [Link]([Link]());
while (n > 0)
{
int rem = n % 10;
i = i * 10 + rem;
n /= 10;
}
if (i == m)
[Link]("{0} is palindrom no ",m);
else
[Link]("{0} is not palindrom no ",m);
[Link]();
}
}
}
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 [Link];
using [Link];
using [Link];
namespace q2
{
class Program
{
static void Main(string[] args)
{
if ([Link] > 0)
foreach (string var in args)
NAME: NAVEEN N
USN:1OX12MCA57
.NET Laboratory[10MCA57]
[Link]("\t " + var);
else
[Link]("argument is not passed !");
[Link]();
}
}
}
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 [Link];
using [Link];
namespace ConsoleApplication8
{
class Program
{
static void Main(string[] args)
{
int sum=0;
if ([Link] > 0)
{
[Link]();
for (int i =0;i<[Link];i++)
sum += [Link](args[i]);
[Link]("The Sum of Arguments passed is:{0}",sum);
}
else
[Link]("argument is not passed !");
[Link]();
}
}
}
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 [Link];
using [Link];
namespace lab3
{
class Program
{
static void Main(string[] args)
{
double a, b, c, d;
[Link]("enter 3 numbers \n");
a = [Link]([Link]());
b = [Link]([Link]());
c = [Link]([Link]());
if (a == 0 || a < 0)
[Link]("invalid values \n");
else
{
d = b * b - 4 * a * c;
if (d > 0)
{
[Link]("roots are real and distinct \n");
[Link]("roots : {0}", -b + [Link](d) / (2 * a));
[Link]("roots : {0}", -b - [Link](d) / (2 * 1));
}
else if (d == 0)
{
[Link]("roots are equal \n");
[Link]("roots :{0}", -b / (2 * a));
}
else
[Link]("roots are imaginary \n");
}
[Link]();
}
}
}
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 [Link];
using [Link];
using [Link];
namespace q4
{
class Program
{
static void Main(string[] args)
{
int a = 10;
object x ;
[Link]("value of a={0}", a);
x = a;
[Link]("value of object x={0}", x);
x = 100;
a =(int) x;
[Link]("value of object x={0}", x);
[Link]("value of a={0}", a);
[Link]();
}
}
}
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 [Link];
using [Link];
using [Link];
namespace stack_operation
{
class Program
{
static void Main(string[] args)
{
stack_operation s1 = new stack_operation();
int ch;
while (true)
{
[Link]("\n [Link] \n [Link] \n [Link] \n [Link] \n");
[Link]("enter yout choice \n");
ch = [Link]([Link]());
switch (ch)
{
case 1: [Link]();
break;
case 2: [Link]();
break;
case 3: [Link]();
break;
case 4: [Link](0);
break;
default: [Link]("\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()
{
[Link]("enter new element to push \n");
int ele=[Link]([Link]());
if(top==4)
[Link]("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)
[Link]("stack underflow \n");
else
{
[Link]("popped element is "+stk[top]);
top=top-1;
}
}
public void display()
{
if(top==-1)
[Link]("no element is in stack");
else
{
[Link]("elements in stack are \n");
for(int i=0;i<=top;i++)
[Link](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 [Link];
using [Link];
using [Link];
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()
{
[Link]("value of a={0},and values of b={1}", a, b);
}
}
class Program
{
static void Main(string[] args)
{
abc y = new abc(4, 9);
[Link]();
abc yy = new abc(64, 16);
[Link]();
abc z = y + yy;
[Link]();
[Link]();
}
}
}
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 [Link];
using [Link];
using [Link];
namespace q7
{
class Program
{
static void Main(string[] args)
{
{
int n,i;
[Link]("ENTER LENGTH OF ARRAY : ");
n=[Link]([Link]());
int[] a = new int[n];
[Link]("** ENTER ARRAY ELEMENTS **");
for (i = 0; i < n; i++)
{
[Link]("ENTER a[{0}] : ", i);
a[i]=[Link]([Link]());
}
int lar;
[Link](a);
[Link](a);
lar = a[0];
for (i = 1; i < [Link]; i++)
{
if (lar != a[i])
{
lar = a[i];
break;
}
}
[Link]("THE SECOND LARGEST NUMBER IS :
{0}",lar);
[Link]("PRESS ENTER TO EXIT...!!!");
[Link]();
}
}
}
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 [Link];
using [Link];
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;
[Link] ("enter the order of 1st matrix \n");
m=[Link] ([Link] ());
n=[Link] ([Link] ());
[Link] ("enter the order of 2nd matrix \n");
p=[Link] ([Link] ());
q=[Link] ([Link] ());
if(n==p)
{
NAME: NAVEEN N
USN:1OX12MCA57
.NET Laboratory[10MCA57]
Page no:
[Link] ("enter the 1st matix elements \n");
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
a[i,j]=[Link] ([Link] ());
[Link] ("enter the 2nd matix elements \n");
for(int i=0;i<p;i++)
for(int j=0;j<q;j++)
b[i,j]=[Link] ([Link] ());
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++)
[Link] (c[i,j]+ "\t");
[Link] ();
}
}
else
[Link] ("incorrect matrix order \n");
[Link] ();
}
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 [Link];
using [Link];
using [Link];
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[][] a=new int[3][];
int p, q, r, sum = 0;
[Link]("enter the first array size \n");
p = [Link]([Link]());
[Link]("enter the second inner array size \n");
q = [Link]([Link]());
[Link]("enter the third inner array size \n");
r = [Link]([Link]());
a[0] = new int[p];
a[1] = new int[q];
a[2] = new int[r];
for (int i = 0; i < [Link]; i++)
{
[Link]("enter the array items of" + (i + 1) + "array \n");
for (int j = 0; j < a[i].Length; j++)
{
a[i][j] = [Link]([Link]());
sum = sum + a[i][j];
}
}
[Link]("sum is" + sum);
[Link]();
}
}
}
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 [Link];
using [Link];
namespace q10
{
class Program
{
static void Main(string[] args)
{
string str,revstr="";
[Link]("enter the string ");
str = [Link]();
for (int i = [Link]-1; i >= 0; i--)
revstr += str[i];
[Link]("reverse of {0} is {1}",str,revstr);
[Link]();
}
}
}
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 [Link];
using [Link];
namespace trycatch
{
class Program
{
static void Main(string[] args)
{
int a = 10, b = 5, c = 5;
int res;
try
{
res = a / (b - c);
[Link]("Result" + res);
}
catch(DivideByZeroException e)
{
[Link]("divide by zero");
}
finally
{
[Link]("End of the program");
[Link]();
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 [Link];
using [Link];
namespace lab12
{
class Program
{
static void Main(string[] args)
{
double a, b, c;
int ch;
[Link]("enter 1st number");
a = [Link]([Link]());
[Link]("enter 2st number");
b = [Link]([Link]());
[Link]("\n [Link] \n [Link]\n [Link] \n
[Link] \n");
[Link]("enter your choice \n");
ch = [Link]([Link]());
switch (ch)
{
case 1: c = a + b;
[Link]("sum is " + c);
break;
case 2: c = a - b;
[Link]("difference is " + c);
break;
case 3: c = a * b;
NAME: NAVEEN N
USN:1OX12MCA57
.NET Laboratory[10MCA57]
Page no:
[Link]("product is " + c);
break;
case 4: c = a % b;
[Link]("remainder is" + c);
break;
default: [Link]("invalid choice \n");
break;
}
[Link]();
}
Output:
Program 13: Demonstrate use of virtual and override keywords in c# with a
simple program.
using System;
using [Link];
using [Link];
namespace lab13
{
class Program
{
public virtual void display()
{
[Link]("Display is called by Base Class ");
}
}
class emp : Program
{
public override void display()
{
[Link]("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]
[Link]();
emp y = new emp();
[Link]();
[Link]();
}
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 [Link];
using [Link];
using [Link];
namespace Lab14
{
class Program
{
static void Main(string[] args)
{
LinkedList<int> ll = new LinkedList<int>();
LinkedListNode<int> node;
int ch, x;
do
{
[Link]("\[Link] First\[Link] Last\[Link] First\[Link]
Last\[Link]\[Link]");
[Link]("Enter your choice : ");
ch = [Link]([Link]());
switch (ch)
{
case 1: [Link]("Enter element to insert : ");
x = [Link]([Link]());
[Link](x);
break;
case 2: [Link]("Enter element to insert : ");
x = [Link]([Link]());
[Link](x);
break;
case 3: [Link]("First element removed");
[Link]();
break;
case 4: [Link]("Last element removed");
[Link]();
break;
NAME: NAVEEN N
USN:1OX12MCA57
.NET Laboratory[10MCA57]
Page no:
case 5: [Link]("\nNumber of elements : " + [Link]);
[Link]("Elements are");
for (node = [Link]; node != null; node = [Link])
[Link]([Link] + " ");
break;
case 6: [Link](0);
break;
default: [Link]("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 [Link];
using [Link];
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()
{
[Link]("Multyply of Two Numbers =:"+(a*b));
}
public override void sum()
{
[Link]("Sum of Two Numbers=;" + (a + b));
}
}
class Program
{
static void Main(string[] args)
{
sum1 x = new sum1();
[Link](5, 5);
[Link]();
[Link]();
[Link]();
}
}
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 [Link];
using [Link];
namespace lab16
{
interface ICloneable
{
void normal_break();
}
class newcar : ICloneable
{
public void normal_break()
{
[Link]("Normal Break Active");
}
}
class Program
{
static void Main(string[] args)
{
newcar aa = new newcar();
aa.normal_break();
[Link]();
}
}
}
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 [Link];
using [Link];
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";
[Link]("student no={0} \n student name={1}",
xx.stu_no, xx.stu_name);
[Link]();
}
}
}
Output:
NAME: NAVEEN N
USN:1OX12MCA57
.NET Laboratory[10MCA57]
Page no:
Program18: Demonstrate arrays of interface types with a C# program.
using System;
using [Link];
using [Link];
using [Link];
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()
{
[Link]("Rectangle: Length:{0} Breadth:{1}", length, breadth);
[Link]("Area : ");
}
public double area()
{
return length * breadth;
}
}
public class circle : shape
{
private double radius;
public circle(double r)
{
radius = r;
}
public void draw()
{
[Link]("\nCircle Radius: " + radius);
[Link]("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 < [Link]; i++)
{
s1[i].draw();
[Link](s1[i].area());
}
[Link]();
}
}
}
Output:
NAME: NAVEEN N
USN:1OX12MCA57
Page no:
.NET Laboratory[10MCA57]
NAME: NAVEEN N
USN:1OX12MCA57
Page no: