Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 20

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()
{
string input;
do
{
Console.Write("Enter first number: ");
int num1 = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter second number: ");


int num2 = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter operator (+, -, *, /): ");


string op = Console.ReadLine();

int result = 0;

switch (op)
{
case "+": result = num1 + num2; break;
case "-": result = num1 - num2; break;
case "*": result = num1 * num2; break;
case "/":
if (num2 != 0)
{
result = num1 / num2;
}
else
{
Console.WriteLine("Cannot divide by zero.");

}
break;
default:
Console.WriteLine("Invalid operator.");
break;
}

Console.WriteLine($"Result: {result}");
Console.Write("Do you want to continue? (y/n): ");
input = Console.ReadLine();
}
while (input.ToLower() == "y");
}
}
OUTPUT
Enter first number: 10
Enter second number: 5
Enter operator (+, -, *, /): *
Result: 50
Do you want to continue? (y/n): y

2) Develop a C# program to print Armstrong Number between 1 to 1000.


using System;

namespace ArmstrongNumbers

class Program

static void Main(string[] args)

int n, r, len;

double sum = 0;

Console.WriteLine("Enter a number:");

n = int.Parse(Console.ReadLine());

len = n.ToString().Length;

int temp = n; // Store the original value

while (n > 0)

r = n % 10;

sum += Math.Pow(r, len);


n /= 10;

if (temp == sum)

Console.WriteLine("Armstrong number");

else

Console.WriteLine("Not Armstrong number");

Console.ReadKey();

OUTPUT

Enter a number:

153

Armstrong number

3) Develop a C# program to list all substrings in a given string. [ Hint: use


of Substring() method]
using System;

class SubstringExample

public static void Main()

Console.WriteLine("Enter a string:");

string input = Console.ReadLine();

Console.WriteLine("All substrings of the given string:");

ListSubstrings(input);

}
public static void ListSubstrings(string input)

for (int i = 1; i <= input.Length; i++)

for (int j = 0; j <= input.Length - i; j++)

string substring = input.Substring(j, i);

Console.WriteLine(substring);

OUTPUT

Enter a string:

abc

All substrings of the given string:

ab

bc

abc

4) Develop a C# program to demonstrate Division by Zero and Index Out


of Range exceptions.
using System;

class ExceptionDemo
{
static void Main()
{
DivideByZeroExceptionExample();
IndexOutOfRangeExceptionExample();
}
static void DivideByZeroExceptionExample()
{
try
{
int numerator = 10;
int denominator = 0;
int result = numerator / denominator; // This line will throw DivideByZeroException
}
catch (DivideByZeroException ex)
{
Console.WriteLine("Divide By Zero Exception caught: " + ex.Message);
}
}

static void IndexOutOfRangeExceptionExample()


{
try
{
int[] array = { 1, 2, 3, 4, 5 };
int indexOutOfRange = array[10]; // This line will throw IndexOutOfRangeException
}
catch (IndexOutOfRangeException ex)
{
Console.WriteLine("Index Out Of Range Exception caught: " + ex.Message);
}
}
}
OUTPUT
Divide By Zero Exception caught: Attempted to divide by zero.
Index Out Of Range Exception caught: Index was outside the bounds of the array.
5) Develop a C# program to generate and printPascal Triangle using Two
Dimensional arrays.
using System;

public class PascalTriangle


{
public static void Main()
{
Console.WriteLine("Display the Pascal's Triangle:");
Console.WriteLine("--------------------------------");

Console.Write("Input number of rows: ");


int noRows = Convert.ToInt32(Console.ReadLine());

for (int i = 0; i < noRows; i++)


{
for (int spaces = 1; spaces <= noRows - i; spaces++)
Console.Write(" ");

int coefficient = 1;
for (int j = 0; j <= i; j++)
{
if (j > 0)
coefficient = coefficient * (i - j + 1) / j;

Console.Write($"{coefficient} ");
}

Console.WriteLine();
}
}
}
OUTPUT
Input number of rows: 5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

6) Develop a C# program to generate and print Floyds Triangle using


Jagged arrays.
using System;

class FloydTriangle
{
static void Main()
{
Console.WriteLine("Enter the number of rows for Floyd's Triangle:");
int rows = int.Parse(Console.ReadLine());

int[][] triangle = new int[rows][];


int count = 1;

for (int i = 0; i < rows; i++)


{
triangle[i] = new int[i + 1];

for (int j = 0; j <= i; j++)


{
triangle[i][j] = count++;
}
}

Console.WriteLine("Floyd's Triangle:");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j <= i; j++)
{
Console.Write(triangle[i][j] + " ");
}
Console.WriteLine();
}
}
}
OUTPUT
Enter the number of rows for Floyd's Triangle:
4
Floyd's Triangle:
1
23
456
7 8 9 10

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 FileCopy
{
static void Main()
{
string sourceFilePath = "source.txt";
string destinationFilePath = "destination.txt";

try
{
string fileContents = File.ReadAllText(sourceFilePath);
File.WriteAllText(destinationFilePath, fileContents);
Console.WriteLine("File contents copied successfully.");
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
}
OUTPUT
// Assuming content of source.txt is "Hello, World!"
File contents copied successfully.

8) Develop a C# 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 Stack
{
private int[] stackArray;
private int top;
private const int maxSize = 10;

public Stack()
{
stackArray = new int[maxSize];
top = -1;
}

public void Push(int item)


{
if (top < maxSize - 1)
{
stackArray[++top] = item;
Console.WriteLine("Pushed " + item + " to the stack.");
}
else
{
Console.WriteLine("Stack overflow. Cannot push.");
}
}

public void Pop()


{
if (top >= 0)
{
int poppedItem = stackArray[top--];
Console.WriteLine("Popped " + poppedItem + " from the stack.");
}
else
{
Console.WriteLine("Stack underflow. Cannot pop.");
}
}

static void Main()


{
Stack stack = new Stack();
stack.Push(5);
stack.Push(10);
stack.Pop();
stack.Pop();
}}
OUTPUT
Pushed 5 to the stack.
Pushed 10 to the stack.
Popped 10 from the stack.
Popped 5 from the stack.

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;

public Complex(double real, double imaginary)


{
this.real = real;
this.imaginary = imaginary;
}

public static Complex operator +(Complex c1, Complex c2)


{
return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
}

public void Display()


{
Console.WriteLine("Result: " + real + " + " + imaginary + "i");
}

static void Main()


{
Console.WriteLine("Enter the first complex number:");
Console.Write("Real part: ");
double real1 = double.Parse(Console.ReadLine());
Console.Write("Imaginary part: ");
double imaginary1 = double.Parse(Console.ReadLine());

Console.WriteLine("Enter the second complex number:");


Console.Write("Real part: ");
double real2 = double.Parse(Console.ReadLine());
Console.Write("Imaginary part: ");
double imaginary2 = double.Parse(Console.ReadLine());

Complex complex1 = new Complex(real1, imaginary1);


Complex complex2 = new Complex(real2, imaginary2);

Complex result = complex1 + complex2;

Console.WriteLine("Sum of two complex numbers:");


result.Display();
}
}
OUTPUT
Enter the first complex number:
Real part: 3
Imaginary part: 5
Enter the second complex number:
Real part: 2
Imaginary part: -1
Sum of two complex numbers:
Result: 5 + 4i
10) Develop a C# program to create a class named shape. Create three sub
classes 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;

class Shape
{
public virtual void Draw()
{
Console.WriteLine("Drawing shape");
}

public virtual void Erase()


{
Console.WriteLine("Erasing shape");
}
}

class Circle : Shape


{
public override void Draw()
{
Console.WriteLine("Drawing circle");
}

public override void Erase()


{
Console.WriteLine("Erasing circle");
}
}

class Triangle : Shape


{
public override void Draw()
{
Console.WriteLine("Drawing triangle");
}

public override void Erase()


{
Console.WriteLine("Erasing triangle");
}
}

class Square : Shape


{
public override void Draw()
{
Console.WriteLine("Drawing square");
}

public override void Erase()


{
Console.WriteLine("Erasing square");
}
}

class PolymorphismDemo
{
static void Main()
{
Shape[] shapes = { new Circle(), new Triangle(), new Square() };

foreach (Shape shape in shapes)


{
shape.Draw();
shape.Erase();
Console.WriteLine();
}
}
}
OUTPUT
Drawing circle
Erasing circle

Drawing triangle
Erasing triangle

Drawing square
Erasing square

11) Develop a C# program to create an abstract class Shape with abstract


methods calculateArea() and calculatePerimeter(). 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;

// Abstract class: Shape


public abstract class Shape
{
public abstract void CalculateArea();
public abstract void CalculatePerimeter();
}

// Derived class: Circle


public class Circle : Shape
{
private double Radius;
public Circle(double radius)
{
Radius = radius;
}

public override void CalculateArea()


{
double area = Math.PI * Radius * Radius;
Console.WriteLine("Area of Circle: " + area.ToString("F2"));
}

public override void CalculatePerimeter()


{
double perimeter = 2 * Math.PI * Radius;
Console.WriteLine("Perimeter of Circle: " + perimeter.ToString("F2"));
}
}

// Derived class: Triangle


public class Triangle : Shape
{
private double Side1;
private double Side2;
private double Side3;

public Triangle(double side1, double side2, double side3)


{
Side1 = side1;
Side2 = side2;
Side3 = side3;
}
public override void CalculateArea()
{
// Using Heron's formula to calculate the area of a triangle
double s = (Side1 + Side2 + Side3) / 2;
double area = Math.Sqrt(s * (s - Side1) * (s - Side2) * (s - Side3));
Console.WriteLine("Area of Triangle: " + area.ToString("F2"));
}

public override void CalculatePerimeter()


{
double perimeter = Side1 + Side2 + Side3;
Console.WriteLine("Perimeter of Triangle: " + perimeter.ToString("F2"));
}
}

class Program
{
static void Main()
{
Circle circle = new Circle(5);
circle.CalculateArea();
circle.CalculatePerimeter();

Console.WriteLine();

Triangle triangle = new Triangle(3, 4, 5);


triangle.CalculateArea();
triangle.CalculatePerimeter();
}
}
OUTPUT
Area of Circle: 78.54
Perimeter of Circle: 31.42

Area of Triangle: 6.00


Perimeter of Triangle: 12.00

12) Develop a C# program to create an interface Resizable with methods


resizeWidth(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;

// Interface: Resizable
public interface Resizable
{
void ResizeWidth(int width);
void ResizeHeight(int height);
}

// Class: Rectangle
public class Rectangle : Resizable
{
private int Width;
private int Height;

public Rectangle(int width, int height)


{
Width = width;
Height = height;
}

public void Display()


{
Console.WriteLine("Rectangle - Width: " + Width + ", Height: " + Height);
}

public void ResizeWidth(int width)


{
Width = width;
Console.WriteLine("Resized Width to: " + Width);
}

public void ResizeHeight(int height)


{
Height = height;
Console.WriteLine("Resized Height to: " + Height);
}
}

class Program
{
static void Main()
{
Rectangle rectangle = new Rectangle(10, 5);

Console.WriteLine("Original Rectangle:");
rectangle.Display();

Console.WriteLine("\nResizing the Rectangle:");


rectangle.ResizeWidth(15);
rectangle.ResizeHeight(8);

Console.WriteLine("\nUpdated Rectangle:");
rectangle.Display();
}
}
OUTPUT
Original Rectangle:
Rectangle - Width: 10, Height: 5

Resizing the Rectangle:


Resized Width to: 15
Resized Height to: 8

Updated Rectangle:
Rectangle - Width: 15, Height: 8

You might also like