Pranav Jituri
Pranav Jituri

Reputation: 823

Output Not Right When Calculating A Point About A Circle

I am not getting correct output in this program in which I have to calculate whether a point entered is inside the circle or outside the circle or on the boundary of the circle given the centre of the circle and radius of the circle. It is sometimes giving correct answer while other times it is not giving. For example if I enter (0,0) as the centre and put radius = 10 and check the point (10,0) , it says that the point lies outside the circle. Dunno why this is happening as in other cases checked it is giving correct answer. Here is the source code to the program -

#include<stdio.h>

main()
{
    float x1,y1,x2,y2,r,z;

    printf("Please Enter The X And Y Coordinates Of The Centre Of The Circle = ");

    scanf("%f%f",&x1,&y1);

    printf("\nPlease Enter The Radius Of The Circle = ");

    scanf("%f",&r);

    printf("\nPlease Enter The Coordinates Of The Point You Want To Check");

    scanf("%f%f",&x2,&y2);

    z=x1*x1+x2*x2-2*x1*x2+y1*y1+y2*y2-2*y1*y2;

    if(z*z==r*r)
        printf("\nThe Point Entered Lies On The Boundary Of The Circle Described");

    else if(z*z>r*r)
        printf("\nThe Point Entered Is Outside The Circle Described");

    else
        printf("\nThe Point Entered Lies Inside The Circle");

}

Upvotes: 1

Views: 81

Answers (1)

haccks
haccks

Reputation: 106012

Change the condition

 if(z*z==r*r)  

to

 if(z==r*r)  

and

 else if(z*z>r*r)  

to

 else if(z>r*r)

because z already is the square of the distance between two points you have calculated.

Upvotes: 2

Related Questions