PART A
1. Write A C# Program To Add Two Numbers Using Command Line
Arguments.
INPUT: -
using System;
class Program
{
static void Main(string[] args)
{
int num1 = [Link](args[0]);
int num2 = [Link](args[1]);
[Link]("sum:" + (num1 + num2));
[Link]();
}
}
OUT PUT: -
2. Write A C# Program To Demonstrate The Use Of Methods And
Operators.
INPUT: -
using System;
class Program
{
// Arithmetic operation
static void ArithmeticOperators(int x, int y)
{
[Link]("Addition: " + (x + y));
}
// Relational operation
static void RelationalOperators(int x, int y)
{
[Link]("Equal: " + (x == y));
}
// Logical operation
static void LogicalOperators(bool x, bool y)
{
[Link]("AND: " + (x && y));
}
// Assignment operation
static void AssignmentOperators()
{
int a = 10;
[Link]("Initial value: " + a);
a += 5;
[Link]("After += 5: " + a);
}
static void Main()
{
int x = 20;
int y = 6;
[Link](" Arithmetic Operators ");
ArithmeticOperators(x, y);
[Link]("\n Relational Operators ");
RelationalOperators(x, y);
[Link]("\n Logical Operators ");
LogicalOperators(true, false);
[Link]("\n Assignment Operators ");
AssignmentOperators();
[Link]();
}
}
OUTPUT: -
3. Write A C# Program To Demonstrate String Function.
INPUT: -
using System;
class Program
{
static void Main()
{
string message = " Hello CSharp World! ";
[Link]("Length: " + [Link]);
[Link]("Uppercase: " + [Link]());
[Link]("Lowercase: " + [Link]());
[Link]("Substring(2, 5): " + [Link](2, 5));
[Link]("Replace 'CSharp' with 'C#': " + [Link]("CSharp", "C#"));
[Link]("Contains 'World': " + [Link]("World"));
[Link]("Index of 'C': " + [Link]('C'));
string first = "Hello";
string second = "World";
string result = [Link](first, " ", second);
[Link]("Concatenated String: " + result);
[Link]();
}
}
OUTPUT: -
4. Write A C# Program To Demonstrate Operations On An Arraylist.
INPUT: -
using System;
using [Link];
class ArrayListDemo
{
static void Main()
{
ArrayList cities = new ArrayList();
[Link]("Mysore");
[Link]("Banglore");
[Link]("Mandya");
[Link]("Initial ArrayList:");
Display(cities);
[Link]("Udupi");
[Link]("\nAfter Inserting/Adding 'Udupi' at index 1:");
Display(cities);
[Link]("Mandya");
[Link]("\nAfter removing 'Mandya':");
Display(cities);
[Link]();
[Link]("\nAfter sorting:");
Display(cities);
[Link]();
}
static void Display(ArrayList list)
{
foreach (var item in list)
{
[Link](item);
}
}
}
OUTPUT: -
5. Write A C# Program To Demonstrate Default And Parameterized
Constructors Using A Student Class.
INPUT: -
using System;
class Student
{
// Fields
public string name;
public int age;
// Default Constructor
public Student()
{
name = "Unknown";
age = 0;
[Link]("Default Constructor Called");
}
// Parameterized Constructor
public Student(string studentName, int studentAge)
{
name = studentName;
age = studentAge;
[Link]("Parameterized Constructor Called");
}
// Method to display student details
public void Display()
{
[Link]("Name: " + name);
[Link]("Age: " + age);
}
}
class Program
{
static void Main()
{
// Using default constructor
Student student1 = new Student();
[Link]();
// Using parameterized constructor
Student student2 = new Student("Ram", 20);
[Link]();
[Link]();
}
}
OUTPUT: -
6. Write C# Program To Demonstrate Multilevel Inheritance Using Classes
Person, Employee And Manager.
INPUT: -
using System;
namespace ConsoleApplication6
{
class Person
{
public string Name;
public int Age;
public void DisplayPersonInfo()
{
[Link]("Name: " + Name);
[Link]("Age: " + Age);
}
}
class Employee : Person
{
public int EmployeeId;
public string Department;
public void DisplayEmployeeInfo()
{
[Link]("Employee ID: " + EmployeeId);
[Link]("Department: " + Department);
}
}
class Manager : Employee
{
public string Role;
public void DisplayManagerInfo()
{
[Link]("Role: " + Role);
}
}
class Program
{
static void Main(string[] args)
{
Manager mgr = new Manager();
[Link] = "John";
[Link] = 35;
[Link] = 1001;
[Link] = "HR";
[Link] = "HR Manager";
[Link]("Manager Details:");
[Link]();
[Link]();
[Link]();
[Link]();
}
}
}
OUTPUT: -
7. Write C# Program To Demonstrate Method Overloading.
INPUT: -
using System;
namespace ConsoleApplication7
{
class Calulator
{
public void show(int a, int b)
{
[Link]("Sum of integers:" + (a + b));
}
public void show(double a, double b)
{
[Link]("Sum of doubles:" + (a + b));
}
}
class Program
{
static void Main(string[] args)
{
Calulator cal = new Calulator();
[Link](5, 10);
[Link](3.5, 2.5);
[Link]();
}
}
}
Output: -
8. Write C# Program To Overload The + Operator To Add Two Objects Of
A Complex Class.
Input: -
using System;
class Complex
{
public int real;
public int imag;
// Constructor
public Complex(int r, int i)
{
real = r;
imag = i;
}
// Overload + operator
public static Complex operator +(Complex a, Complex b)
{
return new Complex([Link] + [Link], [Link] + [Link]);
}
// Display method
public void Show()
{
[Link](real + " + " + imag + "i");
}
}
class Program
{
static void Main()
{
Complex c1 = new Complex(1, 2);
Complex c2 = new Complex(3, 4);
Complex c3 = c1 + c2;
[Link]("Result:");
[Link]();
[Link]();
}
}
OUTPUT: -