Home > OS >  There is data type error in code where the type is boolean instead of integer
There is data type error in code where the type is boolean instead of integer

Time:10-28

So in C# I was writing a simple code:-

To check if a coordinate is inside the circle or not

But as soon as I compiled the program below

public static void Main() 
        {
            int[] centre={0,0};
            int r=5;
            Console.Write("Coordinates: ");
            int pointX=Convert.ToInt16(Console.ReadLine());
            int pointY=Convert.ToInt16(Console.ReadLine());
            bool checkFirst=((pointX - centre[0])^2) ((pointY - centre[1])^2)== r^2;
            string check= checkFirst ? "true" : "false";
            Console.WriteLine(
                "It is {0} that {1},{2} is inside circle {3} of radius {4}.",
                check,pointX,pointY,centre,r);
            
        }

On the line of pointX - centre the error thats shown is that the subtraction is in bool and I can't square it

If I removed the centre and wrote any integer the answer is correct.

Plz help!!!

CodePudding user response:

Use Math.Pow method to calculate the powers:

static void Main(string[] args)
{
    int[] centre = { 0, 0 };
    int r = 5;
    Console.Write("Coordinates: ");
    int pointX = Convert.ToInt16(Console.ReadLine());
    int pointY = Convert.ToInt16(Console.ReadLine());
    bool checkFirst = Math.Pow(pointX - centre[0], 2)   Math.Pow(pointY - centre[1], 2) <= Math.Pow(r, 2);
    string check = checkFirst ? "true" : "false";
    Console.WriteLine($"It is {check} that {pointX},{pointY} is inside circle {centre} of radius {r}.");
}
  • Related