C# Lab File
C# Lab File
Practical File
BCA-P51
Python Programming
Course :- BCA
Semester :- 5th
(School of CA & IT)
Submitted to:-
Submitted by:-
Mr. Gagandeep Makkar
Shristi Goyal
Designation-Associate
Enroll no:-R210529061
Professor
INDEX
Page
S.No. Assignment Name / Practical Name
No.
Write a Python program to count the number of characters in
1. 1
a string. Sample String : ‘google.com’
Write a python program to get a single string from two given
2. strings, separated by a space and swap the first two characters 2
of each string.
Write a python program to add ‘ing’ at the end of a given
3. 3
string
Write a python program to find the first appearance of the
substring ‘not’ and ‘poor’ from a given string, if ‘not’ follows
4. 4
the ‘poor’, replace the whole ‘not’ …’poor’ substring with
‘good’ . return the resulting string.
Write a python program to create a Caesar a encryption. It is a
type of substitution cipher in which each letter in the plaintext
is replaced by a letter some fixed number of position down the
5. alphabet .for example with a left shift of 3,D would be 5
replaced by A,E would become B, and so on. The method is
named after Julius Caesar ,who used it in his private
correspondence .
Write a python that accepts a comma separated sequence of
6. words as input and prints the unique words in sorted from 6
(alphanumerically).
Write a python function to covert a given strings to all
7. uppercase if it contains at least 2 uppercase characters in the 7
first 4 characters.
31. Write a Python function that accepts a string and calculate the 31
number of upper case letters and lower case letters.
using System;
class Program
{
static bool IsPrime(int number)
{
if (number <= 1)
{
return false;
}
return true;
}
// Print comma after the prime number if it's not the last one
if (i != 200)
{
Console.Write(", ");
}
}
}
}
}
Output
Prime numbers between 1 and 200:
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 using System.
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Armstrong numbers between 1 and 1000:");
for (int num = 1; num <= 1000; num++)
{
int originalNumber, remainder, result = 0;
originalNumber = num;
while (originalNumber != 0)
{
remainder = originalNumber % 10;
result += (int)Math.Pow(remainder, 3); // 3 is the number of digits for Armstrong
number
originalNumber /= 10;
}
if (result == num)
{
Console.WriteLine(num);
}
}
}
}
Output
Armstrong numbers between 1 and 1000:
1
2
3
4
5
6
7
8
9
153
370
371
407
Q3. Write a C sharp program using loop that examines all the numbers
between 2 and 1000, and displays only Perfect number (A perfect number is
the one whose sum of their divisors equals the number itself). For example
given the number 6, the sum of its divisors is 6(1+2+3).Hence, 6 is a perfect
number.
using System;
class Program
{
static void Main()
{
Console.WriteLine("Perfect numbers between 2 and 1000:");
for (int number = 2; number <= 1000; number++)
{
if (IsPerfectNumber(number))
{
Console.WriteLine(number);
}
}
}
// If 'i' is not the square root of the number, add the corresponding divisor
if (i != number / i)
{
sum += number / i;
}
}
}
// If the sum of divisors equals the number itself, it's a perfect number
return sum == number;
}
}
Output
Perfect numbers between 2 and 1000:
6
28
496
Q4. Write a C sharp program to accept an array of integers (10) and sort them
in ascending order.
using System;
class Program
{
static void Main()
{
int[] numbers = new int[10];
Q5. Write a program to implement the concept of abstract class using System.
Output
Area of the circle: 78.53981633974483
// This line will result in a compilation error since you cannot inherit from a sealed
class
// class DerivedClass : SealedClass { }
class Program
{
static void Main()
{
// Creating an instance of the sealed class
SealedClass sealedObj = new SealedClass();
sealedObj.Display();
}
}
Output
This is a sealed class.
Q7. Write a C sharp program for jagged array and display its item through
foreach loop.
class Program
{
static void Main()
{
// Jagged array declaration and initialization
int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[] { 1, 2, 3 };
jaggedArray[1] = new int[] { 4, 5 };
jaggedArray[2] = new int[] { 6, 7, 8, 9 };
Output
Jagged Array Elements:
123
45
6789
Q8. Write a program in C Sharp using a class that gets the information about
Employee's such as Emp Id, First Name, Last Name, Basic Salary, Grade,
Address, Pin Code and Contact Number. Write a method that calculates the
Gross Salary (Basic +DA+HRA) and returns the calling program and another
method for the Net salary (Gross - (P.F + Income Tax)).Finally write a method
that prints, a pay slip of an employee, containing all the above components in
a proper format to the console.(Grade A = 20,000 , B=15,000 and C=10,000)
DA=56% and HRA=20%., Pf=780, ITax.
using System;
class Employee
{
// Properties
public int EmpId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public double BasicSalary { get; set; }
public char Grade { get; set; }
public string Address { get; set; }
public string PinCode { get; set; }
public string ContactNumber { get; set; }
// Methods
public double CalculateGrossSalary()
{
double DA = 0.56 * BasicSalary;
double HRA = 0.2 * BasicSalary;
return BasicSalary + DA + HRA;
}
class Program
{
static void Main()
{
// Creating an employee object
Employee employee = new Employee
{
EmpId = 1,
FirstName = "John",
LastName = "Doe",
BasicSalary = 15000,
Grade = 'B',
Address = "123 Main St",
PinCode = "12345",
ContactNumber = "9876543210"
};
Output
Pay Slip:
---------
Employee ID: 1
Name: John Doe
Address: 123 Main St, 12345
Contact Number: 9876543210
Grade: B
Basic Salary: 15000
Gross Salary: 30000
Net Salary: 28220
using System;
class Program
{
static void Main()
{
// Boxing: Converting value type to reference type
int intValue = 42;
object boxedValue = intValue; // Boxing
// Unboxing: Converting reference type back to value type
int unboxedValue = (int)boxedValue; // Unboxing
Output
Original Value: 42
Boxed Value: 42
Unboxed Value: 42
using System;
class Program
{
static void Main()
{
Console.Write("Enter a string: ");
string inputString = Console.ReadLine();
int digitCount = 0;
int charCount = 0;
int punctuationCount = 0;
foreach (char c in inputString)
{
if (char.IsDigit(c))
{
digitCount++;
}
else if (char.IsLetter(c))
{
charCount++;
}
else if (char.IsPunctuation(c) || char.IsSymbol(c))
{
punctuationCount++;
}
}
Output
Enter a string: Hello, World! 123
Number of Digits: 3
Number of Characters: 10
Number of Punctuation Marks: 2
using System;
class Program
{
static void Main()
{
try
{
// Code that may cause an exception
Console.Write("Enter a number: ");
int number = Convert.ToInt32(Console.ReadLine());
int result = 10 / number; // This line may cause a DivideByZeroException if the
number is 0
Console.WriteLine($"Result of 10 divided by {number} is: {result}");
}
catch (FormatException)
{
Console.WriteLine("Invalid input. Please enter a valid number.");
}
catch (DivideByZeroException)
{
Console.WriteLine("Cannot divide by zero. Please enter a non-zero number.");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
}
Output
Case 1: Invalid input (non-numeric):
Enter a number: ABC
Invalid input. Please enter a valid number.
Case 2: Dividing by zero:
Enter a number: 0
Cannot divide by zero. Please enter a non-zero number.
Case 3: Valid input (non-zero):
Enter a number: 2
Result of 10 divided by 2 is: 5
Q12. Write a program to implement multiple inheritances using interface.
using System;
// First interface
interface IShape
{
void Draw();
}
// Second interface
interface IColor
{
void FillColor();
}
class Program
{
static void Main()
{
// Creating an object of the class implementing multiple interfaces
Circle circle = new Circle();
using System;
class Program
{
static void Main()
{
// Creating delegate instances for different arithmetic operations
ArithmeticOperation add = (num1, num2) => num1 + num2;
ArithmeticOperation subtract = (num1, num2) => num1 - num2;
ArithmeticOperation multiply = (num1, num2) => num1 * num2;
ArithmeticOperation divide = (num1, num2) =>
{
if (num2 != 0)
{
return num1 / num2;
}
else
{
Console.WriteLine("Cannot divide by zero.");
return double.NaN;
}
};
// Taking input from the user
Console.Write("Enter first number: ");
double number1 = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter second number: ");
double number2 = Convert.ToDouble(Console.ReadLine());
First, create a class in one namespace called InputHandler to handle user input:
namespace InputNamespace
{
public class InputHandler
{
public static string GetUserName()
{
Console.Write("Enter your name: ");
return Console.ReadLine();
}
}
}
Second, create another class in a different namespace called OutputHandler to handle
output operations:
namespace OutputNamespace
{
public class OutputHandler
{
public static void PrintUserName(string name)
{
Console.WriteLine($"Hello, {name}!");
}
}
}
Finally, use these classes in your main program:
using System;
using InputNamespace;
using OutputNamespace;
class Program
{
static void Main()
{
string userName = InputHandler.GetUserName();
OutputHandler.PrintUserName(userName);
}
}
Output
Enter your name: John
Hello, John!
Q15. Write a program to implement Indexer.
using System;
class MyCollection
{
private string[] items = new string[5];
// Indexer declaration
public string this[int index]
{
get
{
if (index >= 0 && index < items.Length)
{
return items[index];
}
else
{
return "Index out of range";
}
}
set
{
if (index >= 0 && index < items.Length)
{
items[index] = value;
}
else
{
Console.WriteLine("Index out of range");
}
}
}
}
class Program
{
static void Main()
{
MyCollection collection = new MyCollection();
Q16. Write a program to design two interfaces that are having same name
methods how we can access these methods in another class.
using System;
// First interface
interface IInterface1
{
void CommonMethod();
}
// Second interface with the same method name as in the first interface
interface IInterface2
{
void CommonMethod();
}
class Program
{
static void Main()
{
// Creating an object of the class
MyClass myClass = new MyClass();
Output
Method from IInterface1
Method from IInterface2
Q17. Write a program to implement method overloading.
using System;
class Calculator
{
// Method to add two integers
public int Add(int num1, int num2)
{
return num1 + num2;
}
class Program
{
static void Main()
{
Calculator calculator = new Calculator();
Output
Sum of 2 and 3 is: 5
Sum of 2, 3, and 4 is: 9
Sum of 2.5 and 3.7 is: 6.2
Q18. Write a program to implement method overriding.
using System;
class Shape
{
public virtual void Draw()
{
Console.WriteLine("Drawing a shape");
}
}
class Program
{
static void Main()
{
Shape shape = new Shape();
shape.Draw(); // Calls base class method
Output
Drawing a shape
Drawing a circle
First, create a new Windows Forms Application project in Visual Studio and design the form
with buttons for digits, operators, and a textbox to display input and results. Then, add
event handlers to the buttons to handle button clicks and perform calculations. Here's an
example of the code for a basic calculator:
using System;
using System.Windows.Forms;
namespace CalculatorApp
{
public partial class CalculatorForm : Form
{
private string currentInput = string.Empty;
private string operatorSymbol = string.Empty;
private double firstNumber = 0;
public CalculatorForm()
{
InitializeComponent();
}
switch (operatorSymbol)
{
case "+":
result = firstNumber + secondNumber;
break;
case "-":
result = firstNumber - secondNumber;
break;
case "*":
result = firstNumber * secondNumber;
break;
case "/":
if (secondNumber != 0)
{
result = firstNumber / secondNumber;
}
else
{
MessageBox.Show("Cannot divide by zero.");
currentInput = string.Empty;
textBoxResult.Text = string.Empty;
return;
}
break;
}
currentInput = result.ToString();
textBoxResult.Text = currentInput;
}
Output
This code assumes that you have created a Windows Forms application with buttons
for digits (0-9), operators (+, -, *, /), an equal button (=), and a clear button (C). The
‘DigitButtonClick’, ‘OperatorButtonClick’, ‘EqualButtonClick’, and ‘ClearButtonClick’
methods handle button clicks and perform calculations accordingly.
Q20. 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.
TextBoxes for EmpId, First Name, Last Name, Contact No, Designation, Address, and
Pin.
RadioButton or ComboBox for Gender selection.
Buttons for Add, Update, and Delete operations.
2. Set Up the Database:
Create a SQL Server database with a table named Employees that has columns for
EmpId, FirstName, LastName, Gender, ContactNo, Designation, Address, and Pin. Set
EmpId as the primary key.
namespace EmployeeManagementApp
{
public class DatabaseHelper
{
private const string ConnectionString = "Data Source=YourServer;Initial
Catalog=YourDatabase;Integrated Security=True";
Output
4. Handle Button Click Events:
In the form's code-behind file, handle the button click events to call the appropriate
methods from the ‘DatabaseHelper’ class.
Create a SQL Server database named MyDb and a table named Employees with
columns EmpId, FirstName, LastName, Gender, ContactNo, Designation, Address, and
Pin.
Design your Windows Forms application with a ListBox control to display the data.
namespace EmployeeListApp
{
public partial class MainForm : Form
{
private const string ConnectionString = "Data Source=YourServer;Initial
Catalog=MyDb;Integrated Security=True";
public MainForm()
{
InitializeComponent();
PopulateEmployeeListBox();
}
Output
When you run the application, the listBoxEmployees will be populated with
employee details retrieved from the Employees table in the MyDb database.
Q22. Write a program using ADO.net to insert, update, delete data in back
end.
using System;
using System.Data.SqlClient;
namespace DataManipulationApp
{
class Program
{
private const string ConnectionString = "Data Source=YourServer;Initial
Catalog=YourDatabase;Integrated Security=True";
// Display Employees
DisplayEmployees();
// Update Data
UpdateEmployee(1, "Updated John", "Doe", "updatedjohn@example.com");
// Delete Data
DeleteEmployee(1);
Q23. Display the data from the table in a DataGridView control using dataset.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace DataGridViewWithDataSet
{
public partial class MainForm : Form
{
private const string ConnectionString = "Data Source=YourServer;Initial
Catalog=YourDatabase;Integrated Security=True";
public MainForm()
{
InitializeComponent();
LoadDataIntoDataGridView();
}