Anda di halaman 1dari 13

Kuvempu University

Laboratory Assignments

Subject: Basics of .NET


Subject Code: BSIT – 61 L

1. Write a program in C# using command line arguments to display a welcome


message. The message has to be supplied as input in the command line.

using System;
using System.Collections.Generic;
using System.Text;

namespace Exercise_1
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < args.Length; i++)
{
Console.Write(args[i] + " ");
}
Console.ReadLine();

/* Open Command Prompt and go to the location where your this file is
stored.
* csc Filename.cs
* filename.exe Welcome to C#
* you will get output
*/
}
}
}

2. Write a program in C# to find the area of a circle, given its radius.

using System;
using System.Collections.Generic;
using System.Text;

namespace Exercise_2
{
class Program
{
static void Main(string[] args)

Group Members: - RKM, Ashish, Ravi Ranjan, Pankaj, Shlesh, Purushottam, Nishant, Amlesh & Vishwjeet
{
Console.WriteLine("Enter the radius of Circle to find its area");
double radius = double.Parse(Console.ReadLine());

double area = Math.PI * radius * radius;


Console.WriteLine("The area of circle for given radius is : " + area);
Console.ReadLine();
}
}
}

3. Write a program in C# to find the area of polygon – triangle and rectangle by


using multiple constructors.

NOTE:- PLEASE MAKE SURE SOLUTION IS CORRECT OR NOT.

using System;
using System.Collections.Generic;
class rkm
{
public rkm(double basse, double height) //constructor for triangle
{
double b = basse;
double h = height;
double tri_area = (0.5) * b * h;
Console.WriteLine("Area of Triange:- " + tri_area);
}
public rkm(float width, float height) //constructor for Rectangle
{
float w = width;
float h = height;
float rec_area = w * h;
Console.WriteLine("Area of Rectangle:- " + rec_area);
}
public static void Main()
{

Console.WriteLine("============For Triangle==============");
Console.WriteLine("Enter the base of triangle:-");
double tri_base = Convert.ToDouble(Console.ReadLine());

Console.WriteLine("Enter the height of triangle:-");


double tri_height = Convert.ToDouble(Console.ReadLine());

Console.WriteLine("============For Rectangle==============");
Console.WriteLine("Enter the width of rectangle:-");
float rec_width = (float)(Convert.ToDouble(Console.ReadLine()));

Group Members: - RKM, Ashish, Ravi Ranjan, Pankaj, Shlesh, Purushottam, Nishant, Amlesh & Vishwjeet
Console.WriteLine("Enter the height of rectangle:-");
float rec_height = (float)(Convert.ToDouble(Console.ReadLine()));

Console.WriteLine("================Result=================");

rkm th1 = new rkm(tri_base, tri_height);


rkm th2 = new rkm(rec_width, rec_height);

}
}

4. Write a program in C# to add and multiply two numbers and display the results.
Demonstrate the use of ref and out parameters in this program.

using System;
using System.Collections.Generic;
using System.Text;

namespace Exercise_4
{
class Program
{

public static int ADD(ref int x, ref int y)


{
int res = x + y;
return res;
}
public static int Multiply(int x, int y, out int res1)
{
res1 = x * y;
return res1;
}

static void Main(string[] args)


{
Console.WriteLine("Enter First Number");
int n1 = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Enter Second Number");


int n2 = Convert.ToInt32(Console.ReadLine());

int Res = Program.ADD(ref n1, ref n2);


Console.WriteLine("Sum of Two Number is : " + Res);

Program.Multiply(n1, n2, out Res); // Res is responsible to hold return


value which is n1*n2

Group Members: - RKM, Ashish, Ravi Ranjan, Pankaj, Shlesh, Purushottam, Nishant, Amlesh & Vishwjeet
Console.WriteLine("Multiply of Two Number is : " + Res);

Console.ReadLine();
}
}
}

5. Using foreach looping statement, write a program in C# to display all the


elements of string array.

using System;
using System.Collections.Generic;
using System.Text;

namespace Exercise_5
{
class Program
{
static void Main(string[] args)
{
string[] str = { "I", "Love", "My", "India" };

foreach (string s in str)


{
Console.WriteLine(s);

}
Console.ReadLine();
}
}
}

6. Write a program in C# to create a string array to store 10 employees name.


Display the employees name to the console by passing array as a parameter to
the display function.

using System;
using System.Collections.Generic;
using System.Text;

namespace Exercise_6
{
class Program
{
public static void Display(Array arr)

Group Members: - RKM, Ashish, Ravi Ranjan, Pankaj, Shlesh, Purushottam, Nishant, Amlesh & Vishwjeet
{

foreach (string a in arr)


{
Console.WriteLine(a);
}
}

static void Main(string[] args)


{
string[] arr = new string[10];

for (int i = 0; i < arr.Length; i++)


{
Console.WriteLine("Enter " + (i + 1) + " Emplyee Name");
string str = Console.ReadLine();
arr.SetValue(str, i);
}

Console.WriteLine("The Names you have Entered is given below ");


Console.WriteLine();
Display(arr);

Console.ReadLine();
}
}
}

7. Write a program in C# to demonstrate the usage of checked and unchecked


statements.

NOTE:- PLEASE MAKE SURE SOLUTION IS CORRECT OR NOT

using System;
class rkm
{
public static void Main()
{
int i = Int32.MaxValue;
Console.WriteLine("Simple: " + (i + 1));
unchecked
{
Console.WriteLine("Unchecked: " + (i + 1));
}
checked
{
try
{
Console.WriteLine(i + 1);

Group Members: - RKM, Ashish, Ravi Ranjan, Pankaj, Shlesh, Purushottam, Nishant, Amlesh & Vishwjeet
}
catch (Exception ee)
{
Console.WriteLine("Error in Checked Block");
}
}
}
}

8. Write a class called rectangle which consists of members side_1, side_2 and
displayArea(). Write another class square which inherits class rectangle. Take
side of square as input and display the area of square on console.

using System;
using System.Collections.Generic;
using System.Text;
public class RKM
{
public double side_1;
public double side_2;
public void displayArea()
{
double area = side_1 * side_2;
Console.WriteLine("Area is:- " + area);
}
}

class Square : RKM


{
public static void Main()
{
Square sq = new Square();
Console.WriteLine("Enter the side of Square:-");
sq.side_1 = Convert.ToDouble(Console.ReadLine());
sq.side_2 = sq.side_1;
sq.displayArea();
}
}

9. Write a program to sort 20 decimal numbers in decreasing order and print the
result.

using System;
using System.Collections.Generic;
using System.Text;
class RKM

Group Members: - RKM, Ashish, Ravi Ranjan, Pankaj, Shlesh, Purushottam, Nishant, Amlesh & Vishwjeet
{
public static void Main(string[] args)
{
decimal[] arr = { 12.3m, 45.2m, 5.4m, 23.44m, 43.21m, 31.34m, 4.3m, 21.3m,
34.2m, 32.1m, 44.2m, 6.4m, 64.3m, 3.4m, 5.32m, 32.345m, 4.34m, 23.4m,
45.234m, 5.31m };
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine(arr[i]);
}
Console.WriteLine("\n--Number in decreasing order--\n");
int count = 0;
while (count < arr.Length - 1)
{
if (arr[count] < arr[count + 1])
{
decimal temp = arr[count];
arr[count] = arr[count + 1];
arr[count + 1] = temp;
count = 0;
continue;
}
count++;
}
foreach (double i in arr)
{
Console.WriteLine(i);
}
}
}

10. Write a program for matrix multiplication in C#.

using System;
using System.Collections.Generic;
using System.Text;

namespace Exercise_10
{
class Program
{
static void Main(string[] args)
{

int[,] x ={ { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
int[,] y ={ { 11, 12, 13 }, { 14, 15, 16 }, { 17, 18, 19 } };

Console.WriteLine("----First Matrix--------");
for (int i = 0; i < 3; i++)
{

Group Members: - RKM, Ashish, Ravi Ranjan, Pankaj, Shlesh, Purushottam, Nishant, Amlesh & Vishwjeet
for (int j = 0; j < 3; j++)
Console.Write(x[i, j] + " ");
Console.WriteLine();
}

Console.WriteLine("----Second Matrix--------");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
Console.Write(y[i, j] + " ");
Console.WriteLine();
}

Console.WriteLine("Multilpilcation of Two Matrix (Matrix1 * Matrix2) ");

for (int i = 0; i < 3; i++)


{
for (int j = 0; j < 3; j++)
Console.Write(x[i, j] * y[i, j] + " ");
Console.WriteLine();
}
Console.ReadLine();
}
}
}

11. Write a program in C# to sort the students list based on their names. The
students list is stored in a string array.

using System;
using System.Collections.Generic;
using System.Text;

namespace Exercise_11
{
class Program
{

public static void Display(Array arr)


{
foreach (string s in arr)
{
Console.WriteLine(s);

static void Main(string[] args)

Group Members: - RKM, Ashish, Ravi Ranjan, Pankaj, Shlesh, Purushottam, Nishant, Amlesh & Vishwjeet
{
Console.WriteLine("Enter the No of Student");
int n = Convert.ToInt16(Console.ReadLine());

string[] arr = new string[n];

for (int i = 0; i < arr.Length; i++)


{
Console.WriteLine("Enter " + (i + 1) + " Student Name");
string str = Console.ReadLine();
arr.SetValue(str, i);
}

Console.WriteLine("\nThe Names you have Entered i Sort order is given


below ");
Console.WriteLine();

Array.Sort(arr);

Display(arr);

Console.ReadLine();
}
}
}

12. Write a program in C# to demonstrate operator overloading.

NOTE:- PLEASE MAKE SURE SOLUTION IS CORRECT OR NOT.

using System;
class rkm
{
int a;
public rkm()
{
a = 10;
}
public static rkm operator -(rkm obj)
{
obj.a = -obj.a;
return obj;
}
public void display()
{
Console.WriteLine("Number is {0}", a);
}
public static void Main()
{
rkm aa = new rkm();

Group Members: - RKM, Ashish, Ravi Ranjan, Pankaj, Shlesh, Purushottam, Nishant, Amlesh & Vishwjeet
aa.display();
aa = -aa;
aa.display();
}
}

13. Write a program in C# to demonstrate boxing and unboxing opertation.

using System;
using System.Collections.Generic;
using System.Text;

namespace Exercise_13
{
class Program
{
static void Main(string[] args)
{
int i = 10;
Console.WriteLine("Value of i : " + i);
object obj = (object)(i+1); // boxing Convert int to object
Console.WriteLine("Value of obj :"+ obj);

i = (int)obj;
Console.WriteLine("Value of i after unbox "+i);
Console.ReadLine();
}
}
}

14. Write a program in C# to throw and catch any arithmetic exception.

using System;
using System.Collections.Generic;
using System.Text;

namespace Exercise_14
{
class Program
{
static void Main(string[] args)
{
try
{
int n1 =10;
int n = 0;
int res;

Group Members: - RKM, Ashish, Ravi Ranjan, Pankaj, Shlesh, Purushottam, Nishant, Amlesh & Vishwjeet
res = n1 / n;

// res = 10 / 0; Dont direclty use because exception generate when


you refer to Memory Location
Console.WriteLine(res);
}
catch (ArithmeticException ex)
{
Console.WriteLine(ex.Message.ToString());
}
Console.ReadLine();
}
}
}

15. Write a program to demonstrate the usage of threads in C#.

using System;
using System.Threading;
class rkm
{
public void get1()
{
for (int i = 0; i <= 10; i++)
{
Console.WriteLine("Welcome");
Thread.Sleep(1000);
}
}
public void get2()
{
for (int j = 0; j <= 10; j++)
{
Console.WriteLine("India");
Thread.Sleep(1000);
}
}
public static void Main()
{
rkm ff = new rkm();
ThreadStart ts1 = new ThreadStart(ff.get1);
ThreadStart ts2 = new ThreadStart(ff.get2);
Thread t1 = new Thread(ts1);
Thread t2 = new Thread(ts2);
t1.Start();
Thread.Sleep(500);
t2.Start();
}
}

Group Members: - RKM, Ashish, Ravi Ranjan, Pankaj, Shlesh, Purushottam, Nishant, Amlesh & Vishwjeet
Same Program in other style…
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace Exercise_15
{

class Program
{
public void Display()
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine(i);
Thread.Sleep(1000);
}
}
public static void Display(object obj)
{

for (int i = 0; i <= (int)obj; i++)


{
Console.WriteLine(i);
Thread.Sleep(1000);
}
}
public static void Main()
{
Program obj = new Program();
Thread t = new Thread(new ThreadStart(obj.Display));
t.Start();

t.Join();
Console.WriteLine("First Thhread is "+t.ThreadState.ToString());
Console.WriteLine("----------------------------------------------------------");

t = new Thread(new ParameterizedThreadStart(Display));


t.Start(5);
Console.WriteLine("Second Thread is " + t.ThreadState.ToString());

Console.ReadLine();

}
}

Group Members: - RKM, Ashish, Ravi Ranjan, Pankaj, Shlesh, Purushottam, Nishant, Amlesh & Vishwjeet
Group Members: - RKM, Ashish, Ravi Ranjan, Pankaj, Shlesh, Purushottam, Nishant, Amlesh & Vishwjeet

Anda mungkin juga menyukai