C# Lab Manual As On 23-01-2024
C# Lab Manual As On 23-01-2024
C# Lab Manual As On 23-01-2024
C# PROGRAMMING
V Semester
Scheme:2021
Version-1
w.e.f 3rd Nov 2023
Editorial Committee
C# PROGRAMMING Faculty, Dept of CSE
Approved by
HOD, Dept.of CSE
Document Log
M2: Enhancing the knowledge in the changing technology trends by giving hands-
on experience through continuous educationand by making them to organize
&participate in various events.
M3: Impart skills in the field of IT and its related areas with a focus on developing
the required competencies and virtues to meet the industry expectations.
Course Outcomes: At the end of the course,the student will be able to:
CO1 Apply concepts of OOPs in developing solutions to problems
CO2 Develop programs to involving basic features, handling exception and text files
CO3 Make use of modern tools to develop C# programming and applications
Syllabus
Laboratory Experiments:
Implement the following programs using C# Programming Language
1 Develop a c# program to simulate simple arithmetic calculator for Addition, Subtraction,
Multiplication, Division and Mod operations. Read the operator and operands through
console
2 Develop c# program to print Armstrong Number between 1 to 1000
3 Develop a c# program to list all substrings in a given string [Hint:use of Substring()
method]
4 Develop C# program to demonstrate division by zero and index out of range exceptions
5 Develop a C# program to generate and print pascal triangle using two dimensional array
6 Develop a c# program to generate and print floyds triangle using jagged arrays
7 Develop a c# program to read a text file and copy the file contents to another text file
8 Develop a c# program to implement stack with push and pop operations[Hint:Use class
,get/set properties, methods for push and pop and main method
9 Design a class complex with data members, constructor and method for overloading a
binary operator '+'. Develop a c# program to read two complex number and print the
results of addition.
Figure-1
CHAPTER-3
LAB PROGRAMS
1.Develop a c# program to simulate simple arithmetic calculator for Addition, Subtraction,
Multiplication, Division and Mod operations. Read the operator and operands through console
using System;
class Calculator
{
static void Main()
{
Console.WriteLine("Simple Arithmetic Calculator");
Console.WriteLine("----------------------------");
double result = 0;
return;
}
break;
case '%':
// Check for division by zero
if (operand2 != 0)
{
result = operand1 % operand2;
}
else
{
Console.WriteLine("Error: Modulo by zero is not allowed.");
return;
}
break;
default:
Console.WriteLine("Error: Invalid operator.");
return;
}
output:
Simple Arithmetic Calculator
----------------------------
Enter operator (+, -, *, /, %): +
Enter first operand: 8
Enter second operand: 9
Result: 8 + 9 = 17
using System;
class ArmstrongNumbers
{
static void Main()
{
Console.WriteLine("Armstrong Numbers between 1 and 1000:");
}
if (c == arm)
return true;
else
return false;
}
}
Output:
Armstrong Numbers between 1 and 1000:
1
2
3
4
5
6
7
8
9
153
370
371
407
3.Develop a c# program to list all substrings in a given string [Hint:use of Substring() method]
using System;
class SubstringExample
{
static void Main()
{
Console.WriteLine("Enter a string:");
string input = Console.ReadLine();
Console.WriteLine("\nAll substrings:");
ListSubstrings(input);
}
Output:
Enter a string:
ABC
All substrings:
A
AB
ABC
B
BC
C
4. Develop C# program to demonstrate division by zero and index out of range exceptions
using System;
class Program
{
static void Main()
{
DivisionByZeroExceptionExample(); // Division by Zero Exception
IndexOutOfRangeExceptionExample(); // Index Out of Range Exception
}
5. Develop a C# program to generate and print pascal triangle using two dimensional array
using System;
class Program
{
static void Main()
{
Console.Write("Enter the number of rows for Pascal's Triangle: ");
int numRows = Convert.ToInt32(Console.ReadLine());
6. Develop a c# program to generate and print floyds triangle using jagged arrays
using System;
class Program
{
static void Main()
{
Console.Write("Enter the number of rows for Floyd's Triangle: ");
int numRows = Convert.ToInt32(Console.ReadLine());
// Generate and print Floyd's Triangle
int[][] floydsTriangle = GenerateFloydsTriangle(numRows);
PrintFloydsTriangle(floydsTriangle);
}
7.Develop a c# program to read a text file and copy the file contents to another text file
using System;
using System.IO;
class Program
{
static void Main()
{
Console.Write("Enter the path of the source text file: ");
string sourceFilePath = Console.ReadLine();
Output:
Enter the path of the source text file: file1.txt
Enter the path of the destination text file: file2.txt
File contents copied successfully!
(Content of file2.txt displayed)
Example path: C:\Users\Hp\source\repos\readfile\readfile\bin\Debug\net8.0
8.Develop a c# program to implement stack with push and pop operations[Hint:Use class ,get/set
properties, methods for push and pop and main method
using System;
class StackProgram
{
static void Main()
{
Stack stack = new Stack(); // Create a stack
class Stack
{
private const int MaxSize = 10;
private int[] array = new int[MaxSize];
private int top = -1;
9.Design a class complex with data members, constructor and method for overloading a binary
operator '+'. Develop a c# program to read two complex number and print the results of addition.
using System;
class Complex
{
private double real;
private double imaginary;
class Program
{
static void Main()
{
// Read two complex numbers from the user
Console.Write("Enter the real part of the first complex number: ");
double real1 = Convert.ToDouble(Console.ReadLine());
Output:
Enter the real part of the first complex number: 2
Enter the imaginary part of the first complex number: 5
Enter the real part of the second complex number: 8
Enter the imaginary part of the second complex number: 9
Results:
Complex Number: 2 + 5i
Complex Number: 8 + 9i
Complex Number: 10 + 14i
10. Develop a C# program to create a class named shape. Create three subclasses namely: circle,triangle
and square, each class has two member functions named draw() and erase(). Demonstrate
polymorphism concepts by developing suitable methods, defining member data and main program
using System;
// Subclass Circle
class Circle : Shape
{
// Member function to draw a circle
public override void Draw()
{
Console.WriteLine("Drawing a circle");
}
// Subclass Triangle
class Triangle : Shape
{
// Member function to draw a triangle
public override void Draw()
{
Console.WriteLine("Drawing a triangle");
}
{
Console.WriteLine("Erasing a triangle");
}
}
// Subclass Square
class Square : Shape
{
// Member function to draw a square
public override void Draw()
{
Console.WriteLine("Drawing a square");
}
// Member function to erase a square
public override void Erase()
{
Console.WriteLine("Erasing a square");
}
}
class Program
{
static void Main()
{
// Demonstrate polymorphism
// Create an array of shapes
Shape[] shapes = new Shape[3];
// Iterate through the array and call Draw and Erase methods
foreach (Shape shape in shapes)
{
shape.Draw();
shape.Erase();
Console.WriteLine(); // Add a newline for better readability
}
}
}
Output:
Drawing a circle
Erasing a circle
Drawing a triangle
Erasing a triangle
Drawing a square
Erasing a square
11. Develop a c# program to create an abstract class shape with abstract method calculate Area() and
calculate Perimeter(). Create subclasses Circle and Triangle that extend the shape class and implement
the respective methods to calculate the area and perimeter of each shape
using System;
// Subclass Circle
class Circle : Shape
{
private double radius;
// Subclass Triangle
class Triangle : Shape
{
private double side1, side2, side3;
this.side3 = side3;
}
class Program
{
static void Main()
{
// Demonstrate using the abstract class and its subclasses
Output:
Circle - Area: 78.53981633974483, Perimeter: 31.41592653589793
Triangle - Area: 6, Perimeter: 12
12. Develop a C# program to create an interface Resizable with methods reizeWidth(int width) and
resizeHeight(int height) that allow an object to be resized. Create a class Rectangle that implements the
Resizable interface and implements the resize methods
using System;
}
}
class Program
{
static void Main()
{
// Demonstrate using the Resizable interface and Rectangle class
CHAPTER 4
BASIC VIVA QUESTIONS
CHAPTER 5
CONTENT BEYOND SYLLABUS
OUTPUT:
Number: 198
Number in reverse order: 891
2. Write a C# program to compute the sum and product of the numbers from 1 to 10.
using System;
class ProdSum
{
static void Main()
{
int prod;
int sum;
int i;
sum = 0;
prod = 1;
for (i = 1; i <= 10; i++)
{
sum = sum + i;
prod = prod * i;
}
Console.WriteLine("Sum is " + sum);
Console.WriteLine("Product is " + prod);
}
}
Output:
Sum is 55
Product is 3628800
using System;
class MakeOdd
{
static void Main()
{
ushort num;
ushort i;
for (i = 1; i <= 5; i++)
{
num = i;
Console.WriteLine("num: " + num);
num = (ushort)(num | 1);
Console.WriteLine("num after turning on bit zero: "
+ num + "\n");
}
}
}
Output:
num: 1
num after turning on bit zero: 1
num: 2
num after turning on bit zero: 3
num: 3
num after turning on bit zero: 3
num: 4
num after turning on bit zero: 5
num: 5
num after turning on bit zero: 5
Output:
Enter a Number : 5
Factorial of 5 is: 120