C# .NET Programs MCA 5th sem

.NET Lab Programs

1.Write a Program in C# to Check whether a number is Palindrome or not.
2. Write a Program in C# to demonstrate Command line arguments Processing.
3. Write a Program in C# to find the roots of Quadratic Equation.
4. Write a Program in C# to demonstrate boxing and unBoxing.
5. Write a Program in C# to implement Stack operations.
6. Write a program to demonstrate Operator overloading.
7. Write a Program in C# to find the second largest element in a single
dimensional array.
8. Write a Program in C# to multiply to matrices using Rectangular arrays.
9. Find the sum of all the elements present in a jagged array of 3 inner arrays.
10. Write a program to reverse a given string using C#.
11. Using Try, Catch and Finally blocks write a program in C# to demonstrate
error handling.
12. Design a simple calculator using Switch Statement in C#.
13. Demonstrate Use of Virtual and override key words in C# with a simple
program
14. Implement linked lists in C# using the existing collections name space.
15. Write a program to demonstrate abstract class and abstract methods in C#.
16. Write a program in C# to build a class which implements an interface which
is already existing.
17. Write a program to illustrate the use of different properties in C#.
18. Demonstrate arrays of interface types with a C# program.

1. Write a Program in C# to Check whether a number is Palindrome or not.
using System;
class Pallin
{
public static void Main(String[] args)
{
int d,n,rev,m;
Console.WriteLine(“Enter the number”);
n=int.Parse(Console.ReadLine());
m=n;
rev=0;
while(n!=0){
d=n%10;
rev=rev*10+d;
n=n/10;
}
if(rev==m){
Console.WriteLine(“Entered number “+m+” is pallindrom”);
}
else
{
Console.WriteLine(“Entered number “+m+” is not pallindrom”);
}
}
}

2. Write a Program in C# to demonstrate Command line arguments processing.

using System;
namespace ConsoleApplication1
{
class Commandline
{
static void Main(string[] args)
{
Console.Write(“WELCOME to “);
Console.Write(” ” + args[0]);
Console.Write(” ” + args[1]);
Console.ReadLine();
}
}
}

//another
using System;
class CommandLineArgs {
public static void Main(String[] args) {
double a,b;
if(args.Length==0) {
Console.WriteLine(“No arguments found”);
}
else {
a=double.Parse(args[0]);
b=double.Parse(args[1]);
double sum=0,mul=0,div=0,sub=0;
sum=a+b;
Console.WriteLine(“Sum of “+a+” and “+b+” is “+sum);
sub=a-b;
Console.WriteLine(” Substraction of “+b+” by “+a+” is “+sub);
mul=a*b;
Console.WriteLine(” Multiplication of “+a+” and “+b+” is “+mul);
div=a/b;
Console.WriteLine(” Division of “+a+” by “+b+” is “+div);
}
}
}

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

using System;
using System.Text;
namespace ConsoleApplication1
{
class QuadRK
{   static void Main(string[] args)
{  int a, b, c;
float d, x1, x2;
Console.WriteLine(“\n Enter a,b,c :\n”);
Console.Write(” a = “);
a = Convert.ToInt32(Console.ReadLine());
Console.Write(” b = “);
b = Convert.ToInt32(Console.ReadLine());
Console.Write(” c = “);
c = Convert.ToInt32(Console.ReadLine());
d = b * b – 4 * a * c;
if (d > 0)
{  Console.WriteLine(“\n Equation has two roots.”);
x1 = (float)(-b + Math.Sqrt(d)) / (2 * a);
x2 = (float)(-b – Math.Sqrt(d)) / (2 * a);
Console.WriteLine(“\n The roots are:\n”);
Console.WriteLine(“\n root1 = “+x1+”\n root2 = “+x2);
}
else if (d == 0)
{
Console.WriteLine(“\n Equation has only one root.”);
x1 = (float)-b / (2 * a);
Console.WriteLine(“\n The root is:\n”);
Console.WriteLine(“\n root = ” + x1);
}
else
{
Console.WriteLine(“\n Equation has imaginary roots.”);
x1 = (float)-b / (2 * a);
x2 = (float)Math.Sqrt(-d) / (2 * a);
Console.WriteLine(“\n The roots are:\n”);
Console.WriteLine(“\n root1 = ” + x1 + ” + ” + x2 + ” i \n” + ” root2 = ” + x1 + ” – ” + x2 + ” i “);
}
Console.ReadLine();
}   }   }

4. Write a Program in C# to demonstrate boxing and unBoxing.
using System;
class BoxUnbox {
public static void Main(){
int num;
Console.Write(“Enter the number : “);
num=int.Parse(Console.ReadLine());
Object o=num;
Console.WriteLine(“Value in num= “+num);
Console.WriteLine(“Value in Object 0=”+o);
int n;
n=(int)o;
Console.WriteLine(“Value of n=”+n);
}
}

5. Write a Program in C# to implement Stack operations.
using System;
class Stack
{
public static void Main(String[] args)
{
int top=-1,num,choice,max,rpt=1;
int[] stack=new int[20];
Console.WriteLine(“Enter the maximum limit”);
max=int.Parse(Console.ReadLine());
while(rpt!=0)
{
Console.WriteLine(“Stack Operation”);
Console.WriteLine(“1 . PUSH”);
Console.WriteLine(“2 . POP”);
Console.WriteLine(“3 . DISPLAY”);
Console.WriteLine(“0 . EXIT”);
Console.WriteLine(“Enter your choice:”);
choice=int.Parse(Console.ReadLine());
switch(choice)
{
case 1:
if(top==(max-1)) {
Console.WriteLine(“Stack is full”);
}
else {
Console.WriteLine(“Enter the element to be inserted:”);
num=int.Parse(Console.ReadLine());
stack[++top]=num;
Console.WriteLine(“Element is successfully inserted”);
}
break;
case 2:
if(top==-1)
{
Console.WriteLine(“Stack is Empty”);
}
else {
Console.WriteLine(“Deleted Element is :”+stack[top--]);
}
break;
case 3:
if(top==-1)
{
Console.WriteLine(“Stack is Empty”);
}
else {

Console.WriteLine(“Elements in the stack”);
for(int i=top;i>=0;i–)
{
Console.WriteLine(stack[i]);
}
}
break;
case 0:
Environment.Exit(0);
break;
default:
Console.WriteLine(“Worng Entery”);
break;
}
}
}
}

6. Write a program to demonstrate Operator overloading.

using System;

public struct Complex
{
public int real;
public int imaginary;

public Complex(int real, int imaginary)
{
this.real = real;
this.imaginary = imaginary;
}

// Declare which operator to overload (+), the types
// that can be added (two Complex objects), and the
// return type (Complex):
public static Complex operator +(Complex c1, Complex c2)
{
return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
}
// Override the ToString method to display an complex number in the suitable format:
public override string ToString()
{
return(String.Format(“{0} + {1}i”, real, imaginary));
}

public static void Main()
{
Complex num1 = new Complex(2,3);
Complex num2 = new Complex(3,4);

// Add two Complex objects (num1 and num2) through the
// overloaded plus operator:
Complex sum = num1 + num2;

// Print the numbers and the sum using the overriden ToString method:
Console.WriteLine(“First complex number:  {0}”,num1);
Console.WriteLine(“Second complex number: {0}”,num2);
Console.WriteLine(“The sum of the two numbers: {0}”,sum);

}
}

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.Text;

namespace secondlargest
{
class Program
{
static void Main(string[] args)
{
int[] arr ={ 0, 5, 7, -2, 9, 12, 4, 8 };
int high, sechigh,temp;
high = sechigh = arr[0];
Console.WriteLine(“Array elements are”);
for (int i = 0; i < arr.Length; i++)
Console.Write(arr[i]+”  “);
Console.WriteLine();
for (int i = 0; i < arr.Length; i++)
{
temp = arr[i];
if (temp > high)
{
sechigh = high;
high = temp;
}
else if (temp > sechigh && temp != high)
sechigh = temp;
}
Console.WriteLine(“The second largest number in the array is  ” + sechigh);
Console.ReadLine();
}
}
}

8. Write a Program in C# to multiply to matrices using Rectangular arrays.
using System;
class matrixmul
{
public static void Main(String[] args)
{
int[,] a=new int [5,5];
int[,] b=new int [5,5];
int[,] c=new int [10,10];
int m,n,i,j,p,q,k;
Console.WriteLine(“Enter the order of first matrix”);
m=int.Parse(Console.ReadLine());
n=int.Parse(Console.ReadLine());
Console.WriteLine(“Enter the order of second matrix”);
p=int.Parse(Console.ReadLine());
q=int.Parse(Console.ReadLine());
if(n!=p)
Console.WriteLine(“not possible”);
else
{
Console.WriteLine(“enter array elements of fiorst matrix “);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
a[i,j]=int.Parse(Console.ReadLine());

Console.WriteLine(“enter array elements of second matrix “);
for(i=0;i<p;i++)
for(j=0;j<q;j++)
b[i,j]=int.Parse(Console.ReadLine());

for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i,j]=0;
for(k=0;k<n;k++)
c[i,j]=c[i,j]+a[i,k]*b[k,j];
}
}
Console.WriteLine(“\tResult of Multiplication”);
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
Console.Write(“\t”+c[i,j]);
Console.WriteLine();
}
}
}
}

9. Find the sum of all the elements present in a jagged array of 3 inner arrays.
using System;
class MyjaggedArr
{
public static void Main()
{
int[][] myjag=new int[3][];

for(int i=0;i<myjag.Length;i++)
{
myjag[i]=new int[i+3];
}

for(int i=0;i<3;i++)
{
Console.WriteLine(“Enter the Elements of row{0}”,i);

for(int j=0;j<myjag[i].Length;j++)
{

myjag[i][j]=int.Parse(Console.ReadLine());
}
}
int sum=0;
for(int i=0;i<3;i++)
{
for(int j=0;j<myjag[i].Length;j++)
{
sum+=myjag[i][j];
}
}
Console.WriteLine(“sum=”+sum);
}
}

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

using System;
using System.Text;

namespace ConsoleApplication1
{
class ReverseRK
{
static void Main(string[] args)
{
String inv;
String outv=””;
Console.WriteLine(” Enter a string :\n”);
inv=Console.ReadLine();
for(int i=inv.Length-1;i>=0;i–)
{
outv=outv+inv.Substring(i,1);
}
Console.WriteLine(“The reversed string is: “+outv);
Console.ReadLine();
}
}
}

11. Using Try, Catch and Finally blocks write a program in C# to demonstrate error      handling.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Exception
{
static int m = 10;
static int n = 0;
static void Division()
{
try
{
int k = m / n;
}
catch (ArgumentException e)
{
Console.WriteLine(“Exception caught:” + e.Message);
}
finally
{
Console.WriteLine(“Inside division method”);
Console.ReadLine();
}
}
static void Main(string[] args)
{
try
{
Division();
}
catch (DivideByZeroException e)
{
Console.WriteLine(“Exception caught:” + e.Message);
}
finally
{
Console.WriteLine(“inside main method”);
Console.ReadLine();
}
}
} }

12. Design a simple calculator using Switch Statement in C#.
using System;
class Simcalc {
public static void Main(String[] args)
{
double a,b,rpt=1;
int choice;
while(rpt!=0)
{
Console.WriteLine(“Select the operation”);
Console.WriteLine(“1 . Addition”);
Console.WriteLine(“2 . Subtraction”);
Console.WriteLine(“3 . Multiplication”);
Console.WriteLine(“4 . Division”);
Console.WriteLine(“0 . Exit”);
Console.WriteLine(“Enter ur choice :”);
choice=int.Parse(Console.ReadLine());
switch(choice)
{
case 1:
Console.WriteLine(“Enter two numbers:”);
a=double.Parse(Console.ReadLine());
b=double.Parse(Console.ReadLine());
Console.WriteLine(“Result of Addition : “+(a+b));
break;
case 2:
Console.WriteLine(“Enter two numbers:”);
a=double.Parse(Console.ReadLine());
b=double.Parse(Console.ReadLine());
Console.WriteLine(“Result of Subtraction : “+(a-b));
break;
case 3:
Console.WriteLine(“Enter two numbers:”);
a=double.Parse(Console.ReadLine());
b=double.Parse(Console.ReadLine());
Console.WriteLine(“Result of Multiplication : “+(a*b));
break;
case 4:
Console.WriteLine(“Enter two numbers:”);
a=double.Parse(Console.ReadLine());
b=double.Parse(Console.ReadLine());
if(b==0) {
Console.WriteLine(“Division not possible”);
}
else {
Console.WriteLine(“Result of Division : “+(a/b));
}
break;
case 0:
rpt=0;
break;
default:
Console.WriteLine(“Invalid selection”);
break;
}
}
}
}

13. Demonstrate Use of Virtual and override key words in C# with a simple program

using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
public class Customer
{
public virtual void CustomerType()
{
Console.WriteLine(“I am customer”);
}
}
public class CorporateCustomer:Customer
{
public override void CustomerType()
{
Console.WriteLine(“I am Corporate Customer”);
}
}
public class PersonalCustomer:Customer
{
public override void CustomerType()
{
Console.WriteLine(“I am personal customer”);
}
}
class Program
{
static void Main(string[] args)
{
Customer[] c=new Customer[3];
c[0]=new CorporateCustomer();
c[1]=new CorporateCustomer();
c[2]=new Customer();
foreach(Customer customerobject in c)
{
customerobject.CustomerType();
}
Console.ReadLine();
}
}
}

14. Implement linked lists in C# using the existing collections name space.
using System;
using System.Collections.Generic;
using System.Text;

class GenLinkedList
{
public static void Main()
{
Console.WriteLine(“\n**DEMONSTRATION OF LINKED LIST **\n”);
LinkedList<int> ll = new LinkedList<int>();
LinkedListNode<int> node;
int ch,x;
Console.WriteLine(“Initial number of elements:”+ ll.Count);
Console.WriteLine();
do
{
Console.WriteLine(“Linked List Operations\n”);
Console.WriteLine(“1. AddFirst\n2.AddLast\n3.RemoveFirst\n 4.RemoveLast\n5.RemoveSpecified\n6.Display\n7.Exit”);
Console.WriteLine();
Console.WriteLine(“Enter your choice”);
ch = int.Parse(Console.ReadLine());
switch (ch)
{
case 1:Console.WriteLine(“Enter the element toAddFirst”);
x = int.Parse(Console.ReadLine());
ll.AddFirst(x);
Console.WriteLine();
Console.WriteLine(“Number of elements:”+ll.Count);
break;
case 2:Console.WriteLine(“Enter the element to AddLast”);
x = int.Parse(Console.ReadLine());
ll.AddLast(x);
Console.WriteLine();
Console.WriteLine(“Number of elements:”+ll.Count);
break;
case 3:Console.WriteLine(“RemovedFirst element”);
ll.RemoveFirst();
Console.WriteLine();
Console.WriteLine(“Number of elements:”+ll.Count);
break;
case 4:Console.WriteLine(“RemovedLast element”);
ll.RemoveLast();
Console.WriteLine();
Console.WriteLine(“Number of elements:”+ll.Count);
break;

case 5:Console.WriteLine(“Enter the element to Remove”);
x = int.Parse(Console.ReadLine());
ll.Remove(x);
Console.WriteLine();
Console.WriteLine(“Number of elements:”+ll.Count);
break;
case 6:Console.WriteLine(“Number of elements:”+ll.Count);
Console.WriteLine(“Elements are :”);
for (node=ll.First;node != null;node=node.Next)
Console.Write(node.Value + ” “);
Console.WriteLine(“\n”);
break;
case 7:Environment.Exit(0);
break;
default:Console.WriteLine(“Invalid Choice”);
break;
}
Console.ReadLine();
} while(true);
}
}

15. Write a program to demonstrate abstract class and abstract methods in C#.

using System;

abstract class Test {
public int a;
public abstract void A();
}

class Derived1:Test {
public override void A() {
Console.WriteLine(“Derived1 A”);
base.a++;
Console.WriteLine(“a = {0}”,base.a);
}
}
class Derived2:Test {
public override void A() {
Console.WriteLine(“Derived2 A”);
base.a–;
Console.WriteLine(“a = {0}”,base.a);
}
}
class ProgramApp {
static void Main(String[] args) {
Test test1=new Derived1();
test1.A();
Test test2=new Derived2();
test2.A();
}
}

16. Write a program in C# to build a class which implements an interface which  is already existing.
using System;
namespace ConsoleApplication1
{
interface Addition
{
int Add();
}
interface Multiplication
{
int Multiply();
}
class Compute:Addition,Multiplication
{
int x, y;
public Compute(int a, int b)
{
this.x = a;
this.y = b;
}
public int Add()
{
return (x + y);
}
public int Multiply()
{
return (x * y);
}
}
class Interface
{
static void Main(string[] args)
{
int a, b;
Console.Write(“Enter 2 nos:”);
a = Convert.ToInt32(Console.ReadLine());
b = Convert.ToInt32(Console.ReadLine());
Compute ob1=new Compute(a,b);
Console.WriteLine(“Addition is:”+ob1.Add());
Console.WriteLine(“Multiplication is:”+ob1.Multiply());
Console.ReadLine();
}
}
}
17. Write a program to illustrate the use of different properties in C#.
using System;
class Point
{
int x,y;
public Point()
{
}
public Point(int x,int y) {
this.x=x;
this.y=y;
}
public int X
{
set {
this.x=value;
}
get {
return this.x;
}
}
public int Y
{
set {
this.y=value;
}
get {
return this.y;
}
}
}
class PrintApp {
public static void Main(String[] args)
{
Point p=new Point();
p.X=2;
p.Y=5;
Console.WriteLine(“x = “+p.X+” y = “+p.Y);
p.X++;
p.Y+=2;
Console.WriteLine(“After x++ and y+2″);
Console.WriteLine(“x = “+p.X+” y = “+p.Y);
}
}

18. Demonstrate arrays of interface types with a C# program.
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
public static void FindMatches(IList<string>iList,object[] ob)
{
Console.WriteLine(“Match array is:”);
foreach(object o in ob)
{
Console.WriteLine(“{0}”,o.ToString());
}
foreach(object o in ob)
{  if(iList.Contains(o.ToString()))
Console.WriteLine(“\niList contains {0} at index{1}”,o,iList.IndexOf(o.ToString()));
}
}

static void Main(string[] args)
{
string[] strings ={ “one”, “two”, “four”, “eight” };
Console.WriteLine(“Strings array values:\n”);
foreach(String s in strings)
{
Console.WriteLine(“{0}”,s);
}
Console.WriteLine(“\n”);
FindMatches(strings,new String[]{“zero”,”one”,”five”,”eight”});
Console.ReadKey();

}
}}

//another
using System;
using System.Collections;
public class ArrayList1
{
public static void Main()
{
ArrayList MyAL=new ArrayList();
MyAL.Add(“hello”);
MyAL.Add(“world”);
MyAL.Add(“!”);
Console.WriteLine(“MY ARRRAY LIST”);
Console.WriteLine(“count {0}”,MyAL.Count);
Console.WriteLine(“values”);
printValues(MyAL);
}
public static void printValues(IList MyList)
{
foreach(Object o in MyList)
Console.WriteLine(“{0}”,o);
}
}

About these ads

One Response

  1. hey…great job..!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: