.
NET Laboratory Manual[10MCA57]
1
=======================================================================
1. Write a Program in C# to Check whether a number is Palindrome or not.
Program:
using System;
namespace DOTNETLABPROGRAMS
{
class Palindrome
{
public static void Main()
{
//Declaring the variables
int num, temp, rem, rev_num = 0;
Console.WriteLine("Provide a number");
num = int.Parse(Console.ReadLine());
while(num == 0 || num < 0)
{
Console.WriteLine("Enter the valid number");
num = int.Parse(Console.ReadLine());
}
temp = num;
do
{
rem = num % 10; //set digit to the users number mod 10
//reverse the number by multiplying it by 10 then add rem
rev_num = rev_num * 10 + rem;
num /= 10;
} while (num != 0);
//print out the number reversed
Console.WriteLine("************************");
Console.WriteLine("The reverse of the number is:{0}", rev_num);
Console.WriteLine("************************");
Console.WriteLine("\r\n");
//now check to see if the number is a palidrome
if (temp == rev_num)
Console.WriteLine("The number {0} is a palindrome!", temp);
else
Console.WriteLine("The number{0}is not a palindrome",temp);
Console.ReadLine();
}
Output:
Provide a number
121
************************
The reverse of the number is: 121
************************
The number 121 is a palindrome!
By Suma M G, Asst. Prof., Dept of MCA,
Provide a number
324
************************
The reverse of the number is:
************************
423
The number 324 is not a palindrome
RNSIT, Bangalore.
.NET Laboratory Manual[10MCA57]
2
=======================================================================
2. Write a Program in C# to demonstrate Command line arguments Processing.
Program:
using System;
namespace DOTNETLABPROGRAMS
{
class CmdLnArg
{
static void Main(string[] args)
{
if (args.Length == 0)
{
Console.WriteLine("No arguments submitted");
Console.ReadLine();
return;
}
Console.Write("Addition of all the arguments ");
// create variables to hold arguments
int total = 0;
// populate the variables from the arguments
for (int i = 0; i < args.Length; i++)
{
Console.Write(args[i] + " ");
try
{
total = total + Convert.ToInt32(args[i]);
}
catch
{
// Display error if input is missing or invalid
Console.WriteLine("\nInvalid Input");
Console.ReadLine();
return;
}
}
Console.Write("is = " + total);
Console.ReadLine();
return;
}
Output:
Addition of all the arguments 5 a
Addition of all the arguments 5 3 is = 8
Invalid Input
By Suma M G, Asst. Prof., Dept of MCA,
RNSIT, Bangalore.
.NET Laboratory Manual[10MCA57]
3
=======================================================================
3. Write a Program in C# to find the roots of Quadratic Equation.
Program:
using System;
namespace DOTNETLABPROGRAMS
{
class Quadratic
{
public static void Main()
{
int a, b, c;
double disc, deno, x1, x2;
Console.WriteLine("ENTER THE VALUES OF A,B,C...");
a=int.Parse(Console.ReadLine());
b=int.Parse(Console.ReadLine());
c=int.Parse(Console.ReadLine());
}
}
if(a == 0)
{
x1= -c/b;
Console.WriteLine("The roots are Linear: {0}", x1);
Console.ReadLine();
}
else
{
disc = (b * b) - (4 * a * c);
deno = 2 * a;
if(disc > 0)
{
Console.WriteLine("THE ROOTS ARE REAL ROOTS");
x1 = (-b/deno) + (Math.Sqrt(disc) / deno);
x2 = (-b/deno) - (Math.Sqrt(disc) / deno);
Console.WriteLine("THE ROOTS ARE.:{0}and{1}", x1, x2);
Console.ReadLine();
}
else if(disc == 0)
{
Console.WriteLine("THE ROOTS ARE REPEATED ROOTS");
x1 = -b/deno;
Console.WriteLine("THE ROOT IS...: {0}", x1);
Console.ReadLine();
}
else
{
Console.WriteLine("THE ROOTS ARE IMAGINARY ROOTS\n");
x1 = -b/deno;
x2 = (Math.Sqrt(Math.Abs(disc)) / deno);
Console.WriteLine("THE ROOT ONE...: {0}+i{1}", x1, x2);
Console.WriteLine("THE ROOT TWO...: {0}-i{1}", x1, x2);
Console.ReadLine();
}
}
By Suma M G, Asst. Prof., Dept of MCA,
RNSIT, Bangalore.
.NET Laboratory Manual[10MCA57]
4
=======================================================================
Output:
ENTER THE VALUES OF A, B, C...
ENTER THE VALUES OF A, B, C...
2
The roots are Linear: -2
THE ROOTS ARE REAL ROOTS
ENTER THE VALUES OF A, B, C...
ENTER THE VALUES OF A, B, C...
THE ROOTS ARE REPEATED ROOTS
THE ROOTS ARE IMAGINARY ROOTS
THE ROOTS ARE...:
-3.41421356237309
-0.585786437626905
THE ROOT IS...: -1
THE ROOT ONE...: -1+i1.73205080756888
THE ROOT TWO...: -1-i1.73205080756888
By Suma M G, Asst. Prof., Dept of MCA,
RNSIT, Bangalore.
and
.NET Laboratory Manual[10MCA57]
5
=======================================================================
4. Write a Program in C# to demonstrate boxing and unBoxing.
Program:
using System;
namespace DOTNETLABPROGRAMS
{
class BoxUn
{
static void Main()
{
int i = 123;
object o = i; // implicit boxing
try
{
//int j = (short)o; // attempt to unbox but not possible
int j = (int)o; // attempt to unbox
System.Console.WriteLine("Unboxing OK.");
Console.ReadLine();
}
catch (System.InvalidCastException e)
{
System.Console.WriteLine("{0} Error: Incorrect
unboxing.", e.Message);
Console.ReadLine();
}
}
Output:
Unboxing OK.
By Suma M G, Asst. Prof., Dept of MCA,
RNSIT, Bangalore.
.NET Laboratory Manual[10MCA57]
6
=======================================================================
5. Write a Program in C# to implement Stack operations.
Program:
using System;
namespace DOTNETLABPROGRAMS
{
class Operations
{
public int n,top = -1;
public int[] stck;
public Operations(int s)
{
n = s;
stck = new int[n];
}
public void push(int item)
{
stck[++top] = item;
}
public int pop()
{
return stck[top--];
}
public void display()
{
if (top == -1)
{
Console.WriteLine("Stack underflow.");
return;
}
Console.WriteLine("Items of the stack are ->");
for (int i = top; i>=0; i--)
Console.WriteLine(" {0}", stck[i]);
public bool overflow()
{
if(top==(stck.Length-1))
return true;
else
return false;
}
public bool empty()
{
if(top==-1)
return true;
else
return false;
}
By Suma M G, Asst. Prof., Dept of MCA,
RNSIT, Bangalore.
.NET Laboratory Manual[10MCA57]
7
=======================================================================
class Stack
{
public static void Main(string[] args)
{
Console.WriteLine("Enter the Array size:");
int n = int.Parse(Console.ReadLine());
Operations s1 = new Operations(n);
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:
if(s1.overflow())
Console.WriteLine("Stack is full.");
else
{
Console.WriteLine("Enter the item ->");
s1.push(int.Parse(Console.ReadLine()));
Console.WriteLine("Item inserted");
}
break;
case 2:
if(s1.empty())
Console.WriteLine("Stack underflow.");
else
Console.WriteLine("Popped item={0}", s1.pop());
break;
case 3:
s1.display();
break;
case 4: return;
default:
Console.WriteLine("Wrong Choice");
break;
}
}
By Suma M G, Asst. Prof., Dept of MCA,
RNSIT, Bangalore.
.NET Laboratory Manual[10MCA57]
8
=======================================================================
Output:
Enter the Array size:3
Enter your choice ->3
1.Push
Items of the stack are ->
2.Pop
3.Display
20
4.Exit
10
Enter your choice ->1
Enter your choice ->1
Enter the item ->10
Stack is full.
Item inserted
Enter your choice ->2
Enter your choice ->1
Popped item=3
Enter the item ->20
Enter your choice ->2
Item inserted
Popped item=20
Enter your choice ->1
Enter your choice ->2
Enter the item ->3
Popped item=10
Item inserted
Enter your choice ->3
Stack underflow.
Enter your choice ->4
By Suma M G, Asst. Prof., Dept of MCA,
RNSIT, Bangalore.
.NET Laboratory Manual[10MCA57]
9
=======================================================================
6. Write a program to demonstrate Operator overloading.
Program:
using System;
namespace DOTNETLABPROGRAMS
{
class Complex
{
public int real;
public int imaginary;
public Complex(int r, int img)
{
real = r;
imaginary = img;
}
// 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
public override string ToString()
{
if(imaginary <0)
return (String.Format("{0} {1}i", real, imaginary));
else
return(String.Format("{0} + {1}i", real, imaginary));
}
public static void Main()
{
int a1, b1, a2, b2;
Console.WriteLine("Enter a and b values of complex number:");
a1 = Int32.Parse(Console.ReadLine());
b1 = Int32.Parse(Console.ReadLine());
Complex num1 = new Complex(a1, b1);
Console.WriteLine("Enter a and b values of complex number ;");
a2 = Int32.Parse(Console.ReadLine());
b2 = Int32.Parse(Console.ReadLine());
Complex num2 = new Complex(a2, b2);
//Add to Complex objects through the overloaded plus operator
Complex sum = num1 + num2;
//Print the numbers and the sum using the overriden ToString
Console.WriteLine("First complex number: {0}", num1);
Console.WriteLine("Second complex number: {0}", num2);
By Suma M G, Asst. Prof., Dept of MCA,
RNSIT, Bangalore.
.NET Laboratory Manual[10MCA57]
10
=======================================================================
Console.WriteLine("The sum of the two numbers: {0}", sum);
Console.Read();
}
Output:
Enter a and b values of complex number:
2
3
Enter a and b values of complex number ;
1
2
First complex number:
2 + 3i
Second complex number: 1 + 2i
The sum of the two numbers: 3 + 5i
By Suma M G, Asst. Prof., Dept of MCA,
RNSIT, Bangalore.
.NET Laboratory Manual[10MCA57]
11
=======================================================================
7. Write a Program in C# to find the second largest element in a single dimensional array.
Program:
Output:
8. Write a Program in C# to multiply to matrices using Rectangular arrays.
Program:
Output:
9. Find the sum of all the elements present in a jagged array of 3 inner arrays.
Program:
Output:
10. Write a program to reverse a given string using C#.
Program:
Output:
11. Using Try, Catch and Finally blocks write a program in C# to demonstrate error handling.
Program:
Output:
12. Design a simple calculator using Switch Statement in C#.
Program:
Output:
By Suma M G, Asst. Prof., Dept of MCA,
RNSIT, Bangalore.
.NET Laboratory Manual[10MCA57]
12
=======================================================================
13. Demonstrate Use of Virtual and override key words in C# with a simple program
Program:
Output:
14. Implement linked lists in C# using the existing collections name space.
Program:
Output:
15. Implement linked lists in C# using the existing collections name space.
Program:
Output:
By Suma M G, Asst. Prof., Dept of MCA,
RNSIT, Bangalore.