C# Lab Programs 2025-26
INDEX
SL. NO PAGE NO. SIGNATURE/REMARKS
NAME OF THE
PROGRAMS
Write a C# Programs to
1. show the machine details 3-4
like machine name, OS,
Version since last boot up.
2. WAP to count total 5-6
number of alphabets,
digits, and special
characters in string.
WAP to Demonstrate
3. operator overloading to 7-8
add two complex
numbers.
WAP to implement
4. multilevel inheritance. 9-10
WAP to demonstrate
5. ATM software using 11-13
switch statement.
WAP to demonstrate
6. System exception and 14-15
application exception.
WAP that inputs the
7.
coordinates of three
mouse clicks from the
user and then draws a 16-18
triangle in the output
window using those three
points.
Create a currency
8. converter application. 19
Write an application
9. to simulate the
working of font 20-22
dialog box using list
boxes, labels and
controls.
SVS COLLEGE, ILKAL Page 1
C# Lab Programs 2025-26
Write an application to
10. simulate the working of 23-26
font dialog box using list
boxes, labels and buttons
controls.
Create GUI application
11 to insert the data into the
database having fields
such as Roll no, name, 27-31
age, and contact using
Execute-Non-Query.
SVS COLLEGE, ILKAL Page 2
C# Lab Programs 2025-26
1. Write a C# program to show the machine details like machine name, Operating System,
Version, Physical Memory and calulate the time since the Last Boot Up.(Hint: Use
[Link] Class).
using System;
using [Link];
using [Link];
using [Link];
using [Link];
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
[Link]("Machine Name : " + [Link]);
[Link]("Operating System : " + [Link]);
[Link]("OS Version : " + [Link]);
TimeSpan uptime = [Link]([Link]);
[Link]("Uptime : " + [Link] + " days " +
[Link] + " hrs " +
[Link] + " mins " +
[Link] + " secs");
[Link]();
}
SVS COLLEGE, ILKAL Page 3
C# Lab Programs 2025-26
}
}
OUTPUT:
Machine Name : DESKTOP-0DK6G6M
Operating System : Win32NT
OS Version : 6.2.9200.0
Uptime : 15 days 19 hrs 27 mins 58 secs
SVS COLLEGE, ILKAL Page 4
C# Lab Programs 2025-26
2. Write a program in C# to count a total number of alphabets, digits and special chatrecters in a
string.
using System;
namespace CountCharacters
{
class Program
{
static void Main(string[] args)
{
[Link]("Enter any string:");
string str = [Link]();
int alphabets = 0, digits = 0, special = 0;
foreach (char c in str)
{
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
alphabets++;
else if (c >= '0' && c <= '9')
digits++;
else
special++;
}
[Link]("\nTotal Alphabets: " + alphabets);
[Link]("Total Digits: " + digits);
[Link]("Total Special Characters: " + special);
SVS COLLEGE, ILKAL Page 5
C# Lab Programs 2025-26
}
}
OUTPUT:
Enter any string:
Hello@123
Total Alphabets: 5
Total Digits: 3
Total Special Characters: 1
SVS COLLEGE, ILKAL Page 6
C# Lab Programs 2025-26
3. Demontrate operator overloading to addtwo complex numbers.
using System;
using [Link];
using [Link];
using [Link];
using [Link];
namespace ConsoleApp6
{
internal class Program
{
static void Main(string[] args)
{
Complex c1 = new Complex(3, 4);
Complex c2 = new Complex(5, 2);
Complex sum = c1 + c2;
[Link]("First Complex Number : ");
[Link]();
[Link]("Second Complex Number: ");
[Link]();
[Link]("Sum : ");
[Link]();
[Link]();
}
SVS COLLEGE, ILKAL Page 7
C# Lab Programs 2025-26
class Complex
{
public int Real, Imag;
public Complex(int r, int i)
{
Real = r;
Imag = i;
}
public static Complex operator +(Complex a, Complex b)
{
return new Complex([Link] + [Link], [Link] + [Link]);
}
public void Display()
{
[Link](Real + " + " + Imag + "i");
}
}
}
OUTPUT:
First Complex Number : 3 + 4i
Second Complex Number: 5 + 2i
Sum : 8 + 6i
SVS COLLEGE, ILKAL Page 8
C# Lab Programs 2025-26
4. Write a program to implement multilevel inheritance.
using System;
namespace ConsoleApplication3
{
class grand_father
{
public int id = 11;
string name;
public void method1()
{
[Link]("Inside grandfather's method1");
}
}
class parent : grand_father
{
public void method2()
{
[Link](id);
[Link]("Inside parent's method2");
}
public void method3()
{
[Link]("Inside parent's method3");
}
}
class child : parent
SVS COLLEGE, ILKAL Page 9
C# Lab Programs 2025-26
{
public void method4()
{
[Link]("Inside child's method4");
}
}
class Program
{
static void Main(string[] args)
{
child c = new child();
c.method1();
c.method2();
c.method3();
c.method4();
[Link]();
}
}
}
OUTPUT:
Inside grandfather's method1
11
Inside parent's method2
Inside parent's method3
Inside child's method4
SVS COLLEGE, ILKAL Page 10
C# Lab Programs 2025-26
5. Write a C# program to demonstrate ATM software using switch statement.
using System;
using [Link];
using [Link];
using [Link];
using [Link];
namespace ConsoleApp3
{
internal class Program
{
static void Main(string[] args)
{
int balance = 1000;
[Link]("=== ATM Menu ===");
[Link]("1. Check Balance");
[Link]("2. Deposit");
[Link]("3. Withdraw");
[Link]("Enter your choice: ");
int choice = Convert.ToInt32([Link]());
switch (choice)
{
case 1:
[Link]("Your balance is: " + balance);
break;
SVS COLLEGE, ILKAL Page 11
C# Lab Programs 2025-26
case 2:
[Link]("Enter deposit amount: ");
balance += Convert.ToInt32([Link]());
[Link]("New balance: " + balance);
break;
case 3:
[Link]("Enter withdrawal amount: ");
int withdraw = Convert.ToInt32([Link]());
if (withdraw <= balance)
{
balance -= withdraw;
[Link]("Please collect cash. New balance: " + balance);
}
else
{
[Link]("Insufficient balance!");
}
break;
default:
[Link]("Invalid choice!");
break;
}
[Link]();
}
}
}
SVS COLLEGE, ILKAL Page 12
C# Lab Programs 2025-26
OUTPUT:
=== ATM Menu ===
1. Check Balance
2. Deposit
3. Withdraw
Enter your choice: 1
Your balance is: 1000
=== ATM Menu ===
1. Check Balance
2. Deposit
3. Withdraw
Enter your choice: 2
Enter deposit amount: 100
New balance: 1100
=== ATM Menu ===
1. Check Balance
2. Deposit
3. Withdraw
Enter your choice: 3
Enter withdrawal amount: 500
Please collect cash. New balance: 500
SVS COLLEGE, ILKAL Page 13
C# Lab Programs 2025-26
6. Write a program to demonstrate System exception and Application exception.
using System;
using [Link];
using [Link];
using [Link];
using [Link];
namespace ConsoleApp4
{
internal class Program
{
static void Main(string[] args)
{
try
{
int a = 10, b = 0;
int result = a / b;
[Link]("Result: " + result);
}
catch (DivideByZeroException ex)
{
[Link]("System Exception caught: " + [Link]);
}
try
{
[Link]("Enter age: ");
int age = Convert.ToInt32([Link]());
SVS COLLEGE, ILKAL Page 14
C# Lab Programs 2025-26
if (age < 18)
{
throw new ApplicationException("Age must be 18 or above.");
}
else
{
[Link]("Age accepted: " + age);
}
}
catch (ApplicationException ex)
{
[Link]("Application Exception caught: " + [Link]);
[Link](); } } }
OUTPUT:
System Exception caught: Attempted to divide by zero.
Enter age: 15
Application Exception caught: Age must be 18 or above.
System Exception caught: Attempted to divide by zero.
Enter age: 25
Age accepted: 25
SVS COLLEGE, ILKAL Page 15
C# Lab Programs 2025-26
7. Write a program that inputs the coordinates of three mouse click from the user and then
draws a triangle in the output window using those three points.
using System;
using [Link];
using [Link];
using [Link];
using [Link];
namespace ConsoleApp7
{
internal class Program
{
static void Main(string[] args)
{
int[] x = new int[3];
int[] y = new int[3];
[Link]("Enter coordinates of 3 points (0-20):");
for (int i = 0; i < 3; i++)
{
[Link]("Point " + (i + 1) + " X: ");
x[i] = Convert.ToInt32([Link]());
[Link]("Point " + (i + 1) + " Y: ");
y[i] = Convert.ToInt32([Link]());
}
char[,] canvas = new char[21, 21]; // 21x21 grid
for (int i = 0; i < 21; i++)
for (int j = 0; j < 21; j++)
SVS COLLEGE, ILKAL Page 16
C# Lab Programs 2025-26
canvas[i, j] = ' ';
for (int i = 0; i < 3; i++)
canvas[y[i], x[i]] = '*';
for (int i = 0; i < 3; i++)
{
int next = (i + 1) % 3;
int dx = x[next] - x[i];
int dy = y[next] - y[i];
int steps = [Link]([Link](dx), [Link](dy));
for (int s = 1; s < steps; s++)
{
int px = x[i] + dx * s / steps;
int py = y[i] + dy * s / steps;
canvas[py, px] = '*';
}
}
for (int i = 20; i >= 0; i--)
{
for (int j = 0; j < 21; j++)
{
[Link](canvas[i, j]);
}
[Link]();
}
[Link]();
}
}
}
SVS COLLEGE, ILKAL Page 17
C# Lab Programs 2025-26
OUTPUT:
Enter coordinates of 3 points (0-20):
Point 1 X: 2
Point 1 Y: 2
Point 2 X: 15
Point 2 Y: 2
Point 3 X: 8
Point 3 Y: 12
*
**
* *
* *
* *
* *
* *
* *
* *
* *
**************
SVS COLLEGE, ILKAL Page 18
C# Lab Programs 2025-26
8. Create currency converter application.
using System;
namespace CurrencyConverter
{
class Program
{
static void Main()
{
double rate = 83.5;
[Link]("Enter amount in USD: ");
double usd = [Link]([Link]());
double inr = usd * rate
[Link]($"{usd} USD = {inr} INR");
[Link]("\n--- Conversion Done ---");
}
}
}
OUTPUT:
Enter amount in USD: 14
14 USD = 1169 INR
--- Conversion Done ---
SVS COLLEGE, ILKAL Page 19
C# Lab Programs 2025-26
9. Write Create an application to simulate the working of Font Dialog box using list boxes, label
and controls.
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
[Link] = "Username:";
[Link] = "Password:";
}
private void button_Click(object sender, EventArgs e)
{
SVS COLLEGE, ILKAL Page 20
C# Lab Programs 2025-26
if ([Link] == "admin" && [Link] == "1234")
{
[Link]("Login Successful!", "Success");
}
else
{
[Link]("Invalid Username or Password", "Error");
}
}
private void button1_Click(object sender, EventArgs e)
{
[Link]("Login Successful!", "Success");
private void button1_Click_1(object sender, EventArgs e)
{
[Link]("Login Successful!", "Success");
}
}
}
SVS COLLEGE, ILKAL Page 21
C# Lab Programs 2025-26
OUTPUT:
SVS COLLEGE, ILKAL Page 22
C# Lab Programs 2025-26
10. Create an application to simulate the working of font dialog box using list boxes, label and
button controls.
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
namespace WindowsFormsApp4
{
public partial class Form1 : Form
{
Label labelSample;
ListBox listBoxFont, listBoxSize;
Button buttonApply;
public Form1()
{
InitializeComponent();
[Link] = "Font Dialog Simulation";
[Link] = 400;
[Link] = 300;
labelSample = new Label();
[Link] = "Sample Text";
[Link] = new Point(120, 20);
SVS COLLEGE, ILKAL Page 23
C# Lab Programs 2025-26
[Link] = true;
[Link](labelSample);
listBoxFont = new ListBox();
[Link] = new Point(40, 60);
[Link] = new Size(150, 100);
foreach (FontFamily font in [Link])
{
[Link]([Link]);
}
[Link](listBoxFont);
listBoxSize = new ListBox();
[Link] = new Point(210, 60);
[Link] = new Size(60, 100);
for (int i = 8; i <= 24; i += 2)
{
[Link](i);
}
[Link](listBoxSize);
buttonApply = new Button();
[Link] = "Apply Font";
[Link] = new Point(130, 200);
[Link] += ButtonApply_Click;
[Link](buttonApply);
}
private void ButtonApply_Click(object sender, EventArgs e)
{
if ([Link] != null && [Link] != null)
{
SVS COLLEGE, ILKAL Page 24
C# Lab Programs 2025-26
string fontName = [Link]();
float fontSize = [Link]([Link]());
[Link] = new Font(fontName, fontSize);
}
else
{
[Link]("Please select both Font and Size!");
}
}
private void button1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
SVS COLLEGE, ILKAL Page 25
C# Lab Programs 2025-26
OUTPUT:
SVS COLLEGE, ILKAL Page 26
C# Lab Programs 2025-26
11. Create GUI application to insert the data into the database having feilds such as Roll no,
Name, Age, and Contact using Execute-Non-Query.
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
namespace WindowsFormsApp5
public partial class Form1 : Form
Label lblRoll, lblName, lblAge, lblContact;
TextBox txtRoll, txtName, txtAge, txtContact;
Button btnInsert, btnClear;
ListBox listBoxData;
public Form1()
InitializeComponent();
CreateControls();
private void Form1_Load(object sender, EventArgs e)
SVS COLLEGE, ILKAL Page 27
C# Lab Programs 2025-26
private void CreateControls()
[Link] = "Student Data Entry (Manual Insert)";
[Link] = new Size(500, 500);
[Link] = [Link];
lblRoll = new Label();
[Link] = "Roll No:";
[Link] = new Point(40, 40);
[Link] = true;
[Link](lblRoll);
txtRoll = new TextBox();
[Link] = new Point(150, 40);
[Link] = 200;
[Link](txtRoll);
lblName = new Label();
[Link] = "Name:";
[Link] = new Point(40, 80);
[Link] = true;
[Link](lblName);
txtName = new TextBox();
[Link] = new Point(150, 80);
[Link] = 200;
[Link](txtName);
lblAge = new Label();
[Link] = "Age:";
SVS COLLEGE, ILKAL Page 28
C# Lab Programs 2025-26
[Link] = new Point(40, 120);
[Link] = true;
[Link](lblAge);
txtAge = new TextBox();
[Link] = new Point(150, 120);
[Link] = 200;
[Link](txtAge);
lblContact = new Label();
[Link] = "Contact:";
[Link] = new Point(40, 160);
[Link] = true;
[Link](lblContact);
txtContact = new TextBox();
[Link] = new Point(150, 160);
[Link] = 200;
[Link](txtContact);
btnInsert = new Button();
[Link] = "Insert";
[Link] = new Point(150, 210);
[Link] = 80;
[Link] += BtnInsert_Click;
[Link](btnInsert);
btnClear = new Button();
[Link] = "Clear";
[Link] = new Point(250, 210);
[Link] = 80;
SVS COLLEGE, ILKAL Page 29
C# Lab Programs 2025-26
[Link] += (s, e) => ClearFields();
[Link](btnClear);
listBoxData = new ListBox();
[Link] = new Point(40, 270);
[Link] = 380;
[Link] = 150;
[Link](listBoxData);
private void BtnInsert_Click(object sender, EventArgs e)
if ([Link]() == "" || [Link]() == "" ||
[Link]() == "" || [Link]() == "")
[Link]("Please fill all fields.", "Error", [Link],
[Link]);
return;
string record = "Roll No: " + [Link] + " | Name: " + [Link] + " | Age: " + [Link] +
" | Contact: " + [Link];
[Link](record);
[Link]("Data inserted successfully!", "Success", [Link],
[Link]);
ClearFields();
private void ClearFields()
[Link]();
[Link]();
[Link]();
[Link]();
SVS COLLEGE, ILKAL Page 30
C# Lab Programs 2025-26
[Link]();
OUTPUT:
SVS COLLEGE, ILKAL Page 31