The document contains source code for 7 C# programs assigned as exercises in a .NET laboratory course. Each program is presented with its problem statement, source code, and an output section. The programs include: 1) Checking if a number is a palindrome, 2) Processing command line arguments, 3) Finding roots of a quadratic equation, 4) Demonstrating boxing and unboxing, 5) Implementing stack operations, 6) Demonstrating operator overloading, and 7) Finding the second largest element in an array. For each program, the source code implements the given task.
The document contains source code for 7 C# programs assigned as exercises in a .NET laboratory course. Each program is presented with its problem statement, source code, and an output section. The programs include: 1) Checking if a number is a palindrome, 2) Processing command line arguments, 3) Finding roots of a quadratic equation, 4) Demonstrating boxing and unboxing, 5) Implementing stack operations, 6) Demonstrating operator overloading, and 7) Finding the second largest element in an array. For each program, the source code implements the given task.
The document contains source code for 7 C# programs assigned as exercises in a .NET laboratory course. Each program is presented with its problem statement, source code, and an output section. The programs include: 1) Checking if a number is a palindrome, 2) Processing command line arguments, 3) Finding roots of a quadratic equation, 4) Demonstrating boxing and unboxing, 5) Implementing stack operations, 6) Demonstrating operator overloading, and 7) Finding the second largest element in an array. For each program, the source code implements the given task.
1. WRITE A PROGRAM IN C# TO CHECK WHETHER A NUMBER IS PALINDROME OR NOT.
SOURCE CODE:
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace Palindrome { class Program { static void Main(string[] args) {
int num,temp; int digit; int reverse = 0; Console.WriteLine("Enter a number"); num = int.Parse(Console.ReadLine()); temp=num; while(num!=0) { digit = num % 10; reverse = reverse * 10 + digit; num=num /= 10; } Console.WriteLine("The reverse of the number is: {0}",reverse); if (temp == reverse) { Console.WriteLine("This number is a palindrome!"); Console.ReadLine(); } else { Console.WriteLine("This number is not a palindrome"); Console.ReadLine(); } } } }
.Net Laboratory [10MCA57]
Ganesh Hegde ganesh.hsirsi@gmail.com 2
OUTPUT:
.Net Laboratory [10MCA57]
Ganesh Hegde ganesh.hsirsi@gmail.com 3
2. WRITE A PROGRAM IN C# TO DEMONSTRATE COMMAND LINE ARGUMENTS PROCESSING
SOURCE CODE:
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace Lab2 { class Program { static void Main(string[] args) {
Console.WriteLine("\nNumber of CommadLine Arguments :" + args.Length); Console.Write("\nCommandline Arguments Are :\t"); for (int i = 0; i < args.Length; i++) { Console.Write(args[i] + "\t"); } Console.ReadLine(); } } }
.Net Laboratory [10MCA57]
Ganesh Hegde ganesh.hsirsi@gmail.com 4
OUTPUT:
.Net Laboratory [10MCA57]
Ganesh Hegde ganesh.hsirsi@gmail.com 5
3. WRITE A PROGRAM IN C# TO FIND THE ROOTS OF QUADRATIC EQUATION.
SOURCE CODE:
using System; namespace QuadRoots { class Program {
public static void Main() { float a, b, c; double disc, deno, x1, x2;
Console.WriteLine("ENTER THE VALUES OF A,B,C..."); a = float.Parse(Console.ReadLine()); b = float.Parse(Console.ReadLine()); c = float.Parse(Console.ReadLine());
if (a == 0) { x1 = -c / b; Console.WriteLine("The roots are Linear:", x1); }
else { disc = (b * b) - (4 * a * c); deno = 2 * a; if (disc > 0) { Console.WriteLine("THE ROOTS ARE REAL AND DISTINCT ROOTS"); x1 = (-b / deno) + (Math.Sqrt(disc) / deno); x2 = (-b / deno) - (Math.Sqrt(disc) / deno); Console.WriteLine("THE ROOTS ARE... " + x1 + " and " + x2); }
4. WRITE A PROGRAM IN C# TO DEMONSTRATE BOXING AND UNBOXING.
SOURCE CODE:
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace Boxing { class Program { static void Main(string[] args) { int m = 10; object a = m; // boxing try {
Console.WriteLine("Value of m is:" + a); object n = 20; int b = (int)n; // attempt to unbox Console.WriteLine("Value of n is:" + b); System.Console.WriteLine("Unboxing OK."); Console.ReadLine();
5. WRITE A PROGRAM IN C# TO IMPLEMENT STACK OPERATIONS
SOURCE CODE:
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace Stack {
class Program { static void Main(string[] args) { int top = -1; int[] s = new int[10]; Console.WriteLine("Enter The Size of The Stack"); int MAX = int.Parse(Console.ReadLine()); while (true) { Console.WriteLine("1.Push"); Console.WriteLine("2.Pop"); Console.WriteLine("3.Display"); Console.WriteLine("4.Exit");
Console.WriteLine("Enter your choice :"); int ch = int.Parse(Console.ReadLine());
switch (ch) { case 1: if (top > MAX - 1) Console.WriteLine("... Stack Overflow ..."); else { Console.WriteLine("Enter the item :"); int n = int.Parse(Console.ReadLine()); s[++top] = n; }
6. WRITE A PROGRAM TO DEMONSTRATE OPERATOR OVERLOADING.
SOURCE CODE:
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace Overload { public struct addOpp { private double a; public addOpp(double a) { this.a = a;
} public override string ToString() { return string.Format("{0}",a);
}
static public addOpp operator +(addOpp lhs, addOpp rhs) { return new addOpp(lhs.a + rhs.a);
} }
class Program { static void Main(string[] args) { Console.WriteLine("Enter Two Numbers"); addOpp c1 = new addOpp(double.Parse(Console.ReadLine())); addOpp c2 = new addOpp(double.Parse(Console.ReadLine())); addOpp c3 = c1 + c2; Console.WriteLine("First Value is {0}", c1); Console.WriteLine("Second Value is {0}", c2); Console.WriteLine("Addition is {0}", c3); Console.ReadLine();
} }
}
.Net Laboratory [10MCA57]
Ganesh Hegde ganesh.hsirsi@gmail.com 13
OUTPUT:
.Net Laboratory [10MCA57]
Ganesh Hegde ganesh.hsirsi@gmail.com 14
7. WRITE A PROGRAM IN C# TO FIND THE SECOND LARGEST ELEMENT IN A SINGLE DIMENSIONAL ARRAY.
SOURCE CODE:
using System; using System.Collections.Generic; using System.Text; namespace secondLarge { class Program { static void Main(string[] args) { int[] a = new int[10]; int i, j; Console.WriteLine("Enter the No. of Elements"); int n = int.Parse(Console.ReadLine()); Console.WriteLine("Enter the Elements"); for(i=0;i<n;i++) { a[i]=int.Parse(Console.ReadLine()); } for(i=0;i<n;i++) for (j = 0; j < n - 1; j++) { if(a[j]<a[j+1]) { int temp = a[j]; a[j]=a[j+1]; a[j+1]=temp;
} } i = 1; while (a[i] == a[0]) i++; if (i >= n) { Console.WriteLine("Second Largest Element Does Not Exist"); Console.ReadLine(); } else { Console.WriteLine("Second Largest Element is "+a[i]); Console.ReadLine(); }
} } }
.Net Laboratory [10MCA57]
Ganesh Hegde ganesh.hsirsi@gmail.com 15
OUTPUT:
.Net Laboratory [10MCA57]
Ganesh Hegde ganesh.hsirsi@gmail.com 16
8. WRITE A PROGRAM IN C# TO MULTIPLY TO MATRICES USING RECTANGULAR ARRAYS
SOURCE CODE:
using System; class MatrixMultiplication { int[,] a; int[,] b; int[,] c; public int m1, n1, m2, n2; public void ReadMatrix() { Console.WriteLine("\n Size of Matrix 1:"); Console.Write("\n Enter the number of rows in Matrix 1 :"); m1 = int.Parse(Console.ReadLine()); Console.Write("\n Enter the number of columns in Matrix 1 :"); n1 = int.Parse(Console.ReadLine()); a = new int[m1, n1]; Console.WriteLine("\n Size of Matrix 2 :"); Console.Write("\n Enter the number of rows in Matrix 2 :"); m2 = int.Parse(Console.ReadLine()); Console.Write("\n Enter the number of columns in Matrix 2 :"); n2 = int.Parse(Console.ReadLine()); b = new int[m2, n2]; if (n1 != m2) { Console.WriteLine("columns of A & Rows of B matrix are not equal"); Console.ReadLine(); Environment.Exit(0); } else { Console.WriteLine("\n Enter the elements of Matrix 1:"); for (int i = 0; i < m1; i++) { for (int j = 0; j < n1; j++) { a[i, j] = int.Parse(Console.ReadLine()); } } Console.WriteLine("\n Enter the elements of Matrix 2:"); for (int i = 0; i < m2; i++) { for (int j = 0; j < n2; j++) { b[i, j] = int.Parse(Console.ReadLine()); } } } }
public void PrintMatrix() { Console.WriteLine("\n Matrix 1:");
for (int i = 0; i < m1; i++) { .Net Laboratory [10MCA57]
Ganesh Hegde ganesh.hsirsi@gmail.com 17
for (int j = 0; j < n1; j++) { Console.Write("\t" + a[i, j]); } Console.WriteLine(); } Console.WriteLine("\n Matrix 2:"); for (int i = 0; i < m2; i++) { for (int j = 0; j < n2; j++) { Console.Write("\t" + b[i, j]); } Console.WriteLine(); } Console.WriteLine("\n Resultant Matrix after multiplying:"); for (int i = 0; i < m1; i++) { for (int j = 0; j < n2; j++) { Console.Write("\t" + c[i, j]); } Console.WriteLine(); } Console.ReadLine(); } public void MultiplyMatrix() { c = new int[m1, n2]; for (int i = 0; i < m1; i++) { for (int j = 0; j < n2; j++) { c[i, j] = 0; for (int k = 0; k < n1; k++) c[i, j] = c[i, j] + a[i, k] * b[k, j]; } } } } class Matrices{ public static void Main() { MatrixMultiplication MM = new MatrixMultiplication(); MM.ReadMatrix(); MM.MultiplyMatrix(); MM.PrintMatrix(); } }
.Net Laboratory [10MCA57]
Ganesh Hegde ganesh.hsirsi@gmail.com 18
OUTPUT:
.Net Laboratory [10MCA57]
Ganesh Hegde ganesh.hsirsi@gmail.com 19
9. FIND THE SUM OF ALL THE ELEMENTS PRESENT IN A JAGGED ARRAY OF 3 INNER ARRAYS
SOURCE CODE:
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace JagArray { class Program { static void Main(string[] args) { int[][] jarr = new int[3][]; int s = 0; for (int i = 0; i < 3; i++) { Console.WriteLine("Enter the Size of the Array" +(i + 1)); int n = int.Parse(Console.ReadLine()); jarr[i] = new int[n]; Console.WriteLine("Enter the Values of Array " +(i + 1)); for (int j = 0; j < n; j++) { jarr[i][j] = int.Parse(Console.ReadLine()); s = s + jarr[i][j]; } n = n + 0;
10. WRITE A PROGRAM TO REVERSE A GIVEN STRING USING C#.
SOURCE CODE:
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace ReverseStr { class Program { static void Main(string[] args) { Console.WriteLine("Enter the String :"); string a = Console.ReadLine(); int len = a.Length; Console.Write("The Reverse of String is :"); for (int i = len - 1; i >= 0; i--) { Console.Write(a[i]); } Console.WriteLine(); Console.ReadLine(); } } }
.Net Laboratory [10MCA57]
Ganesh Hegde ganesh.hsirsi@gmail.com 22
OUTPUT:
.Net Laboratory [10MCA57]
Ganesh Hegde ganesh.hsirsi@gmail.com 23
11. USING TRY, CATCH AND FINALLY BLOCKS WRITE A PROGRAM IN C# TO DEMONSTRATE ERROR HANDLING.
SOURCE CODE:
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace TryCatch { class Program { static void Main(string[] args) { int[] a = new int[3]; int n = args.Length; try { if (n == 0) { int d = 10 / (n); } if (n == 1) { a[4] =6; } }
catch (IndexOutOfRangeException e) { Console.WriteLine("Exception"+e); } catch (DivideByZeroException e) { Console.WriteLine("DivideByZeroException"+e); } finally { Console.WriteLine("finally block :: End of Program");
}
Console.ReadLine(); } } }
.Net Laboratory [10MCA57]
Ganesh Hegde ganesh.hsirsi@gmail.com 24
OUTPUT:
.Net Laboratory [10MCA57]
Ganesh Hegde ganesh.hsirsi@gmail.com 25
12. DESIGN A SIMPLE CALCULATOR USING SWITCH STATEMENT IN C#.
SOURCE CODE:
using System; using System.Collections.Generic; using System.Text;
namespace SimpleCalc { class Program { static void Main(string[] args) {
float a, b; int ch;
Console.Write("Enter The First No.: "); a = float.Parse(Console.ReadLine()); Console.Write("\nEnter the Second No.: "); b = float.Parse(Console.ReadLine()); while (true) { Console.WriteLine("===============================");
Console.WriteLine("1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n5.Moduler Division\n6.Square\n7.Square Root\n8.Exit"); Console.WriteLine("==============================="); Console.Write("Enter your Choice : "); ch = int.Parse(Console.ReadLine()); switch (ch) { case 1: Console.WriteLine("Addition :" + a + "+" + b + "=" + (a + b)); break; case 2: Console.WriteLine("Subtraction :" + a + "-" + b + "=" + (a - b)); break; case 3: Console.WriteLine("Multiplication :" + a + "*" + b + "=" + (a * b)); break; case 4: Console.WriteLine("Division :" + a + "/" + b + "=" + (a / b)); break; case 5: Console.WriteLine("Moduler Division:"+a+"%" + b + "=" + (a % b)); break; case 6: Console.WriteLine("Square(" + a + ") =" + (a * a)); break; case 7: Console.WriteLine("SquareRoot(" + a + ") =" + Math.Sqrt(a)); break; default: Console.WriteLine("Invalid Input"); Environment.Exit(0); break;
} } } } }
.Net Laboratory [10MCA57]
Ganesh Hegde ganesh.hsirsi@gmail.com 26
OUTPUT:
.Net Laboratory [10MCA57]
Ganesh Hegde ganesh.hsirsi@gmail.com 27
13. DEMONSTRATE USE OF VIRTUAL AND OVERRIDE KEY WORDS IN C# WITH A SIMPLE PROGRAM
SOURCE CODE:
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace VirtualKey {
class person { protected string fname; protected string lname; public person(string fn, string ln) { fname = fn; lname = ln; } public virtual void display() { Console.WriteLine("Person :" + fname + " " + lname);
}
}
class emp : person { public ushort year; public emp(string fn, string ln, ushort yr) : base(fn, ln) { year = yr;
} public override void display() { Console.WriteLine("Employee :"+fname+" "+lname+" "+year); } } class worker : person { public String company; public worker(string fn, string ln, string c):base(fn, ln) { company=c;
static void Main(string[] args) { Console.WriteLine("\n\n*** VIRTUAL AND OVERRIDE KEYWORDS DEMO ***"); person p1 = new person("RAM", "KUMAR"); person p2 = new emp("RAM", "KUMAR",2012); person p3 = new worker("RAM", "KUMAR","ABC TECH SOLS"); p1.display(); p2.display(); p3.display(); Console.ReadLine(); } } }
.Net Laboratory [10MCA57]
Ganesh Hegde ganesh.hsirsi@gmail.com 29
OUTPUT:
.Net Laboratory [10MCA57]
Ganesh Hegde ganesh.hsirsi@gmail.com 30
14. IMPLEMENT LINKED LISTS IN C# USING THE EXISTING COLLECTIONS NAME SPACE.
SOURCE CODE:
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace LinkList { class Program { static LinkedList<int> ll = new LinkedList<int>(); static LinkedListNode<int> node; static void Main(string[] args) {
Console.WriteLine(" LINKED LIST DEMO"); int ch, x; Console.WriteLine("Linked List Operations");
Console.WriteLine("1.AddFirst\n2.AddLast\n3.RemoveFirst\n4.RemoveLast\n5.Remove Specified\n6.Display\n7..Exit"); while (true) { Console.Write("Enter your Choice : "); ch = int.Parse(Console.ReadLine()); switch (ch) {
case 1: Console.Write("Enter the Element to AddFirst : ");
x = int.Parse(Console.ReadLine()); ll.AddFirst(x); display(); break; case 2: Console.WriteLine("Enter the Element to AddLast : "); x = int.Parse(Console.ReadLine()); ll.AddLast(x); display(); break; case 3: if (ll.Count == 0) { Console.WriteLine("Nothing to Delete...!!!"); break; } else { Console.WriteLine("First Element Removed Successfully"); ll.RemoveFirst(); display();
.Net Laboratory [10MCA57]
Ganesh Hegde ganesh.hsirsi@gmail.com 31
break; } case 4: if (ll.Count == 0) { Console.WriteLine("Nothing to Delete...!!!"); break; } else { Console.WriteLine("Last Element Removed Successfully"); ll.RemoveLast(); display();
break; } case 5: if (ll.Count == 0) { Console.WriteLine("Nothing to Delete...!!!"); break; } else { Console.WriteLine("Enter the Element to Remove"); x = int.Parse(Console.ReadLine()); bool b=ll.Remove(x);
15. WRITE A PROGRAM TO DEMONSTRATE ABSTRACT CLASS AND ABSTRACT METHODS IN C#.
SOURCE CODE:
using System; namespace TryCatch { abstract class person { protected string fname; protected string lname; public person(string fn, string ln) { fname = fn; lname = ln; } public abstract void display() { Console.WriteLine("Person :" + fname + " " + lname); } }
class emp : person { public ushort year; public emp(string fn, string ln, ushort yr) : base(fn, ln) { year = yr;
} public override void display() { Console.WriteLine("Employee :" + fname + " " + lname + " " + year); } } class worker : person { public String company; public worker(string fn, string ln, string c) : base(fn, ln) { company = c;
} public override void display() { Console.WriteLine("Worker :" + fname + " " + lname + " " + company); } } class Program { static void Main(string[] args) { Console.WriteLine("**ABSTRACT CLASS AND ABSTRACT METHODS DEMO **"); person p2 = new emp("RAM", "KUMAR", 2012); person p3 = new worker("RAM", "KUMAR", "ABC TECH SOLS"); p2.display(); p3.display(); Console.ReadLine(); } } }
.Net Laboratory [10MCA57]
Ganesh Hegde ganesh.hsirsi@gmail.com 35
OUTPUT:
.Net Laboratory [10MCA57]
Ganesh Hegde ganesh.hsirsi@gmail.com 36
16. WRITE A PROGRAM IN C# TO BUILD A CLASS WHICH IMPLEMENTS AN INTERFACE WHICH ALREADY EXISTS.
SOURCE CODE:
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace Infc { class Point:ICloneable { public int x, y; public Point(int x, int y) { this.x = x; this.y = y;
} public object Clone() {
return new Point(this.x, this.y); } public override string ToString() { return string.Format("X= {0}; y={1}", x, y);
}
} class Porgram{ static void Main(string[] args) { Point p1 =new Point(10,10); Point p2 =(Point)p1.Clone();
p2.x = 20;
Console.WriteLine(p1); Console.WriteLine(p2);
Console.Read(); } } }
.Net Laboratory [10MCA57]
Ganesh Hegde ganesh.hsirsi@gmail.com 37
OUTPUT:
.Net Laboratory [10MCA57]
Ganesh Hegde ganesh.hsirsi@gmail.com 38
17. WRITE A PROGRAM TO ILLUSTRATE THE USE OF DIFFERENT PROPERTIES IN C#.
SOURCE CODE:
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace Properties { class point { int getx, gety; public int x { get { return getx; } set { getx = value; } } public int y { get { return gety; } set { gety = value; } } } class Program { static void Main(string[] args) {
point start = new point(); point end = new point(); start.x = 10; start.y = 20; end.x = 100; end.y = 200; Console.Write("\npoint 1 :" + start.x + " " + end.x); Console.Write("\npoint 2 :" + start.y + " " + end.y); Console.ReadLine(); } } }
.Net Laboratory [10MCA57]
Ganesh Hegde ganesh.hsirsi@gmail.com 39
OUTPUT:
.Net Laboratory [10MCA57]
Ganesh Hegde ganesh.hsirsi@gmail.com 40
18. DEMONSTRATE ARRAYS OF INTERFACE TYPES WITH A C# PROGRAM.
SOURCE CODE:
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace IntrDemo {
public interface Shape { void area();
} public class Circle : Shape { public void area() { Console.WriteLine("*** Calculating Area of Circle ***"); Console.Write("Enter the Radius:"); float r = float.Parse(Console.ReadLine()); Console.WriteLine("Area of Circle = " + (3.142 * r * r)); } } public class Square : Shape { public void area() { Console.WriteLine("*** Calculating Area of Square ***"); Console.Write("Enter the Length:"); float side = float.Parse(Console.ReadLine()); Console.WriteLine("Area of Square = " + (side * side)); } }
class Program { static void Main(string[] args) { Console.WriteLine("*** Arrays of Inerface Demo ***"); Shape[] s = new Shape[2]; s[0] = new Circle(); s[1] = new Square();
for (int i = 0; i < s.Length; i++) { s[i].area(); Console.ReadLine(); } }