C# Lab
C# Lab
NET Laboratory[10MCA57]
Page no:
Output:
NAME: NAVEEN N
USN:1OX12MCA57
.NET Laboratory[10MCA57]
Page no:
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:
Output:
NAME: NAVEEN N
USN:1OX12MCA57
.NET Laboratory[10MCA57]
Page no:
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]
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:
NAME: NAVEEN N
USN:1OX12MCA57
.NET Laboratory[10MCA57]
Output:
NAME: NAVEEN N
USN:1OX12MCA57
Page no:
.NET Laboratory[10MCA57]
Page no:
.NET Laboratory[10MCA57]
Output:
NAME: NAVEEN N
USN:1OX12MCA57
Page no:
.NET Laboratory[10MCA57]
Page no:
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;
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 ());
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]
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:
NAME: NAVEEN N
USN:1OX12MCA57
.NET Laboratory[10MCA57]
Page no:
Output:
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:
NAME: NAVEEN N
USN:1OX12MCA57
.NET Laboratory[10MCA57]
NAME: NAVEEN N
USN:1OX12MCA57
Page no:
.NET Laboratory[10MCA57]
Page no:
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:
Output:
NAME: NAVEEN N
USN:1OX12MCA57
.NET Laboratory[10MCA57]
Page no:
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:
.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: