0% found this document useful (0 votes)
7 views3 pages

Progrma7 10

The document contains two C# programs: the first implements a delegate for performing arithmetic operations such as addition, subtraction, multiplication, division, and modulus, while the second demonstrates runtime polymorphism using an interface for calculating the area of different shapes (circle, triangle, square, rectangle). Each program includes user input for necessary parameters and outputs the results of the operations or area calculations. Both programs illustrate fundamental concepts in C# programming, including delegates, interfaces, and polymorphism.

Uploaded by

23ca017.caysus
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)
7 views3 pages

Progrma7 10

The document contains two C# programs: the first implements a delegate for performing arithmetic operations such as addition, subtraction, multiplication, division, and modulus, while the second demonstrates runtime polymorphism using an interface for calculating the area of different shapes (circle, triangle, square, rectangle). Each program includes user input for necessary parameters and outputs the results of the operations or area calculations. Both programs illustrate fundamental concepts in C# programming, including delegates, interfaces, and polymorphism.

Uploaded by

23ca017.caysus
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/ 3

7.

Write a Program in C# to create and implement a Delegate for any two arithmetic operations

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication7
{
class calculate
{
public float add(float a, float b)
{
return (a + b);
}
public float sub(float a, float b)
{
return (a - b);
}
public float mul(float a, float b)
{
return (a * b);
}
public float quo(float a, float b)
{
return (a / b);
}
public float mod(float a, float b)
{
return (a % b);
}

}
public delegate float calculatordelegate(float a,float b);
class Program
{
static void Main(string[] args)
{
calculate c = new calculate();
calculatordelegate cd = new calculatordelegate(c.add);

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


float a = float.Parse(Console.ReadLine());
Console.Write("Enter second number:");
float b = float.Parse(Console.ReadLine());
Console.WriteLine();

Console.WriteLine("The sum is:" + cd(a, b));

cd += c.sub;
Console.WriteLine("The difference is:" + cd(a, b));

cd += c.mul;
Console.WriteLine("The product is:" + cd(a, b));

cd += c.quo;
Console.WriteLine("The quotient is:" + cd(a, b));

cd += c.mod;
Console.WriteLine("The remainder is:" + cd(a, b));

}
}
}

10. Write a Program in C# Demonstrate arrays of interface types (for runtime polymorphism).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication10
{
interface shape
{
double cal_area();
}

class circle:shape
{
public double cal_area()
{
Console.WriteLine();
Console.Write("Enter the radius:");
double r = double.Parse(Console.ReadLine());
double area=3.14 * r * r;
return area;
}

}
class triangle:shape
{

public double cal_area()


{
Console.WriteLine();
Console.Write("Enter the three sides of triangle:");
double a = double.Parse(Console.ReadLine());
double b = double.Parse(Console.ReadLine());
double c = double.Parse(Console.ReadLine());
double s = (a + b + c) / 2.0;

double s1 = s*(s - a);


double s2 = s - b;
double s3 = s - c;

double area = Math.Sqrt( s1 * s2 * s3);


return area;
}

}
class square:shape
{

public double cal_area()


{
Console.WriteLine();
Console.Write("Enter the side of a square:");
double a = double.Parse(Console.ReadLine());
double area=a*a;
return area;
}

}
class rectangle:shape
{
public double cal_area()
{
Console.WriteLine();
Console.Write("Enter the length and breadth of a rectangle:");
double l = double.Parse(Console.ReadLine());
double b = double.Parse(Console.ReadLine());
double area = l *b ;
return area;
}

}
class Program
{
static void Main(string[] args)
{
shape[] s = new shape[4];
s[0] = new circle();
s[1] = new triangle();
s[2] = new square();
s[3] = new rectangle();

for (int i = 0; i <s.Length; i++)


{
Console.WriteLine("The area is {0:0.00} ",s[i].cal_area());

}
}
}

You might also like