C# practical file
C# practical file
3. Write a C sharp program to accept an array of integers (10) and sort them in
ascending order.
6. Write a C sharp program for jagged array and display its item through
foreach loop.
7. Write a program to demonstrate boxing and unboxing.
16. Create a front end interface in windows that enables a user to accept the
details of an employee like EmpId ,First Name, Last Name, Gender, Contact
No, Designation, Address and Pin. Create a database that stores all these
details in a table. Also, the front end must have a provision to Add, Update
and Delete a record of an employee.
17. Create a database named MyDb (SQL or MS Access).Connect the database
with your window application to display the data in List boxes using Data
Reader.
18. Display the data from the table in a DataGridView control using dataset.
19. Create a registration form in ASP.NET and use different types of validation
controls.
20. Display the data from the table in a Repeater control using dataset in
ASP.net.
C# Practical
Q1. Write a C sharp program to generate prime numbers between 1 to200 and also print to the
console.
Ans.
using System
class PrimeNumbers
{
static void Main()
{
Console.WriteLine("Prime numbers between 1 and 200 are:\n");
OUTPUT:-
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199
Q2. Write a program to print ARMSTRONG number.
Ans.
using System;
class ArmstrongNumbers
{
static void Main()
{
Console.WriteLine("Armstrong numbers between 1 and 1000 are:\n");
OUTPUT:-
Ans.
using System;
class SortArray
Console.WriteLine("Enter 10 integers:");
numbers[i] = Convert.ToInt32(Console.ReadLine());
Array.Sort(numbers);
OUTPUT:-
Enter 10 integers:
Enter number 1: 45
Enter number 2: 12
Enter number 3: 89
Enter number 4: 7
Enter number 5: 56
Enter number 6: 34
Enter number 7: 23
Enter number 8: 78
Enter number 9: 10
5 7 10 12 23 34 45 56 78 89
Q4. Write a program to implement the concept of abstract class.
Ans.
using System;
// Abstract class
abstract class Shape
{
// Abstract method (no implementation here)
public abstract double Area();
// Non-abstract method
public void Display()
{
Console.WriteLine("Calculating area of the shape:");
}
}
// Derived class 1
class Circle : Shape
{
public double Radius { get; set; }
OUTPUT:-
Area of Rectangle: 24
Q5. Write a program to implement the concept of sealed class.
Ans.
using System;
// Base class
class Vehicle
{
public void ShowType()
{
Console.WriteLine("This is a vehicle.");
}
}
// Sealed class - cannot be inherited
sealed class Car : Vehicle
{
public void ShowModel()
{
Console.WriteLine("This is a car - Model: Honda City.");
}
}
// The following code will cause a compile-time error:
// class SportsCar : Car
// {
// // Error: cannot derive from sealed type 'Car'
// }
class Program
{
static void Main()
{
Car myCar = new Car();
myCar.ShowType(); // Method from base class
myCar.ShowModel(); // Method from sealed class
}
}
OUTPUT:-
This is a vehicle.
Ans.
using System;
class JaggedArrayExample
int row = 0;
Console.WriteLine();
row++;
}}}
OUTPUT:-
Row 0: 1 2 3
Row 1: 4 5
Row 2: 6 7 8 9
Q7. Write a program to demonstrate boxing and unboxing.
Ans.
using System;
class BoxingUnboxingDemo
{
static void Main()
{
int num = 100; // Value type
object obj = num; // Boxing: Converting value type to object type
Console.WriteLine("Boxing:");
Console.WriteLine("Value type 'int' num = " + num);
Console.WriteLine("Boxed into object obj = " + obj);
int unboxedNum = (int)obj; // Unboxing: Converting object back to value type
Console.WriteLine("\nUnboxing:");
Console.WriteLine("Unboxed object obj back to int = " + unboxedNum);
}
}
OUTPUT:-
Boxing:
Value type 'int' num = 100
Boxed into object obj = 100
Unboxing:
Unboxed object obj back to int = 100
Q8. Write a program to find number of digit, character, and punctuation in entered string.
Ans.
using System;
class StringAnalysis
int digitCount = 0;
int letterCount = 0;
int punctuationCount = 0;
if (char.IsDigit(ch))
digitCount++;
else if (char.IsLetter(ch))
letterCount++;
else if (char.IsPunctuation(ch))
punctuationCount++;
OUTPUT:-
Enter a string: Hello123! How are you doing today? I'm fine :)
Number of digits: 3
Number of letters: 31
Ans.
using System;
class ExceptionHandlingDemo
try
int result = num1 / num2; // Division operation which may cause exception (divide by zero)
}
finally
Console.WriteLine("Execution completed.");
OUTPUT 1:-
Enter a number: 10
Result of division: 5
Execution completed.
OUTPUT 2:-
Enter a number: 10
Execution completed.
OUTPUT 3:-
Execution completed.
Q10. Write a program to implement multiple inheritances using interface.
Ans.
using System;
// Interface 1
interface IShape
void Draw();
// Interface 2
interface IColor
void FillColor();
class Program
{
circle.Draw();
circle.FillColor();
OUTPUT:-
Ans.
using System;
class ArithmeticOperations
if (num2 == 0)
return 0;
}
return num1 / num2;
Console.WriteLine("\nArithmetic Operations:");
OUTPUT:-
Arithmetic Operations:
Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2
Q12. Write a program to implement Indexer.
Ans.
using System;
class Student
get
return names[index];
else
set
{
names[index] = value;
else
Console.WriteLine("Invalid index");
class Program
studentList[0] = "John";
studentList[1] = "Alice";
studentList[2] = "Bob";
}
OUTPUT:-
Ans.
using System;
class Calculator
double n1 = Convert.ToDouble(num1);
double n2 = Convert.ToDouble(num2);
return n1 + n2;
}
}
class Program
OUTPUT:-
Ans.
using System;
// Base class
class Animal
// Derived class
Console.WriteLine("Dog barks");
Console.WriteLine("Cat meows");
}
class Program
OUTPUT:-
Dog barks
Cat meows
Q15. Write a program in C sharp to create a calculator in windows form.
Ans.
using System;
using System.Windows.Forms;
namespace WindowsFormsCalculator
public CalculatorForm()
InitializeComponent();
textBox_Display.Clear();
isOperatorClicked = false;
textBox_Display.Text += button.Text;
operatorUsed = button.Text;
resultValue = double.Parse(textBox_Display.Text);
isOperatorClicked = true;
switch (operatorUsed)
case "+":
break;
case "-":
break;
case "*":
break;
case "/":
if (double.Parse(textBox_Display.Text) == 0)
textBox_Display.Text = "Error";
else
}
break;
default:
break;
textBox_Display.Clear();
resultValue = 0;
operatorUsed = "";
this.SuspendLayout();
//
// textBox_Display
//
this.textBox_Display.Name = "textBox_Display";
this.textBox_Display.TabIndex = 0;
this.textBox_Display.Text = "0";
this.textBox_Display.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
//
//
this.button_1.Text = "1";
this.button_2.Text = "2";
//
//
this.button_Subtract.Text = "-";
//
//
this.button_Equals.Text = "=";
//
//
this.button_Clear.Text = "C";
//
// CalculatorForm
//
this.Controls.Add(this.textBox_Display);
this.Controls.Add(this.button_1);
this.Controls.Add(this.button_2);
this.Controls.Add(this.button_3);
this.Controls.Add(this.button_4);
this.Controls.Add(this.button_5);
this.Controls.Add(this.button_6);
this.Controls.Add(this.button_7);
this.Controls.Add(this.button_8);
this.Controls.Add(this.button_9);
this.Controls.Add(this.button_0);
this.Controls.Add(this.button_Add);
this.Controls.Add(this.button_Subtract);
this.Controls.Add(this.button_Multiply);
this.Controls.Add(this.button_Divide);
this.Controls.Add(this.button_Equals);
this.Controls.Add(this.button_Clear);
this.Name = "CalculatorForm";
this.Text = "Calculator";
this.ResumeLayout(false);
this.PerformLayout();
OUTPUT:-
When you run the application, you will see a simple calculator UI with buttons for digits,
operators, and result.
Q16. Create a front end interface in windows that enables a user to accept the details of an
employee like EmpId ,First Name, Last Name, Gender, Contact No, Designation, Address and
Pin. Create a database that stores all these details in a table. Also, the front end must have a
provision to Add, Update and Delete a record of an employee.
Ans.
USE EmployeeDB;
FirstName NVARCHAR(50),
LastName NVARCHAR(50),
Gender NVARCHAR(10),
ContactNo NVARCHAR(15),
Designation NVARCHAR(50),
Address NVARCHAR(100),
Pin NVARCHAR(10)
);
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace EmployeeFormApp
{
public partial class Form1 : Form
public Form1()
InitializeComponent();
LoadData();
sda.Fill(dt);
dataGridView1.DataSource = dt;
cmd.Parameters.AddWithValue("@EmpId", txtEmpId.Text);
cmd.Parameters.AddWithValue("@FirstName", txtFirstName.Text);
cmd.Parameters.AddWithValue("@LastName", txtLastName.Text);
cmd.Parameters.AddWithValue("@Gender", cmbGender.Text);
cmd.Parameters.AddWithValue("@ContactNo", txtContact.Text);
cmd.Parameters.AddWithValue("@Designation", txtDesignation.Text);
cmd.Parameters.AddWithValue("@Address", txtAddress.Text);
cmd.Parameters.AddWithValue("@Pin", txtPin.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
LoadData();
ClearFields();
FirstName = @FirstName,
LastName = @LastName,
Gender = @Gender,
ContactNo = @ContactNo,
Designation = @Designation,
Address = @Address,
Pin = @Pin
cmd.Parameters.AddWithValue("@EmpId", txtEmpId.Text);
cmd.Parameters.AddWithValue("@FirstName", txtFirstName.Text);
cmd.Parameters.AddWithValue("@LastName", txtLastName.Text);
cmd.Parameters.AddWithValue("@Gender", cmbGender.Text);
cmd.Parameters.AddWithValue("@ContactNo", txtContact.Text);
cmd.Parameters.AddWithValue("@Designation", txtDesignation.Text);
cmd.Parameters.AddWithValue("@Address", txtAddress.Text);
cmd.Parameters.AddWithValue("@Pin", txtPin.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
LoadData();
ClearFields();
cmd.Parameters.AddWithValue("@EmpId", txtEmpId.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
LoadData();
ClearFields();
{
txtEmpId.Clear();
txtFirstName.Clear();
txtLastName.Clear();
txtContact.Clear();
txtDesignation.Clear();
txtAddress.Clear();
txtPin.Clear();
cmbGender.SelectedIndex = -1;
if (e.RowIndex >= 0)
txtEmpId.Text = row.Cells[0].Value.ToString();
txtFirstName.Text = row.Cells[1].Value.ToString();
txtLastName.Text = row.Cells[2].Value.ToString();
cmbGender.Text = row.Cells[3].Value.ToString();
txtContact.Text = row.Cells[4].Value.ToString();
txtDesignation.Text = row.Cells[5].Value.ToString();
txtAddress.Text = row.Cells[6].Value.ToString();
txtPin.Text = row.Cells[7].Value.ToString();
OUTPUT:-
Ans.
USE MyDb;
FirstName NVARCHAR(50),
Designation NVARCHAR(50)
);
-- Sample data
using System.Data.SqlClient;
using System.Windows.Forms;
namespace MyDbReaderApp
public Form1()
InitializeComponent();
listBox1.Items.Clear();
listBox2.Items.Clear();
listBox3.Items.Clear();
con.Open();
while (reader.Read())
listBox1.Items.Add(reader["EmpId"].ToString());
listBox2.Items.Add(reader["FirstName"].ToString());
listBox3.Items.Add(reader["Designation"].ToString());
reader.Close();
con.Close();
OUTPUT:-
ListBox1 (EmpId):-
1
ListBox2 (FirstName):-
Kush
Love
Ankit
ListBox3 (Designation):-
Developer
Designer
Tester
Q18. Display the data from the table in a DataGridView control using dataset.
Ans.
USE MyDb;
FirstName NVARCHAR(50),
LastName NVARCHAR(50),
Designation NVARCHAR(50)
);
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
Step 4: Full Code for Form1.cs:-
namespace MyDbGridViewApp
public Form1()
InitializeComponent();
adapter.Fill(ds, "Employees");
dataGridView1.DataSource = ds.Tables["Employees"];
}
Output (after clicking Load Data):-
EmpId FirstName LastName Designation
Ans.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<style>
</style>
</head>
<body>
<h2>Registration Form</h2>
<div class="form-group">
ControlToValidate="txtName"
</div>
<div class="form-group">
ControlToValidate="txtEmail"
ControlToValidate="txtEmail"
ValidationExpression="^[^@\s]+@[^@\s]+\.[^@\s]+$"
</div>
<div class="form-group">
ControlToValidate="txtPassword"
</div>
<div class="form-group">
ControlToCompare="txtPassword"
ControlToValidate="txtConfirmPassword"
</div>
<div class="form-group">
ControlToValidate="txtAge"
MinimumValue="18"
MaximumValue="60"
Type="Integer"
</div>
<div class="form-group">
</div>
</div>
</form>
</body>
</html>
}
protected void btnRegister_Click(object sender, EventArgs e)
if (Page.IsValid)
OUTPUT:-
Registration successful!
Q20. Display the data from the table in a Repeater control using dataset in ASP.net.
Ans.
USE MyDb;
Name NVARCHAR(50),
Course NVARCHAR(50)
);
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<style>
.student-box {
padding: 10px;
margin: 8px;
background-color: #f5f5f5;
width: 300px;
</style>
</head>
<body>
<h2>Students List</h2>
<ItemTemplate>
<div class="student-box">
</div>
</ItemTemplate>
</asp:Repeater>
</form>
</body>
</html>
using System.Data;
using System.Data.SqlClient;
if (!IsPostBack)
BindRepeater();
adapter.Fill(ds, "Students");
Repeater1.DataSource = ds.Tables["Students"];
Repeater1.DataBind();
OUTPUT:-
Students List
[Student 1 Box]
ID: 1
Course: MCA
[Student 2 Box]
ID: 2
[Student 3 Box]
ID: 3
Course: BCA