C# Manual Draft (Ise)
C# Manual Draft (Ise)
C# Manual Draft (Ise)
class Calculator
{
static void Main()
{
string input;
do
{
Console.Write("Enter first number: ");
int num1 = Convert.ToInt32(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
namespace ArmstrongNumbers
class Program
int n, r, len;
double sum = 0;
Console.WriteLine("Enter a number:");
n = int.Parse(Console.ReadLine());
len = n.ToString().Length;
while (n > 0)
r = n % 10;
if (temp == sum)
Console.WriteLine("Armstrong number");
else
Console.ReadKey();
OUTPUT
Enter a number:
153
Armstrong number
class SubstringExample
Console.WriteLine("Enter a string:");
ListSubstrings(input);
}
public static void ListSubstrings(string input)
Console.WriteLine(substring);
OUTPUT
Enter a string:
abc
ab
bc
abc
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);
}
}
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
class FloydTriangle
{
static void Main()
{
Console.WriteLine("Enter the number of rows for Floyd's Triangle:");
int rows = int.Parse(Console.ReadLine());
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.
class Stack
{
private int[] stackArray;
private int top;
private const int maxSize = 10;
public Stack()
{
stackArray = new int[maxSize];
top = -1;
}
class Complex
{
private double real;
private double imaginary;
class Shape
{
public virtual void Draw()
{
Console.WriteLine("Drawing shape");
}
class PolymorphismDemo
{
static void Main()
{
Shape[] shapes = { new Circle(), new Triangle(), new Square() };
Drawing triangle
Erasing triangle
Drawing square
Erasing square
class Program
{
static void Main()
{
Circle circle = new Circle(5);
circle.CalculateArea();
circle.CalculatePerimeter();
Console.WriteLine();
// 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;
class Program
{
static void Main()
{
Rectangle rectangle = new Rectangle(10, 5);
Console.WriteLine("Original Rectangle:");
rectangle.Display();
Console.WriteLine("\nUpdated Rectangle:");
rectangle.Display();
}
}
OUTPUT
Original Rectangle:
Rectangle - Width: 10, Height: 5
Updated Rectangle:
Rectangle - Width: 15, Height: 8