0% found this document useful (0 votes)
129 views

C# File

.net

Uploaded by

rajesh_34
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
129 views

C# File

.net

Uploaded by

rajesh_34
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

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 System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Mayank
{
public class Complex
{
public int real;
public int imaginary;
public Complex(int real, int imaginary)
{
this.real = real;
this.imaginary = imaginary;
}
public static Complex operator +(Complex c1, Complex c2)
{
return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
}
public static Complex operator -(Complex c1, Complex c2)
{
return new Complex(c1.real - c2.real, c1.imaginary - c2.imaginary);
}
public static Complex operator *(Complex c1, Complex c2)
{
return new Complex(((c1.real * c2.real) - (c1.imaginary * c2.imaginary)), ((c1.real *
c2.imaginary) + (c1.imaginary * c2.real)));
}
public override string ToString()
{
return (System.String.Format("{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;
Console.WriteLine("First complex number: {0}", num1);
Console.WriteLine("\nSecond complex number: {0}", num2);
Console.WriteLine("\n\nThe sum of the two numbers: {0}", sum);
Console.WriteLine("\n\nThe Minus of the two numbers: {0}", minus);
Console.WriteLine("\n\nThe Multiplication of the two numbers: {0}", Multiplication);
Console.WriteLine("\nPress any key to exit..");
Console.ReadKey();
}
}
}

Q14. Write a program to Subscribe and unsubscribe an event.


Ans:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Mayank
{
class Questionn14
{
public class MediaStorage

{
public delegate int PlayMedia();
public void ReportResult(PlayMedia playerDelegate)
{
if (playerDelegate() == 0)
{
Console.WriteLine("Media played successfully.");
}
else
{
Console.WriteLine("Media did not play successfully.");
}
}
}
public class AudioPlayer
{
private int audioPlayerStatus;
public int PlayAudioFile()
{
Console.WriteLine("Simulating playing an audio file here.");
audioPlayerStatus = 0;
return audioPlayerStatus;
}
}
public class VideoPlayer
{
private int videoPlayerStatus;
public int PlayVideoFile()
{
Console.WriteLine("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

MediaStorage.PlayMedia audioPlayerDelegate = new


MediaStorage.PlayMedia(myAudioPlayer.PlayAudioFile);
MediaStorage.PlayMedia videoPlayerDelegate = new
MediaStorage.PlayMedia(myVideoPlayer.PlayVideoFile);
// call the delegates
myMediaStorage.ReportResult(audioPlayerDelegate);
myMediaStorage.ReportResult(videoPlayerDelegate);
}
}
class Program
{
static void Main(string[] args)
{
Tester t = new Tester();
t.Run();
Console.ReadKey();
}
}
}
}

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 System.Collections.Generic;
using System.Linq;
using System.Text;
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()
{
Console.WriteLine("\n\nEnter Book Title :");
title = Console.ReadLine();
Console.WriteLine("Enter Book Price :");
price = Convert.ToDouble(Console.ReadLine());
}
}
public class Book : Publication
{
int page;
public void getdata()
{
Console.WriteLine("Enter Number of Pages :");
page = Convert.ToInt32(Console.ReadLine());
}
public void pagecount()
{
Console.WriteLine("\n\nTitle of the Book is :" + title);
Console.WriteLine("Price of the Book is :" + price);
Console.WriteLine("Total No. of Pages is :" + page);
}
}
public class Tape : Publication
{
double min;
public void getdata()
{
Console.WriteLine("Enter Minutes :");
min = Convert.ToDouble(Console.ReadLine());
}
public void playingtime()

{
Console.WriteLine("\n\nTitle of the Book is :" + title);
Console.WriteLine("Price of the Book is :" + price);
Console.WriteLine("Total No. of Playing Time is :" + min);
}
}
class Question16
{
static void Main(string[] args)
{
char ch;
Book b = new Book();
Tape t = new Tape();
b.getvalue();
b.getdata();
b.pagecount();
t.getvalue();
t.getdata();
t.playingtime();
Console.ReadLine();
}

}
}

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 System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
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;
};
Console.WriteLine("The factorial of 3 is " + fact(3));
Console.WriteLine("The factorial of 5 is " + fact(5));
Console.ReadLine();
}
}
}

(b):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
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 = m.sum;
int result = add(5, 10);
Console.WriteLine("Sum of the Numbers is : " + result);
Console.ReadLine();
}
}
}

Q19. Write a C# Program to obtain the positive values contained in an array of


integer using LINQ.
Ans:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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++)
{
Console.Write("Enter Number : ");
scores[i] = Convert.ToInt32(Console.ReadLine());
}
int highScoreCount = scores.Where(n => n > 0).Count();
Console.WriteLine("\n\n{0} Numbers are Positive", highScoreCount);
Console.ReadLine();
}
}
}

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 System.Collections.Generic;
using System.Linq;
using System.Text;
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++)
{
Console.Write("Enter Number : ");
scores[i] = Convert.ToInt32(Console.ReadLine());
}
var number = from n in scores
orderby n
select n;
Console.Write("\n");
foreach (var n in number)
{
Console.Write("\t{0} ", n);
}
Console.ReadLine();
}
}
}

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 System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace Mayank
{
class Question21
{
static void Main(string[] args)
{
string[] scores = {"mayank","agarwal","Dbms","Dhruv"};
var number= from n in scores
where n.StartsWith("A") || n.StartsWith("D")
select n;
Console.Write("\n");
foreach(var x in number)
{
Console.Write("\t{0} ",x);
}
Console.ReadLine();
}

}
}

Q25. Write a C# program to invoke the thread in Sleep or Wait state.


Ans:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
namespace Mayank
{
class Question25
{
static void Main()
{
var stopwatch = Stopwatch.StartNew();
Thread.Sleep(0);
stopwatch.Stop();
Console.WriteLine(stopwatch.ElapsedMilliseconds);
Console.WriteLine(DateTime.Now.ToLongTimeString());
stopwatch = Stopwatch.StartNew();
Thread.Sleep(5000);
stopwatch.Stop();
Console.WriteLine(stopwatch.ElapsedMilliseconds);
Console.WriteLine(DateTime.Now.ToLongTimeString());
stopwatch = Stopwatch.StartNew();
System.Threading.Thread.Sleep(1000);
stopwatch.Stop();
Console.WriteLine(stopwatch.ElapsedMilliseconds);
stopwatch = Stopwatch.StartNew();
Thread.SpinWait(100000 * 10000);
stopwatch.Stop();
Console.WriteLine(stopwatch.ElapsedMilliseconds);
Console.WriteLine("\n\nPress any key to continue...");
Console.ReadLine();
}

}
}

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 System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace Mayank
{
class Question26
{
public static void Main()
{
Process notePad = Process.Start("notepad");
Console.WriteLine("Started notepad process Id = " + notePad.Id);
Console.WriteLine("All instances of notepad:");
Process[] localByName = Process.GetProcessesByName("notepad");
int i = localByName.Length;
while (i > 0)
{
Console.WriteLine(localByName[i - 1].Id.ToString());
i -= 1;
}
Process chosen;
i = localByName.Length;
while (i > 0)

{
Console.WriteLine("Enter a process Id to kill the process");
string id = Console.ReadLine();
if (id == "")
break;
try
{
chosen = Process.GetProcessById(Int32.Parse(id));
}
catch (Exception e)
{
Console.WriteLine("Incorrect entry.");
continue;
}
if (chosen.ProcessName == "notepad")
{
chosen.Kill();
chosen.WaitForExit();
}
i -= 1;
}

Console.ReadLine();
}
}
}

Q27. Write a program to input a computer name and print the IP address of the
computer.
Ans:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
namespace Mayank
{
class Program
{
static void Main(string[] args)
{
string hostName = Dns.GetHostName();
Console.WriteLine(hostName);
string myIP = Dns.GetHostByName(hostName).AddressList[0].ToString();
Console.WriteLine("My IP Address is :" + myIP);
Console.ReadKey();
}
}
}

You might also like