6 Sem Kuvempu University Practical Solution
6 Sem Kuvempu University Practical Solution
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;
class prac1 { public static void Main(string[] args) { foreach(string k in args) { Console.Write(k+" "); } Console.ReadLine(); } }
2.) Write a program in C# to find the area of a circle, given its radius.
using System;
class prac2 { public static void Main() { float radius=8.9f; float pi=3.14f; float area; area=pi*radius*radius; Console.WriteLine("The Area Of Circle whose Radius is {0} :- {1}",radius,area); Console.ReadLine(); } }
3.) Write a program in C# to find the area of polygon triangle and rectangle by using multiple constructors. using System;
Console.WriteLine("The Area of Triangle of length {0} and height {1} is: {2}",length,height,areaoftriangle); Console.ReadLine(); } prac3(double len,double breadth) { double areaofrectangle=len*breadth; Console.WriteLine("The Area of Rectangle of length {0} and height {1} is: {2}",len,breadth,areaofrectangle); Console.ReadLine(); } public static void Main() { prac3 obj1=new prac3(0.5,4.2,3.8); prac3 obj2=new prac3(5.2,2.6); Console.ReadLine(); } }
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;
class prac4
{ public static void add(ref int num1,ref int num2) { int res_add=num1+num2; Console.WriteLine("The Addition Of {0} and {1} usingis : {2} ",num1,num2,res_add); Console.ReadLine(); } public static void product(out int number1,out int number2) { number1=10; number2=10; int res_product=number1*number2; Console.WriteLine ("The Product Of {0} and {1} usingis : {2}",number1,number2,res_product); Console.ReadLine(); } public static void Main(string[] args) { int num1; int num2; num1=10; num2=10; add(ref num1,ref num2); int number1;
5.) Using foreach looping statement, write a program in C# to display all the elements of string array.
using System;
class prac5 { public static void Main(string[] args) { string[] name={"Shakty","Shashi","Gaurav","Rahul","Pawan","Gandharva","Rakesh"}; foreach(string element in name) { Console.WriteLine(element); Console.WriteLine(); } 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;
class prac6 { public static void display(out string[] name) { name=new string[10]{"Shakty","Ronisha","Shashi","Gaurav","Rahul","Pawan","Gandharva","Rakesh","Ma nish","Mohd. Arif"}; } public static void Main(string[] args) { string[] name; display(out name); Console.WriteLine("Array Elements Are :"); for(int i=0;i { Console.WriteLine(name[i]); } Console.WriteLine(); Console.WriteLine(); Console.WriteLine();
7.) Write a program in C# to demonstrate the usage of checked and unchecked statements.
using System;
class prac7 { static void Main() { int x=Int32.MaxValue; Console.WriteLine(x+1); checked { Console.WriteLine(x+1); }
unchecked { Console.WriteLine(x+1); } } }
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;
public class rectangle { public int side_1=30; public int side_2=40; public virtual void displayArea() { int area=side_1*side_2; Console.WriteLine("Area of Rectangle is:-{0}",area); } } class square:rectangle
{ public override void displayArea() { int area=side_1*side_1; Console.WriteLine("Area of Square whose side {0} which is inherited fromclass is:{1}",side_1,area); } public static void Main() { square obj=new square(); obj.displayArea(); Console.ReadLine(); } } 9.) Write a program to sort 20 decimal numbers in decreasing order and print the result.
using System;
int i=0;
System.Console.WriteLine(x);
double[] a = new double[] {87.1,76.4,76.2,80.5,96.7,4.56,34.11,100.45,89.07,44.34,200.1,10.34,96.45,72.33,7.4,2.5,65.56,8 7.87,99.83,101.66}; sort(a); display(a); Console.Read(); } } } 10.) Write a program for matrix multiplication in C#. using System; class Matrix3D { public const int DIMSIZE = 3; private double[ , ] matrix = new double[DIMSIZE, DIMSIZE]; public double this[int x,int y] { get { return matrix[x, y]; } set { matrix[x, y] = value;} }
public static Matrix3D operator *(Matrix3D mat1, Matrix3D mat2) { Matrix3D newMatrix = new Matrix3D(); for (int x=0; x < DIMSIZE; x++) for (int y=0; y < DIMSIZE; y++) newMatrix[x, y] = mat1[x, y] * mat2[x, y]; return newMatrix; } } class MatrixTest { public static Random rand = new Random(); static void Main() { Matrix3D mat1 = new Matrix3D(); Matrix3D mat2 = new Matrix3D(); InitMatrix(mat1); InitMatrix(mat2); Console.WriteLine("Matrix 1 : "); PrintMatrix(mat1); Console.WriteLine("Marix 2: "); PrintMatrix(mat2); Matrix3D mat3 = mat1 * mat2;
Console.WriteLine(); Console.WriteLine("Matrix 1 * Matrix 2 = "); PrintMatrix(mat3); } public static void InitMatrix(Matrix3D mat) { for (int x=0; x < Matrix3D.DIMSIZE; x++) for (int y=0; y < Matrix3D.DIMSIZE; y++) mat[x, y] = rand.NextDouble()*10; } //done
public static void PrintMatrix(Matrix3D mat) { Console.WriteLine(); for (int x=0; x< Matrix3D.DIMSIZE; x++) { Console.Write("[ "); for (int y=0; y < Matrix3D.DIMSIZE; y++) { Console.Write("{0,8: # . 00}",mat[x, y]); if((y+1 % 2) { Console.Write(" , "); }
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;
namespace sorting {
class Class1 {
[STAThread]
} i++; } }
System.Console.WriteLine(x);
string[] b = new String[] {"sumit","samit","scmit","sgmit","sglit"}; sort(b); display(b); Console.Read(); } } } 12.) Write a program in C# to demonstrate operator overloading using System; class Matrix3D { public const int DIMSIZE = 3; private double[ , ] matrix = new double[DIMSIZE, DIMSIZE]; public double this[int x,int y] { get { return matrix[x, y]; } set { matrix[x, y] = value;}
} public static Matrix3D operator +(Matrix3D mat1, Matrix3D mat2) { Matrix3D newMatrix = new Matrix3D(); for (int x=0; x < DIMSIZE; x++) for (int y=0; y < DIMSIZE; y++) newMatrix[x, y] = mat1[x, y] + mat2[x, y]; return newMatrix; } } class MatrixTest { public static Random rand = new Random(); static void Main() { Matrix3D mat1 = new Matrix3D(); Matrix3D mat2 = new Matrix3D(); InitMatrix(mat1); InitMatrix(mat2); Console.WriteLine("Matrix 1 : "); PrintMatrix(mat1); Console.WriteLine("Marix 2: "); PrintMatrix(mat2);
Matrix3D mat3 = mat1 + mat2; Console.WriteLine(); Console.WriteLine("Matrix 1 + Matrix 2 = "); PrintMatrix(mat3); } public static void InitMatrix(Matrix3D mat) { for (int x=0; x < Matrix3D.DIMSIZE; x++) for (int y=0; y < Matrix3D.DIMSIZE; y++) mat[x, y] = rand.NextDouble()*10; } public static void PrintMatrix(Matrix3D mat) { Console.WriteLine(); for (int x=0; x< Matrix3D.DIMSIZE; x++) { Console.Write("[ "); for (int y=0; y < Matrix3D.DIMSIZE; y++) { Console.Write("{0,8: # . 00}",mat[x, y]); if((y+1 % 2) { Console.Write(" , ");
} Console.WriteLine(" ] "); } Console.WriteLine(); } }} 13.) Write a program in C# to demonstrate boxing and unboxing opertation. using System;
class prac13 { public static void Main() { int unbox_var=10; int box_var=100; object obj=box_var; //boxing ie converting value type to reference type Console.WriteLine("Value after boxing is : {0}",obj); unbox_var=(int)obj; //unboxing ie converting reference type to value type
Console.WriteLine("Value after unboxing is : {0} ",unbox_var); Console.ReadLine(); } } 14.) Write a program in C# to throw and catch any arithmetic exception.
using System;
class prac14 { public static void Main() { int num1=10; int num2=0; int div; try { div=num1/num2; Console.WriteLine(div); Console.ReadLine(); } catch(DivideByZeroException e) { Console.WriteLine("Exception Message : "+e.Message); } } } 15.) Write a program to demonstrate the usage of threads in C#. using System;
} public EventWaitHandle NewItemEvent { get { return _newItemEvent; } } public WaitHandle[] EventArray { get { return _eventArray; } }
public void ThreadRun() { int count = 0; Random r = new Random(); while (!_syncEvents.ExitThreadEvent.WaitOne(0, false)) { lock (((ICollection)_queue).SyncRoot) { while (_queue.Count < 20) { _queue.Enqueue(r.Next(0, 100)); _syncEvents.NewItemEvent.Set(); count++; } } } Console.WriteLine("Producer thread: produced {0} items", count); } private Queue_queue; private SyncEvents _syncEvents; }
public class Consumer { public Consumer(Queueq, SyncEvents e) { _queue = q; _syncEvents = e; } public void ThreadRun() { int count = 0; while (WaitHandle.WaitAny(_syncEvents.EventArray) != 1) { lock (((ICollection)_queue).SyncRoot) { int item = _queue.Dequeue(); } count++; } Console.WriteLine("Consumer Thread: consumed {0} items", count); } private Queue_queue; private SyncEvents _syncEvents; }
Console.WriteLine("Configuring worker threads..."); Producer producer = new Producer(queue, syncEvents); Consumer consumer = new Consumer(queue, syncEvents);
Console.WriteLine("main thread waiting for threads to finish..."); producerThread.Join(); consumerThread.Join(); } } Created with page4, copyright 2012 by Humansoftware Development Corp., USA. All rights reserved. Login