Q13.
Write a program to input a complex number that has three parts- real,
imaginary and i_imaginary. Overload unary ++ & -- operator to
increment/decrement the complex number. +, - & *operators to add substract
and multiply two complex numbers.
Ans:
using System;
using [Link];
using [Link];
using [Link];
namespace Mayank
{
public class Complex
{
public int real;
public int imaginary;
public Complex(int real, int imaginary)
{
[Link] = real;
[Link] = imaginary;
}
public static Complex operator +(Complex c1, Complex c2)
{
return new Complex([Link] + [Link], [Link] + [Link]);
}
public static Complex operator -(Complex c1, Complex c2)
{
return new Complex([Link] - [Link], [Link] - [Link]);
}
public static Complex operator *(Complex c1, Complex c2)
{
return new Complex((([Link] * [Link]) - ([Link] * [Link])), (([Link] *
[Link]) + ([Link] * [Link])));
}
public override string ToString()
{
return ([Link]("{0} + {1}i", real, imaginary));
}
}
class TestComplex
{
static void Main()
{
Complex num1 = new Complex(2, 3);
Complex num2 = new Complex(3, 4);
Complex sum = num1 + num2;
Complex minus = num1 - num2;
Complex Multiplication = num1 * num2;
[Link]("First complex number: {0}", num1);
[Link]("\nSecond complex number: {0}", num2);
[Link]("\n\nThe sum of the two numbers: {0}", sum);
[Link]("\n\nThe Minus of the two numbers: {0}", minus);
[Link]("\n\nThe Multiplication of the two numbers: {0}", Multiplication);
[Link]("\nPress any key to exit..");
[Link]();
}
}
}
Q14. Write a program to Subscribe and unsubscribe an event.
Ans:
using System;
using [Link];
using [Link];
using [Link];
namespace Mayank
{
class Questionn14
{
public class MediaStorage
{
public delegate int PlayMedia();
public void ReportResult(PlayMedia playerDelegate)
{
if (playerDelegate() == 0)
{
[Link]("Media played successfully.");
}
else
{
[Link]("Media did not play successfully.");
}
}
}
public class AudioPlayer
{
private int audioPlayerStatus;
public int PlayAudioFile()
{
[Link]("Simulating playing an audio file here.");
audioPlayerStatus = 0;
return audioPlayerStatus;
}
}
public class VideoPlayer
{
private int videoPlayerStatus;
public int PlayVideoFile()
{
[Link]("Simulating a failed video file here.");
videoPlayerStatus = -1;
return videoPlayerStatus;
}
}
public class Tester
{
public void Run()
{
MediaStorage myMediaStorage = new MediaStorage();
// instantiate the two media players
AudioPlayer myAudioPlayer = new AudioPlayer();
VideoPlayer myVideoPlayer = new VideoPlayer();
// instantiate the delegates
[Link] audioPlayerDelegate = new
[Link]([Link]);
[Link] videoPlayerDelegate = new
[Link]([Link]);
// call the delegates
[Link](audioPlayerDelegate);
[Link](videoPlayerDelegate);
}
}
class Program
{
static void Main(string[] args)
{
Tester t = new Tester();
[Link]();
[Link]();
}
}
}
}
Q16. Create a class called Publication that stores the title (a string) and price ( a
float) of publication. From this class derive two classes: book, which adds a
page count ( a int), and tape, which adds playing time in minutes(a float). Each
of these classes should have a getdata() method to get its data from the user at
the keyboard, and a displaydata() to display its data. Declare Instance objects
of book and tape and cal the getdata and display data of the classes.
Ans:
using System;
using [Link];
using [Link];
using [Link];
namespace Mayank
{
public class Publication
{
public string title;
public double price;
//public void settitle(string t,float p)
//{
// title=t;
// price=p;
//}
public void getvalue()
{
[Link]("\n\nEnter Book Title :");
title = [Link]();
[Link]("Enter Book Price :");
price = [Link]([Link]());
}
}
public class Book : Publication
{
int page;
public void getdata()
{
[Link]("Enter Number of Pages :");
page = Convert.ToInt32([Link]());
}
public void pagecount()
{
[Link]("\n\nTitle of the Book is :" + title);
[Link]("Price of the Book is :" + price);
[Link]("Total No. of Pages is :" + page);
}
}
public class Tape : Publication
{
double min;
public void getdata()
{
[Link]("Enter Minutes :");
min = [Link]([Link]());
}
public void playingtime()
{
[Link]("\n\nTitle of the Book is :" + title);
[Link]("Price of the Book is :" + price);
[Link]("Total No. of Playing Time is :" + min);
}
}
class Question16
{
static void Main(string[] args)
{
char ch;
Book b = new Book();
Tape t = new Tape();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
}
}
}
Q17. Write a Program C# to create functions using lambda expressions that returns
a) The factorial of the value it is passed.
b) The Sum of digits of the number passed.
Ans:
(a):
using System;
using [Link];
using [Link];
using [Link];
using [Link];
namespace Mayank
{
delegate int IntOp(int end);
class Question17
{
static void Main(String[] args)
{
IntOp fact = n =>
{
int r = 1;
for (int i = 1; i <= n; i++)
{
r = i * r;
}
return r;
};
[Link]("The factorial of 3 is " + fact(3));
[Link]("The factorial of 5 is " + fact(5));
[Link]();
}
}
}
(b):
using System;
using [Link];
using [Link];
using [Link];
using [Link];
namespace Mayank
{
class Math
{
public int sum(int x, int y)
{
return x + y;
}
}
class Question17_2
{
delegate int del(int x, int y);
static void Main(string[] args)
{
Math m = new Math();
del add = [Link];
int result = add(5, 10);
[Link]("Sum of the Numbers is : " + result);
[Link]();
}
}
}
Q19. Write a C# Program to obtain the positive values contained in an array of
integer using LINQ.
Ans:
using System;
using [Link];
using [Link];
using [Link];
namespace Mayank
{
class Question19
{
static void Main(string[] args)
{
//int[] scores = { 90, 71, 82, 93, 75, 82 };
int[] scores = new int[5];
for (int i = 0; i < 5; i++)
{
[Link]("Enter Number : ");
scores[i] = Convert.ToInt32([Link]());
}
int highScoreCount = [Link](n => n > 0).Count();
[Link]("\n\n{0} Numbers are Positive", highScoreCount);
[Link]();
}
}
}
Q21. Write a C# Program that uses order by to retrieve the values in an int array in
ascending order using LINQ.
Ans:
using System;
using [Link];
using [Link];
using [Link];
namespace Mayank
{
class Question21
{
static void Main(string[] args)
{
//int[] scores = { 90, 71, 82, 93, 75, 82 };
int[] scores = new int[5];
for (int i = 0; i < 5; i++)
{
[Link]("Enter Number : ");
scores[i] = Convert.ToInt32([Link]());
}
var number = from n in scores
orderby n
select n;
[Link]("\n");
foreach (var n in number)
{
[Link]("\t{0} ", n);
}
[Link]();
}
}
}
Q23. Write a C# program to add elements to a XML file (Student database) and
retrieve values form the XML file where student name starts with A and D
using LINQ.
Ans:
using System;
using [Link];
using [Link];
using [Link];
using [Link];
namespace Mayank
{
class Question21
{
static void Main(string[] args)
{
string[] scores = {"mayank","agarwal","Dbms","Dhruv"};
var number= from n in scores
where [Link]("A") || [Link]("D")
select n;
[Link]("\n");
foreach(var x in number)
{
[Link]("\t{0} ",x);
}
[Link]();
}
}
}
Q25. Write a C# program to invoke the thread in Sleep or Wait state.
Ans:
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
namespace Mayank
{
class Question25
{
static void Main()
{
var stopwatch = [Link]();
[Link](0);
[Link]();
[Link]([Link]);
[Link]([Link]());
stopwatch = [Link]();
[Link](5000);
[Link]();
[Link]([Link]);
[Link]([Link]());
stopwatch = [Link]();
[Link](1000);
[Link]();
[Link]([Link]);
stopwatch = [Link]();
[Link](100000 * 10000);
[Link]();
[Link]([Link]);
[Link]("\n\nPress any key to continue...");
[Link]();
}
}
}
Q26. Write a C# program to get the process id for all of currently running process
and stop any of the process.
Ans:
using System;
using [Link];
using [Link];
using [Link];
using [Link];
namespace Mayank
{
class Question26
{
public static void Main()
{
Process notePad = [Link]("notepad");
[Link]("Started notepad process Id = " + [Link]);
[Link]("All instances of notepad:");
Process[] localByName = [Link]("notepad");
int i = [Link];
while (i > 0)
{
[Link](localByName[i - 1].[Link]());
i -= 1;
}
Process chosen;
i = [Link];
while (i > 0)
{
[Link]("Enter a process Id to kill the process");
string id = [Link]();
if (id == "")
break;
try
{
chosen = [Link]([Link](id));
}
catch (Exception e)
{
[Link]("Incorrect entry.");
continue;
}
if ([Link] == "notepad")
{
[Link]();
[Link]();
}
i -= 1;
}
[Link]();
}
}
}
Q27. Write a program to input a computer name and print the IP address of the
computer.
Ans:
using System;
using [Link];
using [Link];
using [Link];
using [Link];
namespace Mayank
{
class Program
{
static void Main(string[] args)
{
string hostName = [Link]();
[Link](hostName);
string myIP = [Link](hostName).AddressList[0].ToString();
[Link]("My IP Address is :" + myIP);
[Link]();
}
}
}