Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
23 views

Using System

The document contains code examples demonstrating various C# programming concepts including: 1) Factorial calculation, currency conversion, and quadratic equation solver programs. 2) Demonstration of inheritance between Vehicle, Car, ElectricCar, and Truck classes. 3) Examples of constructors, properties, and method in a Student class. 4) Handling exceptions in a number division program. 5) Implementation of a delegate and event to subscribe a method to receive notifications.

Uploaded by

Pawan
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)
23 views

Using System

The document contains code examples demonstrating various C# programming concepts including: 1) Factorial calculation, currency conversion, and quadratic equation solver programs. 2) Demonstration of inheritance between Vehicle, Car, ElectricCar, and Truck classes. 3) Examples of constructors, properties, and method in a Student class. 4) Handling exceptions in a number division program. 5) Implementation of a delegate and event to subscribe a method to receive notifications.

Uploaded by

Pawan
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/ 11

using System;

namespace pract9
{
class Program
{

static void Main(string[] args)


{
Console.WriteLine("Pawan \n");
int num, fact = 1;
Console.WriteLine("Enter an number: ");
num = int.Parse(Console.ReadLine());
for (int i = 1; i <= num; i++)
{
fact = fact * i;
}
Console.WriteLine(fact);
Console.ReadKey();

}
}
}

Pract10
using System;

namespace pract10
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("pawan \n");
int inr;
Console.WriteLine("Enter an Dollar Rupees");
inr = int.Parse(Console.ReadLine());
double rupee = inr * 82.88;

Console.WriteLine("Converting After Dollar to indian Rupee : " +


rupee);
Console.ReadKey();

}
}
}

Pract 11
using System;

namespace pract11
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Pawan ");
int a, b, c;
Console.WriteLine("Enter an 'a' no: ");
a = int.Parse(Console.ReadLine());
Console.WriteLine("Enter an 'b' no: ");
b = int.Parse(Console.ReadLine());
Console.WriteLine("Enter an 'c' no: ");
c = int.Parse(Console.ReadLine());
double d, x1, x2;
d = b * b - 4 * a * c;
if (d == 0)
{
x1 = -b / (2.0 * a);
x2 = x1;
Console.WriteLine("First root 1" + x1);
Console.WriteLine("Second root 1" + x2);
}
else if (d > 0)
{
x1 = (-b + Math.Sqrt(d)) / (2 * a);
x2 = (-b - Math.Sqrt(d)) / (2 * a);
Console.WriteLine("First root " + x1);
Console.WriteLine("Second root " + x2);
}
else
{
Console.WriteLine("The answer is imaginary");
}
Console.ReadKey();

}
}
}

Pract 12

Console.WriteLine("Pawan \n");
double fahrenheit;
double celsius;
Console.WriteLine("Enter an Celsius: \n");
celsius = int.Parse(Console.ReadLine());
Console.WriteLine("Celsius: " + celsius);
fahrenheit = (celsius * 9) / 5 + 32;
Console.WriteLine("Fahrenheit: " + fahrenheit);
Console.ReadLine();
Pract13
using System;

namespace pract13
{
public class MathOperations
{
public int Add(int a, int b)
{
return a + b;
}

public double Add(double a, double b)


{
return a + b;
}

public string Add(string a, string b)


{
return a + b;
}
}

class Program
{

static void Main(string[] args)


{
MathOperations A = new MathOperations();

int intSum = A.Add(6, 3);


double doubleSum = A.Add(4.2, 2);
string stringConcatenation = A.Add("Hello", "Pawan");

Console.WriteLine($"Integer sum: {intSum}");


Console.WriteLine($"Double sum: {doubleSum}");
Console.WriteLine($"String concatenation: {stringConcatenation}");
Console.ReadKey();

}
}
}

Pract 14
using System;

// Base class
public class Vehicle
{
public void Start()
{
Console.WriteLine("The Pawan" +" vehicle is starting.");
}
}

// Single Inheritance: Derived class inherits from a single base class


public class Car : Vehicle
{
public void Drive()
{
Console.WriteLine("The car is driving.");
}
}

// Multilevel Inheritance: Derived class inherits from a class which itself inherits from
a base class
public class ElectricCar : Car
{
public void Charge()
{
Console.WriteLine("The electric car is charging.");
}
}

// Hierarchical Inheritance: Multiple derived classes inherit from the same base class
public class Truck : Vehicle
{
public void Haul()
{
Console.WriteLine("The truck is hauling.");
}
}

// Multiple Inheritance using Interfaces


public interface IFlyable
{
void Fly();
}

public interface ISubmersible


{
void Submerge();
}

public class AmphibiousVehicle : Vehicle, IFlyable, ISubmersible


{
public void Fly()
{
Console.WriteLine("The amphibious vehicle is flying.");
}

public void Submerge()


{
Console.WriteLine("The amphibious vehicle is submerging.");
}
}

class Program
{
static void Main(string[] args)
{
Car car = new Car();
car.Start();
car.Drive();

ElectricCar electricCar = new ElectricCar();


electricCar.Start();
electricCar.Drive();
electricCar.Charge();

Truck truck = new Truck();


truck.Start();
truck.Haul();

AmphibiousVehicle amphibiousVehicle = new AmphibiousVehicle();


amphibiousVehicle.Start();
amphibiousVehicle.Fly();
amphibiousVehicle.Submerge();
Console.ReadKey();
}
}
Pract15
using System;

namespace pract15
{
public class Student
{
public string Name { get; set; }
public int Age { get; set; }
public string Major { get; set; }

// Default constructor
public Student()
{
Name = "Unknown";
Age = 0;
Major = "Undeclared";
}

// Constructor with two parameters


public Student(string name, int age)
{
Name = name;
Age = age;
Major = "Undeclared";
}

// Constructor with three parameters


public Student(string name, int age, string major)
{
Name = name;
Age = age;
Major = major;
}

public void DisplayInfo()


{
Console.WriteLine($"Name: {Name}, Age: {Age}, Major: {Major}");
}
}

class Program
{
static void Main(string[] args)
{
Student student1 = new Student("Pawan", 20, "BscIt");
Student student2 = new Student("Akash", 20, "Mca");
Student student3 = new Student("Ahin", 22, "Computer Science");

Console.WriteLine("Student 1:");
student1.DisplayInfo();

Console.WriteLine("\nStudent 2:");
student2.DisplayInfo();

Console.WriteLine("\nStudent 3:");
student3.DisplayInfo();
Console.ReadKey();
}
}
}

Pract16
using System;
namespace pract16
{
class Program
{
static void Main(string[] args)
{
try
{
Console.Write("Pawan\n");
Console.Write("Enter a number: ");
int num = Convert.ToInt32(Console.ReadLine());

int result = 10 / num;

Console.WriteLine($"Result: {result}");
}
catch (DivideByZeroException)
{
Console.WriteLine("Error: Cannot divide by zero.");
}
catch (FormatException)
{
Console.WriteLine("Error: Invalid input format.");
}
finally
{
Console.WriteLine("Execution completed.");
}
Console.ReadKey();
}
}
}

Pract17
using System;

namespace pract17
{
// Define a delegate
delegate void MessageHandler(string message);

class Notifier
{
// Declare an event of the delegate type
public event MessageHandler MessageSent;

public void SendNotification(string message)


{
Console.WriteLine($"Sending notification: {message}");
MessageSent?.Invoke(message);
}
}

class Subscriber
{
public void OnMessageReceived(string message)
{
Console.WriteLine($"Received message: {message}");
}
}

class Program
{
static void Main(string[] args)
{
Notifier notifier = new Notifier();
Subscriber subscriber = new Subscriber();

// Subscribe the subscriber's method to the event


notifier.MessageSent += subscriber.OnMessageReceived;

Console.WriteLine("Enter a notification message:");


string message = Console.ReadLine();

// Send the notification, which triggers the event


notifier.SendNotification(message);
Console.ReadKey();
}
}
}
Pract 18

You might also like