0% found this document useful (0 votes)
3 views26 pages

C#file

The document contains multiple C# programs demonstrating various programming concepts such as arithmetic operations, pyramid patterns, grade calculation, finding the largest number, volume calculation using method overloading, area calculation using constructors, multiple inheritance through interfaces, operator overloading, exception handling with multiple catch statements, and single inheritance. Each program includes user input, processing logic, and output display. The examples illustrate fundamental programming techniques and object-oriented principles in C#.

Uploaded by

Charlie
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views26 pages

C#file

The document contains multiple C# programs demonstrating various programming concepts such as arithmetic operations, pyramid patterns, grade calculation, finding the largest number, volume calculation using method overloading, area calculation using constructors, multiple inheritance through interfaces, operator overloading, exception handling with multiple catch statements, and single inheritance. Each program includes user input, processing logic, and output display. The examples illustrate fundamental programming techniques and object-oriented principles in C#.

Uploaded by

Charlie
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Program 1: Program to perform arithmetic operation

using System;

class ArithmeticOperations

static void Main()

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

double num1 = Convert.ToDouble(Console.ReadLine());

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

double num2 = Convert.ToDouble(Console.ReadLine());

Console.WriteLine("Choose operation: +, -, *, /");

char operation = Convert.ToChar(Console.ReadLine());

double result = 0;

switch (operation)

case '+':

result = num1 + num2;

break;

case '-':

result = num1 - num2;

break;

Vanshika
case '*':

result = num1 * num2;

break;

case '/':

if (num2 != 0)

result = num1 / num2;

else

Console.WriteLine("Error! Division by zero is not allowed.");

return;

default:

Console.WriteLine("Invalid operation selected.");

return;

Console.WriteLine($"Result: {result}");

OUTPUT:

Vanshika
Program 2: Program to print pyramid pattern by using loop

using System;

class PyramidPattern

static void Main()

int n = 5; // Number of rows for the pyramid

for (int i = 1; i <= n; i++)

// Print spaces

for (int j = 1; j <= n - i; j++)

Console.Write(" ");

// Print stars

for (int k = 1; k <= (2 * i - 1); k++)

Console.Write("*");

// Move to the next line

Console.WriteLine();

Vanshika
OUTPUT:

Vanshika
Program 2: Program to print pyramid by using go to statement

using System;

class PyramidPattern

static void Main()

int n = 5; // Number of rows in the pyramid

int i = 1, j, space;

StartRow:

if (i > n) return; // Stop when all rows are printed

space = n - i; // Calculate spaces before stars

j = 1;

PrintSpace:

if (space > 0)

Console.Write(" ");

space--;

goto PrintSpace;

Vanshika
PrintStar:

if (j <= (2 * i - 1)) // Print stars

Console.Write("*");

j++;

goto PrintStar;

Console.WriteLine(); // Move to the next line

i++;

goto StartRow; // Repeat for next row

OUTPUT:

Vanshika
Program 3: Program to display grade of marks by using switch statement

using System;

class Program

static void Main()

Console.Write("Enter marks (0-100): ");

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

string grade = marks switch

>= 90 and <= 100 => "A",

>= 80 and < 90 => "B",

>= 70 and < 80 => "C",

>= 60 and < 70 => "D",

>= 50 and < 60 => "E",

>= 0 and < 50 => "F",

_ => "Invalid marks"

};

Console.WriteLine("Your grade is: " + grade);

Vanshika
OUTPUT:

Vanshika
Program 4: Program to find largest number among three input numbers

using System;

class Program

static void Main()

// Prompt user for input

Console.WriteLine("Enter three numbers:");

// Read and convert inputs

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

double num1 = Convert.ToDouble(Console.ReadLine());

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

double num2 = Convert.ToDouble(Console.ReadLine());

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

double num3 = Convert.ToDouble(Console.ReadLine());

// Determine the largest number

double largest = (num1 > num2) ? ((num1 > num3) ? num1 : num3) : ((num2 > num3) ?
num2 : num3);

Vanshika
// Display the result

Console.WriteLine($"The largest number is: {largest}");

OUTPUT:

Vanshika
Program 5: Program to find volume of cube, cylinder and cuboid by using
method overloading

using System;

class VolumeCalculator

// Method to calculate the volume of a cube

public double CalculateVolume(double side)

return Math.Pow(side, 3);

// Method to calculate the volume of a cylinder

public double CalculateVolume(double radius, double height)

return Math.PI * Math.Pow(radius, 2) * height;

// Method to calculate the volume of a cuboid

public double CalculateVolume(double length, double width, double height)

return length * width * height;

Vanshika
}

class Program

static void Main()

VolumeCalculator calculator = new VolumeCalculator();

// Cube volume

Console.WriteLine("Volume of Cube: " + calculator.CalculateVolume(5));

// Cylinder volume

Console.WriteLine("Volume of Cylinder: " + calculator.CalculateVolume(3, 7));

// Cuboid volume

Console.WriteLine("Volume of Cuboid: " + calculator.CalculateVolume(4, 5, 6));

OUTPUT:

Vanshika
Program 6: Program to find the area of rectangle by using constructor

using System;

class Rectangle

private double length;

private double width;

// Constructor

public Rectangle(double l, double w)

length = l;

width = w;

// Method to calculate area

public double GetArea()

return length * width;

// Method to display area

public void Display()

Vanshika
Console.WriteLine("Length: " + length);

Console.WriteLine("Width: " + width);

Console.WriteLine("Area of Rectangle: " + GetArea());

class Program

static void Main()

Console.Write("Enter the length of the rectangle: ");

double length = Convert.ToDouble(Console.ReadLine());

Console.Write("Enter the width of the rectangle: ");

double width = Convert.ToDouble(Console.ReadLine());

// Creating object and passing values to constructor

Rectangle rect = new Rectangle(length, width);

// Display the area

rect.Display();

Vanshika
OUTPUT:

Vanshika
Program 7: Program to implement multiple inheritance by using interface

using System;

interface IAddition

int Add(int a, int b);

interface IMultiplication

int Multiply(int a, int b);

class Calculator : IAddition, IMultiplication

public int Add(int a, int b)

return a + b;

public int Multiply(int a, int b)

return a * b;

Vanshika
}

class Program

static void Main()

Calculator calc = new Calculator();

int num1 = 10, num2 = 5;

Console.WriteLine($"Addition of {num1} and {num2}: {calc.Add(num1, num2)}");

Console.WriteLine($"Multiplication of {num1} and {num2}: {calc.Multiply(num1, num2)}");

OUTPUT:

Vanshika
Program 8: Program to implement overloading unary operator

using System;

class Number

private int value;

// Constructor

public Number(int val)

value = val;

// Overloading Unary Plus (+)

public static Number operator +(Number n)

return new Number(+n.value);

// Overloading Unary Minus (-)

public static Number operator -(Number n)

Vanshika
return new Number(-n.value);

// Overloading Logical NOT (!)

public static bool operator !(Number n)

return n.value == 0;

// Overloading Increment Operator (++)

public static Number operator ++(Number n)

return new Number(n.value + 1);

// Overloading Decrement Operator (--)

public static Number operator --(Number n)

return new Number(n.value - 1);

// Display Method

public void Display()

Vanshika
Console.WriteLine("Value: " + value);

// Main Method to Test

public static void Main()

Number num1 = new Number(10);

Number num2 = new Number(-5);

Number num3 = new Number(0);

Console.WriteLine("Original Values:");

num1.Display();

num2.Display();

num3.Display();

Console.WriteLine("\nApplying Unary Operators:");

Number posNum = +num1;

Number negNum = -num1;

Number incremented = ++num1;

Number decremented = --num2;

posNum.Display();

negNum.Display();

incremented.Display();

Vanshika
decremented.Display();

Console.WriteLine("\nLogical NOT Operator:");

Console.WriteLine("!num1: " + !num1);

Console.WriteLine("!num3: " + !num3); // Should return true as num3 is 0

OUTPUT:

Vanshika
Program 9: Program to implement multiple catch statement

using System;

class MultipleCatchDemo

static void Main()

try

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

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

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

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

// Division operation

int result = num1 / num2;

Console.WriteLine($"Result: {result}");

// Array index access

int[] arr = { 1, 2, 3 };

Console.WriteLine("Accessing array element at index 5: " + arr[5]);

catch (DivideByZeroException ex)

Vanshika
{

Console.WriteLine("Error: Division by zero is not allowed.");

Console.WriteLine("Exception Details: " + ex.Message);

catch (FormatException ex)

Console.WriteLine("Error: Invalid input format. Please enter numeric values.");

Console.WriteLine("Exception Details: " + ex.Message);

catch (IndexOutOfRangeException ex)

Console.WriteLine("Error: Array index is out of bounds.");

Console.WriteLine("Exception Details: " + ex.Message);

catch (Exception ex)

Console.WriteLine("A general exception occurred.");

Console.WriteLine("Exception Details: " + ex.Message);

finally

Console.WriteLine("Execution completed.");

Vanshika
}

OUTPUT:

Vanshika
Program 1O: Program to implement single inheritance

using System;

class Parent

protected int num1, num2;

public void SetValues(int a, int b)

num1 = a;

num2 = b;

public void DisplayNumbers()

Console.WriteLine("Number 1: " + num1);

Console.WriteLine("Number 2: " + num2);

class Child : Parent

public int CalculateSum()

return num1 + num2;

Vanshika
}

class Program

static void Main()

Child obj = new Child();

obj.SetValues(10, 20);

obj.DisplayNumbers();

Console.WriteLine("Sum: " + obj.CalculateSum());

OUTPUT:

Vanshika

You might also like