Foros aprenderaprogramar.com

Aprender a programar => C, C++, C#, Java, Visual Basic, HTML, PHP, CSS, Javascript, Ajax, Joomla, MySql y más => Mensaje iniciado por: Dimitar Stefanov en 18 de Julio 2016, 20:48

Título: C# Programa que determine si un punto está dentro o fuera de una cicunferencia
Publicado por: Dimitar Stefanov en 18 de Julio 2016, 20:48
Ejercicio:

Citar
Write a program that reads the coordinates of a point x and y and using an expression checks if given point (x, y) is inside a circle K({0, 0}, 2) - the center has coordinates (0, 0) and the circle has radius 2. The program should then print "yes DISTANCE" if the point is inside the circle or "no DISTANCE" if the point is outside the circle.

    In place of DISTANCE print the distance from the beginning of the coordinate system - (0, 0) - to the given point.

El código:

Código: [Seleccionar]
using System;

class Program {
static void Main(){
float x;
float y;
float radio = 2.0f;
Console.Write("Introduce la X: ");
x = float.Parse(Console.ReadLine());
Console.Write("Introduce la Y: ");
y = float.Parse(Console.ReadLine());

if(Math.Sqrt((Math.Pow((x-0),2))+(Math.Pow((y-0),2)))<=radio){
Console.WriteLine("Entra, distancia: "+ (Math.Sqrt(Math.Pow((x-0), 2) + Math.Pow((y-0), 2))).ToString("f2"));
}else {
Console.WriteLine("NO entra, distancia: " + (Math.Sqrt(Math.Pow((x-0), 2) + Math.Pow((y-0), 2))).ToString("f2"));
}
}
}

Saludos.