NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

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());
// Generate and print Pascal's Triangle
int[,] pascalsTriangle = GeneratePascalsTriangle(numRows);
PrintPascalsTriangle(pascalsTriangle);
}
static int[,] GeneratePascalsTriangle(int numRows)
{
int[,] triangle = new int[numRows, numRows];
for (int i = 0; i < numRows; i++) {
for (int j = 0; j <= i; j++) {
if (j == 0 || j == i)
triangle[i, j] = 1;
else
triangle[i, j] = triangle[i - 1, j - 1] + triangle[i - 1, j];
} }
return triangle;
}
static void PrintPascalsTriangle(int[,] triangle)
{
int numRows = triangle.GetLength(0);
Console.WriteLine("Pascal's Triangle:");
for (int i = 0; i < numRows; i++) {
// Add spacing to center the triangle
Console.Write(new string(' ', (numRows - i)));
for (int j = 0; j <= i; j++) {
Console.Write($"{triangle[i, j]} "); }
Console.WriteLine();
} } }


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);
}
static int[][] GenerateFloydsTriangle(int numRows)
{
int[][] triangle = new int[numRows][];
for (int i = 0; i < numRows; i++)
{
triangle[i] = new int[i + 1];
int value = 1;
for (int j = 0; j <= i; j++)
{
triangle[i][j] = value++;
}
}
return triangle;
}
static void PrintFloydsTriangle(int[][] triangle)
{
int numRows = triangle.Length;
Console.WriteLine("Floyd's Triangle:");
for (int i = 0; i < numRows; i++)
{
Console.Write(new string(' ', (numRows - i)));
for (int j = 0; j < triangle[i].Length; j++) {
Console.Write($"{triangle[i][j]} "); }
Console.WriteLine();
}
}
}


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();
Console.Write("Enter the path of the destination text file: ");
string destinationFilePath = Console.ReadLine();
// Read and copy the file contents
CopyFileContents(sourceFilePath, destinationFilePath);
Console.WriteLine("File contents copied successfully!");
/* Display the content */
string fileContent = File.ReadAllText(destinationFilePath);
Console.WriteLine("File Content:");
Console.WriteLine(fileContent);
}
static void CopyFileContents(string sourceFilePath, string destinationFilePath)
{
try
{
// Read the contents of the source file
string fileContents = File.ReadAllText(sourceFilePath);
// Write the contents to the destination file
File.WriteAllText(destinationFilePath, fileContents);
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
}


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;
// Constructor to initialize the complex number
public Complex(double real, double imaginary)
{
this.real = real;
this.imaginary = imaginary;
}
// Overloaded + operator for complex number addition
public static Complex operator +(Complex c1, Complex c2)
{
double realSum = c1.real + c2.real;
double imaginarySum = c1.imaginary + c2.imaginary;
return new Complex(realSum, imaginarySum);
}
// Method to display the complex number
public void Display()
{
Console.WriteLine($"Complex Number: {real} + {imaginary}i");
}
}
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());
Console.Write("Enter the imaginary part of the first complex number: ");
double imaginary1 = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter the real part of the second complex number: ");
double real2 = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter the imaginary part of the second complex number: ");
double imaginary2 = Convert.ToDouble(Console.ReadLine());
// Create Complex objects
Complex complex1 = new Complex(real1, imaginary1);
Complex complex2 = new Complex(real2, imaginary2);
// Perform addition using the overloaded + operator
Complex sum = complex1 + complex2;
// Display the results
Console.WriteLine("nResults:");
complex1.Display();
complex2.Display();
sum.Display();
}
}


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;
// Abstract base class Shape
abstract class Shape
{
// Abstract method to calculate area
public abstract double CalculateArea();
// Abstract method to calculate perimeter
public abstract double CalculatePerimeter();
}
// Subclass Circle
class Circle : Shape
{
private double radius;
// Constructor for Circle
public Circle(double radius)
{
this.radius = radius;
}
// Implementation of CalculateArea for Circle
public override double CalculateArea()
{
return Math.PI * radius * radius;
}
// Implementation of CalculatePerimeter for Circle
public override double CalculatePerimeter()
{
return 2 * Math.PI * radius;
}
}
// Subclass Triangle
class Triangle : Shape
{
private double side1, side2, side3;
// Constructor for Triangle
public Triangle(double side1, double side2, double side3)
{
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
// Implementation of CalculateArea for Triangle
public override double CalculateArea()
{
// Using Heron's formula to calculate the area of a triangle
double s = (side1 + side2 + side3) / 2;
return Math.Sqrt(s * (s - side1) * (s - side2) * (s - side3));
}
// Implementation of CalculatePerimeter for Triangle
public override double CalculatePerimeter()
{
return side1 + side2 + side3;
}
}
class Program
{
static void Main()
{
// Demonstrate using the abstract class and its subclasses
// Create instances of Circle and Triangle
Circle circle = new Circle(5);
Triangle triangle = new Triangle(3, 4, 5);
// Calculate and display area and perimeter for Circle
Console.WriteLine("Circle - Area: {0}, Perimeter: {1}", circle.CalculateArea(),
circle.CalculatePerimeter());
// Calculate and display area and perimeter for Triangle
Console.WriteLine("Triangle - Area: {0}, Perimeter: {1}", triangle.CalculateArea(),
triangle.CalculatePerimeter());
}
}


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;
// Define the Resizable interface
interface Resizable
{
void ResizeWidth(int width);
void ResizeHeight(int height);
}
// Implement the Resizable interface in the Rectangle class
class Rectangle : Resizable
{
private int width;
private int height;
// Constructor for Rectangle
public Rectangle(int width, int height)
{
this.width = width;
this.height = height;
}
// Implementation of ResizeWidth method
public void ResizeWidth(int newWidth)
{
if (newWidth > 0)
{
width = newWidth;
Console.WriteLine($"Width resized to {width}");
}
else
{
Console.WriteLine("Invalid width. Width must be greater than 0.");
}
}
// Implementation of ResizeHeight method
public void ResizeHeight(int newHeight)
{
if (newHeight > 0)
{
height = newHeight;
Console.WriteLine($"Height resized to {height}");
}
else
{
Console.WriteLine("Invalid height. Height must be greater than 0.");
}
}
// Method to display the current dimensions of the rectangle
public void DisplayDimensions()
{
Console.WriteLine($"Rectangle Dimensions - Width: {width}, Height: {height}");
}
}
class Program
{
static void Main()
{
// Demonstrate using the Resizable interface and Rectangle class
// Create an instance of Rectangle
Rectangle rectangle = new Rectangle(5, 8);
// Display initial dimensions
Console.WriteLine("Initial Rectangle Dimensions:");
rectangle.DisplayDimensions();
// Resize width and height using the Resizable interface methods
rectangle.ResizeWidth(10);
rectangle.ResizeHeight(12);
// Display updated dimensions
Console.WriteLine("nUpdated Rectangle Dimensions:");
rectangle.DisplayDimensions();
}
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.