C#LAB
C#LAB
using System;
class Program
{
static void Main()
{
Console.WriteLine("Enter the first number:");
double num1 = Convert.ToDouble(Console.ReadLine());
double result = 0;
switch (op)
{
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
case '%':
result = num1 % num2;
break;
default:
Console.WriteLine("Invalid operation.");
break;
}
***************************************************
class ArmstrongNumbers
{
static void Main()
{
int start = 1, end = 1000;
while (originalNum != 0)
{
remainder = originalNum % 10;
result += (int)Math.Pow(remainder, 3);
originalNum /= 10;
}
if (result == num)
{
Console.WriteLine(num);
}
}
}
}
*****************************************************
****PGRM-3 SUBSTRING****
using System;
class Program
{
static void Main()
{
string str = "hello";
namespace ExceptionDemo
{
class Program
{
static void Main(string[] args)
{
int num1, num2, arrayIndex;
int[] myArray = { 10, 20, 30 };
try
{
// Potential divide by zero exception
int result = num1 / num2;
Console.WriteLine("Result: {0}", result);
// Fill the remaining elements using the formula C(n, r) = C(n-1, r-1) +
C(n-1, r)
for (int i = 2; i < rows; i++)
{
for (int j = 1; j < i; j++)
{
pascalTriangle[i, j] = pascalTriangle[i - 1, j - 1] +
pascalTriangle[i - 1, j];
}
}
// Print the Pascal's triangle in a triangular shape
for (int i = 0; i < rows; i++)
{
// Print leading spaces for alignment
for (int j = 0; j < rows - i - 1; j++)
{
Console.Write(" "); // Two spaces for better spacing
}
class Program
{
static void Main(string[] args)
{
Console.Write("Enter the number of rows for Floyd's Triangle: ");
int rows = int.Parse(Console.ReadLine());
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;
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();
}
}
}
**************PGRM-7******************
using System;
using System.IO;
class FileCopy
{
static void Main()
{
Console.WriteLine("Enter the path of the source text file:");
string sourceFilePath = Console.ReadLine();
try
{
// Ensure the destination directory exists
Directory.CreateDirectory(Path.GetDirectoryName(destinationFilePath));
*************************PGRM-8*************************
using System;
class StackImplementation
{
static void Main()
{
// Create a stack instance
Stack stack = new Stack();
while (true)
{
Console.WriteLine("Choose an option:");
Console.WriteLine("1. Push");
Console.WriteLine("2. Pop");
Console.WriteLine("3. Display");
Console.WriteLine("4. Exit");
switch (choice)
{
case 1:
Console.WriteLine("Enter the value to push:");
int valueToPush = GetValue();
stack.Push(valueToPush);
break;
case 2:
int poppedValue = stack.Pop();
if (poppedValue != -1)
Console.WriteLine($"Popped element: {poppedValue}");
break;
case 3:
stack.Display();
break;
case 4:
Console.WriteLine("Exiting the program.");
Environment.Exit(0);
break;
default:
Console.WriteLine("Invalid choice. Please choose a valid
option.");
break;
}
}
}
class Stack
{
private const int MaxSize = 10;
private int[] items;
private int top;
public Stack()
{
items = new int[MaxSize];
top = -1;
}
items[++top] = value;
Console.WriteLine($"Pushed element: {value}");
}
Console.WriteLine("Stack elements:");
for (int i = top; i >= 0; i--)
{
Console.WriteLine(items[i]);
}
}
}
**************************PGM-9******************************
using System;
class Complex
{
private double real;
private double imaginary;
// Constructor
public Complex(double real, double imaginary)
{
this.real = real;
this.imaginary = imaginary;
}
class Program
{
static void Main()
{
Console.WriteLine("Enter the first complex number:");
Console.Write("Real part: ");
double real1 = Convert.ToDouble(Console.ReadLine());
Console.Write("Imaginary part: ");
double imaginary1 = Convert.ToDouble(Console.ReadLine());
**************PGRM-10*********************************************
using System;
class Shape
{
public virtual void Draw()
{
Console.WriteLine("Drawing a generic shape.");
}
class Program
{
static void Main()
{
// Create instances of the derived classes
Shape circle = new Circle();
Shape triangle = new Triangle();
Shape square = new Square();
*****************************PGRM-11**********************
using System;
class Program
{
static void Main()
{
// Input for Circle
Console.WriteLine("Enter the radius of the circle:");
double circleRadius = Convert.ToDouble(Console.ReadLine());
Circle circle = new Circle(circleRadius);
*********************PGRM-12*****************************
using System;
class Program
{
static void Main()
{
// Input for initial values
Console.WriteLine("Enter the initial width of the rectangle:");
int initialWidth = Convert.ToInt32(Console.ReadLine());