C# Function distance that calculates the distance between two points (x1, y1) and (x2, y2). All numbers and return values should be of type double.

/* C# Function distance that calculates the distance between two points (x1, y1) and (x2, y2), 
   All numbers and return values should be of type double. */
using System;
public class Demo
{
    public static float Distance(int x1, int y1, int x2, int y2)
    {
        float d;
        d = (float)(Math.Sqrt((Math.Pow(x2 - x1, 2)) + (Math.Pow(y2 - y1, 2))));
        return d;
    }

    public static void Main(string[] args)
    {
        int x1, x2, y1, y2;
        float r = 0;
        Console.WriteLine("Two Points (x1,x2) & (y1,y2) : \n");

        Console.WriteLine("Enter x1:");
        x1 = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Enter x2:");
        x2 = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Enter y1:");
        y1 = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Enter y2:");
        y2 = Convert.ToInt32(Console.ReadLine());

        r = Distance(x1, y1, x2, y2);
        Console.WriteLine("Distance between the 2 points is {0}", r);
        Console.ReadLine();
    }
}

Output :
Two Points (x1,x2) & (y1,y2) :

Enter x1:
1
Enter x2:
2
Enter y1:
3
Enter y2:
4
Distance between the 2 points is 1.414214



Leave a comment