0% found this document useful (0 votes)
36 views13 pages

AWPpr Shriom Gupta

Uploaded by

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

AWPpr Shriom Gupta

Uploaded by

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

Practical 1a: Create an application that obtains four int values from

the user and displays the product.


INPUT:
using System;

class Program
{
static void Main(string[] args)
{
// Declare variables to store the four integers
int num1, num2, num3, num4;

// Prompt the user for input and read the values


Console.WriteLine("Enter the first integer:");
num1 = int.Parse(Console.ReadLine());

Console.WriteLine("Enter the second integer:");


num2 = int.Parse(Console.ReadLine());

Console.WriteLine("Enter the third integer:");


num3 = int.Parse(Console.ReadLine());

Console.WriteLine("Enter the fourth integer:");


num4 = int.Parse(Console.ReadLine());

// Calculate the product


int product = num1 * num2 * num3 * num4;

// Display the product


Console.WriteLine($"The product of the numbers is: {product}");
}
}

OUTPUT:
Practical 1b: Create an application to demonstrate string
operations.
INPUT:
using System;

class Program
{
static void Main(string[] args)
{
string firstName = "shriom ";
string lastName = "gupta";
string fullName = firstName + " " + lastName;

Console.WriteLine("String Concatenation:");
Console.WriteLine(fullName);
Console.WriteLine();

// Demonstrating string interpolation


Console.WriteLine("String Interpolation:");
Console.WriteLine($"Full Name: {fullName}");
Console.WriteLine();

// Demonstrating string length


Console.WriteLine("String Length:");
Console.WriteLine($"Length of the full name is: {fullName.Length}");
Console.WriteLine();

// Demonstrating substring extraction


Console.WriteLine("Substring Extraction:");
string firstThreeLetters = fullName.Substring(0, 3); // Extract first 3
characters
Console.WriteLine($"First three letters of the full name:
{firstThreeLetters}");
Console.WriteLine();

// Demonstrating string case changes


Console.WriteLine("Changing Case:");
Console.WriteLine($"Uppercase: {fullName.ToUpper()}");
Console.WriteLine($"Lowercase: {fullName.ToLower()}");
Console.WriteLine();

// Demonstrating trimming of spaces


string paddedString = " Hello shriom! ";
Console.WriteLine("Trimming Spaces:");
Console.WriteLine($"Before Trim: '{paddedString}'");
Console.WriteLine($"After Trim: '{paddedString.Trim()}'");
Console.WriteLine();

// Demonstrating finding a character


Console.WriteLine("Finding a Character:");
int indexOfSpace = fullName.IndexOf(" ");
Console.WriteLine($"The index of the space between first and last name
is: {indexOfSpace}");
Console.WriteLine();

// Demonstrating string replacement


Console.WriteLine("String Replacement:");
string modifiedName = fullName.Replace("shriom", "suraj");
Console.WriteLine($"Original name: {fullName}");
Console.WriteLine($"Modified name: {modifiedName}");
Console.WriteLine();

// Demonstrating string splitting


Console.WriteLine("String Splitting:");
string[] nameParts = fullName.Split(' ');
Console.WriteLine($"First Name: {nameParts[0]}, Last Name:
{nameParts[1]}");
Console.WriteLine();

// Demonstrating string formatting


Console.WriteLine("String Formatting:");
double price = 29.99;
int quantity = 3;
Console.WriteLine($"Total price for {quantity} items: {price *
quantity:C}");
Console.WriteLine();
}
}

OUTPUT:

Practical 1c: Create an application that receives the (Student ID,


Student Name, Course Name, Date of Birth) information of all the
students once the data entered.
INPUT:
using System;
using System.Collections.Generic;
class Student
{
// Properties to hold student information
public string StudentId { get; set; }
public string StudentName { get; set; }
public string CourseName { get; set; }
public DateTime DateOfBirth { get; set; }

// Constructor to initialize the Student object


public Student(string studentId, string studentName, string courseName,
DateTime dateOfBirth)
{
StudentId = studentId;
StudentName = studentName;
CourseName = courseName;
DateOfBirth = dateOfBirth;
}

// Method to display student details


public void DisplayStudentInfo()
{
Console.WriteLine($"Student ID: {StudentId}");
Console.WriteLine($"Student Name: {StudentName}");
Console.WriteLine($"Course Name: {CourseName}");
Console.WriteLine($"Date of Birth: {DateOfBirth.ToShortDateString()}");
Console.WriteLine();
}
}

class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>();

while (true)
{
Console.WriteLine("Enter Student Information");

// Input for Student ID


Console.Write("Enter Student ID: ");
string studentId = Console.ReadLine();

// Input for Student Name


Console.Write("Enter Student Name: ");
string studentName = Console.ReadLine();

// Input for Course Name


Console.Write("Enter Course Name: ");
string courseName = Console.ReadLine();

// Input for Date of Birth


DateTime dateOfBirth;
while (true)
{
Console.Write("Enter Date of Birth (dd/mm/yyyy): ");
string dobInput = Console.ReadLine();
if (DateTime.TryParse(dobInput, out dateOfBirth))
{
break;
}
else
{
Console.WriteLine("Invalid Date format. Please try again.");
}
}

// Create a new Student object and add it to the list


Student student = new Student(studentId, studentName, courseName,
dateOfBirth);
students.Add(student);

// Ask if the user wants to enter another student


Console.Write("Do you want to enter another student? (yes/no): ");
string continueInput = Console.ReadLine().ToLower();

if (continueInput != "yes")
{
break;
}
}

// Display all students' information


Console.WriteLine("\nAll Student Information:");
foreach (Student student in students)
{
student.DisplayStudentInfo();
}
}
}

OUTPUT:

Practical 2a: Create simple application to perform following


operations.
INPUT:
using System;
using System.Collections.Generic;
namespace Practical2A
{
class MyUtility
{
public int Fact(int x)
{
int f = 1;
for (int i = 2; i <= x; i++)
f = f * i;
return f;
}
public float DToR(float d)
{
return d * 83.96f;
}
public float RToD(float r)
{
return r / 83.96f;
}
public float CToF(float c)
{
return (c * 9) / 5 + 32;
}
}
internal class Program
{
static void Main(string[] args)
{
MyUtility m1 = new MyUtility();
Console.WriteLine("1. Factorial\n2. $ To inr \n3. inr To $ \n4.
Celsius to Fahrenhieit ");
Console.Write("Enter Choice ");
int c = int.Parse(Console.ReadLine());
if (c == 1)
{
Console.Write("Enter Number ");
int no = int.Parse(Console.ReadLine());
int ans = m1.Fact(no);
Console.WriteLine("Factorial " + ans);
}
else if (c == 2)
{
Console.Write("Enter $ amt ");
float d = float.Parse(Console.ReadLine());
float ans = m1.DToR(d);
Console.WriteLine("INR " + ans);
}
else if (c == 3)
{
Console.Write("Enter inr amt ");
float inr = float.Parse(Console.ReadLine());
float ans = m1.RToD(inr);
Console.WriteLine("$ " + ans);
}
else if (c == 4)
{
Console.Write("Enter Celsius ");
float ce = float.Parse(Console.ReadLine());
float ans = m1.CToF(ce);
Console.WriteLine("Fahrenhieit value " + ans);
}
else
{
Console.WriteLine("Wrong Value");
}
Console.ReadKey();
}
}
}
OUTPUT:
Practical 2b: Create simple application to demonstrate following
concepts.
INPUT (i)Function Overloading:
using System;
using System.Collections.Generic;
namespace Practical2b
{
class MyMaths
{
public int Add(int x, int y)
{
return x + y;
}
public float Add(float x, int y)
{ return x + y; }
public int Add(int x, int y, int z)
{ return x + y + z; }
}
internal class Program
{
static void Main(string[] args)
{
MyMaths m1 = new MyMaths();
Console.WriteLine(m1.Add(50, 94, 96));
Console.WriteLine(m1.Add(50, 94));
Console.WriteLine(m1.Add(50.3f, 94));
Console.ReadKey();
}
}
}
OUTPUT:

INPUT (ii)Inheritance(All Type):


using System;
// ============================
// Single Inheritance
// ============================
public class Animal
{
public void Eat()
{
Console.WriteLine("The animal is eating.");
}
}

// ============================
// Multilevel Inheritance
// ============================
public class Mammal : Animal
{
public void Walk()
{
Console.WriteLine("The mammal is walking.");
}
}

// ============================
// Hierarchical Inheritance
// ============================
public class Dog : Animal
{
public void Bark()
{
Console.WriteLine("The dog is barking.");
}
}

public class Cat : Animal


{
public void Meow()
{
Console.WriteLine("The cat is meowing.");
}
}

// ============================
// Multiple Inheritance (via Interfaces)
// ============================
public interface IPet
{
void Play();
}

public interface IPlayable


{
void Fetch();
}

// ============================
// Hybrid Inheritance (Using both Base Class and Interfaces)
// ============================
public class PetDog : Animal, IPet, IPlayable
{
public void Play()
{
Console.WriteLine("The dog is playing.");
}
public void Fetch()
{
Console.WriteLine("The dog is fetching the ball.");
}
}

// ============================
// Main Program (Demonstrating all types of Inheritance)
// ============================
class Program
{
static void Main()
{
Console.WriteLine("---- Single Inheritance Example ----");
Animal genericAnimal = new Animal();
genericAnimal.Eat();

Console.WriteLine("\n---- Multilevel Inheritance Example ----");


Mammal myMammal = new Mammal();
myMammal.Eat(); // Inherited from Animal
myMammal.Walk(); // Specific to Mammal

Console.WriteLine("\n---- Hierarchical Inheritance Example ----");


Dog myDog = new Dog();
myDog.Eat(); // Inherited from Animal
myDog.Bark(); // Specific to Dog

Cat myCat = new Cat();


myCat.Eat(); // Inherited from Animal
myCat.Meow(); // Specific to Cat

Console.WriteLine("\n---- Multiple Inheritance (via Interfaces) Example -


---");
PetDog myPetDog = new PetDog();
myPetDog.Eat(); // Inherited from Animal
myPetDog.Play(); // Implemented from IPet
myPetDog.Fetch(); // Implemented from IPlayable
}
}

OUTPUT:
NOTE: This example demonstrates all the different types of inheritance in C# running
together in a single program. You can observe how each type of inheritance works and how
they complement each other:

 Single Inheritance: One class inherits from another.


 Multilevel Inheritance: A chain of inheritance from parent to grandchild class.
 Hierarchical Inheritance: Multiple classes inherit from a single parent class.
 Multiple Inheritance (via Interfaces): A class implements multiple interfaces.
 Hybrid Inheritance: A combination of class inheritance and interface
implementation.

INPUT (iii)Constructor overloading:


using System;
class ADD
{
int x, y;
double f;
string s;
public ADD(int a, double b)
{
x = a;
f = b;
}
public ADD(int a, string b)
{
y = a;
s = b;
}
public void show()
{
Console.WriteLine("1st constructor (int + float): {0} ",
(x + f));
}
public void show1()
{
Console.WriteLine("2nd constructor (int + string): {0}",
(s + y));
}
}
internal class Program
{
static void Main(string[] args)
{
ADD g = new ADD(50, 70.20);
g.show();
ADD q = new ADD(50, "Roll No. is ");
q.show1();
Console.ReadKey();
}
}
OUTPUT:
INPUT (iv)Interface:
using System;
interface IAnimal
{
void animalSound(); // interface method (does not have a body)
}
// Pig "implements" the IAnimal interface
class Pig : IAnimal
{
public void animalSound()
{
// The body of animalSound() is provided here
Console.WriteLine("The pig says: tp tp ");
}
}
internal class Program
{
static void Main(string[] args)
{
Pig myPig = new Pig(); // Create a Pig object
myPig.animalSound();
Console.ReadKey();
}
}
OUTPUT:

Practical 2c: Create simple application to demonstrate use of


following concepts.
INPUT (i)Using Delegates and Events:
using System;

// Define a delegate type


public delegate void Notify(string message);

// Class that raises an event


public class Publisher
{
// Declare an event based on the delegate type
public event Notify OnNotify;

// Method to trigger the event


public void TriggerEvent(string message)
{
// Check if there are subscribers
OnNotify?.Invoke(message); // Trigger the event
}
}

// Subscriber class that listens to the event


public class Subscriber
{
public void OnEventOccurred(string message)
{
Console.WriteLine("Event received: " + message);
}
}

class Program
{
static void Main()
{
// Create instances of Publisher and Subscriber
Publisher publisher = new Publisher();
Subscriber subscriber = new Subscriber();

// Subscribe to the event


publisher.OnNotify += subscriber.OnEventOccurred;

// Trigger the event


publisher.TriggerEvent("Hello from the event!");
}
}
OUTPUT:

INPUT (ii)Exception Handling:


using System;

// Custom exception class


class MyException : Exception
{
// Default constructor
public MyException() : base() { }

// Constructor that accepts a custom message


public MyException(string message) : base(message) { }
}

internal class Program


{
static void Main(string[] args)
{
try
{
Console.Write("Enter employee number: ");
int eno = int.Parse(Console.ReadLine()); // Corrected this line

// Throw exception if employee number is greater than 800


if (eno > 800) throw new MyException("Invalid employee number");
}
catch (MyException ex) // Catch the custom exception
{
Console.WriteLine(ex.Message); // Display the exception message
}

Console.ReadKey(); // Corrected this line to ReadKey()


}
}
OUTPUT:

You might also like