C# File
C# File
C# File
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);
{
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
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();
}
}
}
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();
}
}
}
}
}
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();
}
}
}