Thanks to visit codestin.com
Credit goes to www.scribd.com

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

C Sharp PART A Programs

The document contains multiple C# programming exercises demonstrating various concepts such as command line arguments, methods and operators, string functions, array lists, constructors, multilevel inheritance, method overloading, and operator overloading. Each section includes code examples and expected outputs for clarity. The exercises aim to provide practical understanding of C# programming fundamentals.
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)
4 views13 pages

C Sharp PART A Programs

The document contains multiple C# programming exercises demonstrating various concepts such as command line arguments, methods and operators, string functions, array lists, constructors, multilevel inheritance, method overloading, and operator overloading. Each section includes code examples and expected outputs for clarity. The exercises aim to provide practical understanding of C# programming fundamentals.
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

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 = int.Parse(args[0]);
int num2 = int.Parse(args[1]);
Console.WriteLine("sum:" + (num1 + num2));
Console.ReadKey();
}
}

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)
{
Console.WriteLine("Addition: " + (x + y));
}
// Relational operation
static void RelationalOperators(int x, int y)
{
Console.WriteLine("Equal: " + (x == y));
}
// Logical operation
static void LogicalOperators(bool x, bool y)
{
Console.WriteLine("AND: " + (x && y));
}
// Assignment operation
static void AssignmentOperators()
{
int a = 10;
Console.WriteLine("Initial value: " + a);

a += 5;
Console.WriteLine("After += 5: " + a);
}
static void Main()
{
int x = 20;
int y = 6;
Console.WriteLine(" Arithmetic Operators ");
ArithmeticOperators(x, y);
Console.WriteLine("\n Relational Operators ");
RelationalOperators(x, y);
Console.WriteLine("\n Logical Operators ");
LogicalOperators(true, false);
Console.WriteLine("\n Assignment Operators ");
AssignmentOperators();
Console.ReadKey();
}
}
OUTPUT: -
3. Write A C# Program To Demonstrate String Function.

INPUT: -
using System;
class Program
{
static void Main()
{
string message = " Hello CSharp World! ";
Console.WriteLine("Length: " + message.Length);
Console.WriteLine("Uppercase: " + message.ToUpper());
Console.WriteLine("Lowercase: " + message.ToLower());
Console.WriteLine("Substring(2, 5): " + message.Substring(2, 5));
Console.WriteLine("Replace 'CSharp' with 'C#': " + message.Replace("CSharp", "C#"));
Console.WriteLine("Contains 'World': " + message.Contains("World"));
Console.WriteLine("Index of 'C': " + message.IndexOf('C'));
string first = "Hello";
string second = "World";
string result = string.Concat(first, " ", second);
Console.WriteLine("Concatenated String: " + result);

Console.ReadKey();
}
}

OUTPUT: -
4. Write A C# Program To Demonstrate Operations On An Arraylist.

INPUT: -
using System;
using System.Collections;
class ArrayListDemo
{
static void Main()
{
ArrayList cities = new ArrayList();
cities.Add("Mysore");
cities.Add("Banglore");
cities.Add("Mandya");
Console.WriteLine("Initial ArrayList:");
Display(cities);
cities.Add("Udupi");
Console.WriteLine("\nAfter Inserting/Adding 'Udupi' at index 1:");
Display(cities);
cities.Remove("Mandya");
Console.WriteLine("\nAfter removing 'Mandya':");
Display(cities);
cities.Sort();
Console.WriteLine("\nAfter sorting:");
Display(cities);
Console.ReadKey();
}
static void Display(ArrayList list)
{
foreach (var item in list)
{
Console.WriteLine(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;
Console.WriteLine("Default Constructor Called");
}
// Parameterized Constructor
public Student(string studentName, int studentAge)
{
name = studentName;
age = studentAge;
Console.WriteLine("Parameterized Constructor Called");
}
// Method to display student details
public void Display()
{
Console.WriteLine("Name: " + name);
Console.WriteLine("Age: " + age);
}
}
class Program
{
static void Main()
{
// Using default constructor
Student student1 = new Student();
student1.Display();

// Using parameterized constructor


Student student2 = new Student("Ram", 20);
student2.Display();
Console.ReadKey();
}
}
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()
{
Console.WriteLine("Name: " + Name);
Console.WriteLine("Age: " + Age);
}
}
class Employee : Person
{
public int EmployeeId;
public string Department;
public void DisplayEmployeeInfo()
{
Console.WriteLine("Employee ID: " + EmployeeId);
Console.WriteLine("Department: " + Department);
}
}
class Manager : Employee
{
public string Role;
public void DisplayManagerInfo()
{
Console.WriteLine("Role: " + Role);
}
}
class Program
{
static void Main(string[] args)
{
Manager mgr = new Manager();
mgr.Name = "John";
mgr.Age = 35;
mgr.EmployeeId = 1001;
mgr.Department = "HR";
mgr.Role = "HR Manager";
Console.WriteLine("Manager Details:");
mgr.DisplayPersonInfo();
mgr.DisplayEmployeeInfo();
mgr.DisplayManagerInfo();
Console.ReadKey();
}
}
}

OUTPUT: -
7. Write C# Program To Demonstrate Method Overloading.

INPUT: -

using System;
namespace ConsoleApplication7
{
class Calulator
{
public void show(int a, int b)
{
Console.WriteLine("Sum of integers:" + (a + b));
}
public void show(double a, double b)
{
Console.WriteLine("Sum of doubles:" + (a + b));
}
}
class Program
{
static void Main(string[] args)
{
Calulator cal = new Calulator();
cal.show(5, 10);
cal.show(3.5, 2.5);
Console.ReadKey();
}
}
}

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(a.real + b.real, a.imag + b.imag);
}
// Display method
public void Show()
{
Console.WriteLine(real + " + " + imag + "i");
}
}
class Program
{
static void Main()
{
Complex c1 = new Complex(1, 2);
Complex c2 = new Complex(3, 4);
Complex c3 = c1 + c2;
Console.WriteLine("Result:");
c3.Show();
Console.ReadKey();
}
}
OUTPUT: -

You might also like