Programs
Programs
Programs
NET LABORATORY—10MCA57
CONTENT
Sl Date Name of Program Page
No. No.
9 Find the sum of all the elements present in a jagged array of 3 inner arrays.
Teacher Sign……………………………………………….
1
.NET LABORATORY—10MCA57
Program:
using System;
using System.Collections.Generic;
using System.Text;
namespace LABPROGRAMS
{
class PALINDROME1
{
public static void Main(string[] args)
{
Console.WriteLine("\n\nEnter n value");
n = int.Parse(Console.ReadLine());
p = n;
while (n > 0)
{
rem = n % 10;
n = n / 10;
rev = (rev * 10) + rem;
}
if (p == rev)
Console.WriteLine("The value {0} is palindrome", rev);
else
Console.WriteLine("The value {0} is not palindrome", rev);
Console.ReadLine();
}
2
.NET LABORATORY—10MCA57
Program:
using System;
using System.Collections.Generic;
using System.Text;
namespace LABPROGRAMS
{
class CMDLINE2
{
static void Main(string[] args)
{
int n, i;
Console.WriteLine("PROGRAM TO IMPLEMENT COMMAND LINE
ARGUMENTS\n\n");
if (args.Length == 0)
{
Console.WriteLine("NO ARGUMENTS SUBMITTED");
Console.ReadLine();
}
}
}
}
3
.NET LABORATORY—10MCA57
Program:
using System;
namespace C3
{
class Program
{
static void Main(string[] args)
{
int a, b, c,d;
double r1,r2;
Console.WriteLine("Let the quadratic equation is ax2+bx+c");
Console.WriteLine("Enter a ->");
a=int.Parse (Console.ReadLine());
Console.WriteLine("Enter b ->");
b = int.Parse(Console.ReadLine());
Console.WriteLine("Enter c ->");
c = int.Parse(Console.ReadLine());
d = (b * b) - 4 * a * c;
if (d > 0)
{
Console.WriteLine("Roots will be real numbers");
r1 = (double)(-b + Math.Sqrt(d)) / 2 * a;
r2 = (double) (-b - Math.Sqrt(d)) / 2 * a;
Console.WriteLine("root1={0}", r1);
Console.WriteLine("root2={0}", r2);
}
else
{
Console.WriteLine(" Roots will be imaginary");
}
Console.ReadLine();
}
}
}
4
.NET LABORATORY—10MCA57
Program:
using System;
namespace C4
{
class Program
{
static void Main(string[] args)
{
int i = 8;
object o = i; // boxing
int j = (int)o; // unboxing
Console.WriteLine("o={0}", o);
Console.WriteLine("j={0}", j);
Console.ReadLine();
}
}
}
5
.NET LABORATORY—10MCA57
Program:
using System;
namespace C5
{
class stack
{
public int[] stck = new int[50];
public int top;
public void push(int item)
{
if (top == stck.Length - 1)
Console.WriteLine("Stack is full.");
else
stck[++top] = item;
}
public int pop()
{
if (top < 0)
{
Console.WriteLine("Stack underflow.");
return 0;
}
else
return stck[top--];
}
public void display()
{
Console.WriteLine("Items of the stack are ->");
for (int i = 1; i <= top; i++)
Console.WriteLine(" {0}",stck[i]);
}
}
class Program
{
static void Main(string[] args)
6
.NET LABORATORY—10MCA57
{
int top = -1;
stack s1 = new stack();
Console.WriteLine("1.Push");
Console.WriteLine("2.Pop");
Console.WriteLine("3.Display");
Console.WriteLine("4.Exit");
while (true)
{
Console.WriteLine("Enter your choice ->");
int ch = int.Parse(Console.ReadLine());
switch (ch)
{
case 1:
Console.WriteLine("Enter the item ->");
s1.push(int.Parse(Console.ReadLine()));
Console.WriteLine("Item inserted");
break;
case 2:
Console.WriteLine("Popped item={0}", s1.pop());
break;
case 3:
s1.display();
break;
default:
Console.WriteLine("Wrong Choice");
break;
}
}
}
}
7
.NET LABORATORY—10MCA57
Program:
using System;
namespace C6
{
class Complex
{
private int x;
private int y;
public Complex()
{
}
public Complex(int i, int j)
{
x = i;
y = j;
}
public void ShowXY()
{
Console.WriteLine("{0} {1}",x,y);
}
public static Complex operator -(Complex c)
{
Complex temp = new Complex();
temp.x = -c.x;
8
.NET LABORATORY—10MCA57
temp.y = -c.y;
return temp;
}
}
class Program
{
static void Main(string[] args)
{
Complex c1 = new Complex(90, 50);
c1.ShowXY(); // displays 10 & 20
Complex c2 = new Complex();
c2.ShowXY(); // displays 0 & 0
c2 = -c1;
c2.ShowXY(); // diapls -10 & -20
Console.ReadLine();
} }}
7. Write a Program in C# to find the second largest element in a single dimensional array.
Program:
using System;
using System.Collections.Generic;
using System.Text;
namespace LABPROGRAMS
{
class SNDLARGE7
{
9
.NET LABORATORY—10MCA57
10
.NET LABORATORY—10MCA57
Program:
using System;
using System.Collections.Generic;
using System.Text;
namespace LABPROGRAMS
{
class MATRIXMUL8
{
static void Main(string[] args)
{
if (c1 != r2)
{
Console.WriteLine("columns of A matrix and Rows of B matrix are not equal");
11
.NET LABORATORY—10MCA57
Console.ReadLine();
Environment.Exit(0);
}
{
for (j = 0; j < c2; j++)
{
b[i, j] = Int32.Parse(Console.ReadLine());
}
}
Console.WriteLine("Result matrix");
Console.WriteLine("*************");
for (i = 0; i < r2; i++)
{
for (j = 0; j < c2; j++)
{
Console.Write("{0}\t", c[i, j]);
}
Console.WriteLine();
}
12
.NET LABORATORY—10MCA57
Console.ReadLine();
}
}
}
9. Find the sum of all the elements present in a jagged array of 3 inner arrays.
Program:
using System;
namespace C9
{
class Program
{
static void Main(string[] args)
{
int[][] jagged = new int[3][];
int sum=0;
jagged[0] = new int[2] { 5, 6 };
jagged[1] = new int[3] { 2, 3, 6 };
jagged[2] = new int[3] { 7, 0,1 };
13
.NET LABORATORY—10MCA57
}
}
Program:
using System;
using System.Collections.Generic;
using System.Text;
namespace LABPROGRAMS
{
class STRINGREV10
{
static void Main(string[] args)
{
14
.NET LABORATORY—10MCA57
string s;
char[] d = new char[100];
int len, j = 0;
15
.NET LABORATORY—10MCA57
11. Using Try, Catch and Finally blocks write a program in C# to demonstrate error handling.
Program:
using System;
namespace C11
{
class Program
{
static void Main(string[] args)
{
while (true)
{
try
{
string userInput;
Console.Write("Input a number between 0 and 5 " + "(or just hit return to exit) > ");
userInput = Console.ReadLine();
if (userInput == "")
{
break;
}
int index = Convert.ToInt32(userInput);
if (index < 0 || index > 5)
{
throw new IndexOutOfRangeException("You typed in " + userInput);
}
Console.WriteLine("Your number was " + index);
}
catch (IndexOutOfRangeException ex)
{
Console.WriteLine("Exception: " + "Number should be between 0 and 5. {0}",
ex.Message);
}
catch (Exception ex)
{
Console.WriteLine("An exception was thrown. Message was: {0}", ex.Message);
}
catch
{
Console.WriteLine("Some other exception has occurred");
16
.NET LABORATORY—10MCA57
}
finally
{
Console.WriteLine("Thank you");
}
}
}
}
}
Program:
using System;
using System.Collections.Generic;
using System.Text;
namespace Calculator
{
class Program
{
double num1, num2;
char option, choice;
double Result;
public void Number()
{
for (; ; )
{
Console.WriteLine("Enter the first number :");
17
.NET LABORATORY—10MCA57
num1 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter the Second number :");
num2 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Main Menu");
Console.WriteLine("1. Addition");
Console.WriteLine("2. Subtraction");
Console.WriteLine("3. Multiplication");
Console.WriteLine("4. Division");
switch (option)
{
case '1': Result = num1 + num2;
Console.WriteLine("The Result of Addition :{0}", Result);
break;
case '2': Result = num1 - num2;
Console.WriteLine("The Result of Subtraction :{0}", Result);
break;
case '3': Result = num1 * num2;
Console.WriteLine("The Result of Multiplication :{0}", Result);
break;
case '4': Result = num1 / num2;
Console.WriteLine("The Result of Division :{0}", Result);
break;
default: Console.WriteLine("Invalid Option");
break;
}
Console.WriteLine("Do u want to Continue? [y/n]");
choice = Convert.ToChar(Console.ReadLine());
if (choice == 'n')
break;
}
}
}
class ClassMain
{
static void Main(string[] args)
{
Program calc = new Program();
calc.Number();
18
.NET LABORATORY—10MCA57
}
}
}
13. Demonstrate Use of Virtual and override key words in C# with a simple program.
Program:
using System;
using System.Collections.Generic;
using System.Text;
namespace Virtual_and_Overide
{
class Department
{
public virtual void AcceptDeptDetails()
{
19
.NET LABORATORY—10MCA57
}
public override void DisplayDeptDetails()
{
Console.WriteLine("Name of the HOD : {0}", HODName);
Console.WriteLine("No. of Faculties : {0}", NumOf_Faculties);
Console.WriteLine("No. of Students : {0}", NumOf_students);
}
}
class MBA : Department
{
string HODName,temp1;
int NumOf_Faculties, NumOf_students;
public override void AcceptDeptDetails()
{
Console.WriteLine("\n MBA DETAILS \n");
Console.WriteLine("Enter HOD Name :");
HODName = Console.ReadLine();
Console.WriteLine("Enter No. of Faculties :");
temp1 = Console.ReadLine();
if (temp1 != "")
NumOf_Faculties = Convert.ToInt32(temp1);
20
.NET LABORATORY—10MCA57
}
public override void DisplayDeptDetails()
{
Console.WriteLine("Name of the HOD : {0}", HODName);
Console.WriteLine("No. of Faculties : {0}", NumOf_Faculties);
Console.WriteLine("No. of Students : {0}", NumOf_students);
}
}
class Implement
{
public void callAcceptFunction(Department clg)
{
clg.AcceptDeptDetails();
}
public void callDisplayFunction(Department clg)
{
clg.DisplayDeptDetails();
}
}
class Program
{
static void Main(string[] args)
{
Implement imp = new Implement();
MCA MC = new MCA();
MBA MB = new MBA();
imp.callAcceptFunction(MC);
imp.callDisplayFunction(MC);
Console.WriteLine();
imp.callAcceptFunction(MB);
imp.callDisplayFunction(MB);
Console.ReadLine();
}
}
}
21
.NET LABORATORY—10MCA57
14. Implement linked lists in C# using the existing collections name space.
Program:
using System;
using System.Collections.Generic;
using System.Text;
namespace LinkedListEx
{
class Program
{
static void Main(string[] args)
{
int option;
char choice;
int item, key;
LinkedList<int> List = new LinkedList<int>();
try
{
22
.NET LABORATORY—10MCA57
for (; ; )
{
Console.WriteLine("Main Menu");
Console.WriteLine("1. Add the node from the beginning");
Console.WriteLine("2. Add the node from the Last");
Console.WriteLine("3. Add the node after the key Node");
Console.WriteLine("4. Delete the node from the beginning");
Console.WriteLine("5. Delete the node from Last");
Console.WriteLine("6. search the Node");
Console.WriteLine("7. display the nodes in the linked list");
if (List.FindLast(key) != null)
{
Console.WriteLine("Enter Value to enter :");
item = Convert.ToInt32(Console.ReadLine());
List.AddAfter(current, item); break;
}
else
{
Console.WriteLine("Entered Key value is not found ");
break;
}
case 4:
if (List.First == null)
{
Console.WriteLine("linked list is empty");
break;
}
else
23
.NET LABORATORY—10MCA57
{
List.RemoveFirst();
break;
}
if (List.FindLast(key) == null)
{
Console.WriteLine("Entered Key value is not found ");
break;
}
else
{
Console.WriteLine("Key value is found");
break;
}
}
Console.WriteLine("Do u want to Continue [y/n] :");
choice = Convert.ToChar(Console.ReadLine());
if (choice == 'n')
24
.NET LABORATORY—10MCA57
break;
}
}
catch
{
Console.WriteLine("Invalide Input");
Console.ReadLine();
}
}
}}
25
.NET LABORATORY—10MCA57
15. Write a program to demonstrate abstract class and abstract methods in C#.
Program:
using System;
using System.Collections.Generic;
using System.Text;
namespace Abstract_Class_and_Method
{
public abstract class Furniture
{
protected string color;
protected int width;
protected int height;
public abstract void Accept();
public abstract void Display();
}
class Bookshelf : Furniture
{
private int numOf_shelves;
public override void Accept()
{
string str1, str2, str3;
Console.WriteLine("ENTER VALUES FOR BOOKSHELF");
Console.WriteLine("Enter Color ");
color = Console.ReadLine();
Console.WriteLine("Enter Width ");
str1 = Console.ReadLine();
width = Convert.ToInt32(str1);
Console.WriteLine("Enter Height");
str2 = Console.ReadLine();
height = Convert.ToInt32(str2);
Console.WriteLine("Enter No. of Shelves ");
str3 = Console.ReadLine();
numOf_shelves = Convert.ToInt32(str3);
}
public override void Display()
{
Console.WriteLine("DISPLAYING VALUES FOR BOOKSHELF");
Console.WriteLine("Color is {0}", color);
Console.WriteLine("Width is {0}", width);
Console.WriteLine("Height is {0}", height);
Console.WriteLine("Number of shelves are {0}", numOf_shelves);
}
}
26
.NET LABORATORY—10MCA57
27
.NET LABORATORY—10MCA57
16. Write a program in C# to build a class which implements an interface which is already
existing.
Program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace InterfaceImplementation
{
public interface calc
{
void Addition(int a,int b);
void Subtraction(int a, int b);
}
class interfaceUser : calc
28
.NET LABORATORY—10MCA57
{
public void Addition(int a,int b)
{
Console.WriteLine("Result of Addition :{0}", a + b);
}
public void Subtraction(int a, int b)
{
Console.WriteLine("Result of Subtraction :{0}", a - b);
} }
class Program
{
static void Main(string[] args)
{
interfaceUser iUser = new interfaceUser();
int a = 0, b = 0;
string temp1,temp2;
Console.WriteLine("Enter first value :");
temp1 = Console.ReadLine();
if(temp1!="")
a = Convert.ToInt32(temp1);
iUser.Addition(a,b);
iUser.Subtraction(a,b);
Console.ReadLine();
}
}
}
29
.NET LABORATORY—10MCA57
Program:
using System;
using System.Collections.Generic;
using System.Text;
namespace PropertiesEx
{
class Favorites
{
private string color;
private string player;
private string company;
30
.NET LABORATORY—10MCA57
class Program
{
static void Main(string[] args)
{
string colr, playr, comp;
Console.WriteLine("Enter u r Favorite color :");
colr=Console.ReadLine();
Console.WriteLine("Enter u r Favorite Player :");
playr = Console.ReadLine();
Console.WriteLine("Enter u r Favorite Company :");
comp = Console.ReadLine();
Console.ReadLine();
}
}
}
31
.NET LABORATORY—10MCA57
using System;
using System.Collections;
public class MyComparer : IComparer {
public int Compare(object aobj, object bobj) {
int a = (int)aobj;
int b = (int)bobj;
return (a < b ? 1 : a == b ? 0 : -1);
}
}
public class SortDemo {
public static void Main() {
const int N = 14;
ArrayList list = new ArrayList();
for (int i = 0; i < N; i++)
list.Add(i);
list.Sort();
for (int i = 0; i < N; i++)
Console.Write(list[i] + " ");
Console.WriteLine();
list.Sort(new MyComparer());
for (int i = 0; i < N; i++)
Console.Write(list[i] + " ");
Console.WriteLine();
}
}
32