C Program To Check If Point Lies Inside, Outside or On The Circle


Given the coordinates(cx, cy) of center of a circle and its radius, write a C program that will determine whether a point(x, y) lies inside the Circle, on the Circle or outside the Circle. (Hint: Use sqrt() and pow() functions)

Note:
Center Point – (cx, cy);
We need to find the position of point (x, y);

Logic To Check whether Point Lies Inside, Outside or On The Circle

First we need to calculate the distance of the point(x, y) from the center(cx, cy) of the circle. Next we need to compare the distance with the radius of the Circle.

Conditions To Determine The Position of the Point(x, y)
1. Distance is greater than radius: point is outside the Circle.
2. Distance is less than radius : point is inside the Circle.
3. Distance is equal to the radius: point is on the Circle.

Related Read:
Basic Arithmetic Operations In C
Calculate Power of a Number using pow(): C Program

Expected Output for the Input

User Input:
Enter the center point(cx, cy)
0
0
Enter radius of the circle
6
Enter the point(x, y) to check its position
3
3

Output:
Point (3.00, 3.00) is inside the Circle

Formula To Calculate Distance from point(x, y) To Center Point (cx, cy)

distance = sqrt( pow( (x – cx), 2 ) + pow( (y – cy), 2) );

Note: sqrt() and pow() are builtin method present in library file or header file math.h

Video Tutorial: C Program To Check If Point Lies Inside, Outside or On The Circle


[youtube https://www.youtube.com/watch?v=cHsl0baVz7A]

YouTube Link: https://www.youtube.com/watch?v=cHsl0baVz7A [Watch the Video In Full Screen.]

Source Code: C Program To Check If Point Lies Inside, Outside or On The Circle

#include < stdio.h >
#include < math.h >

int main()
{
    float cx, cy, radius, x, y, distance;

    printf("Enter the center point(cx, cy)\n");
    scanf("%f%f", &cx, &cy);

    printf("Enter radius of the circle\n");
    scanf("%f", &radius);

    printf("Enter the point(x, y) to check its position\n");
    scanf("%f%f", &x, &y);

    distance = sqrt( pow( (x - cx), 2 ) + pow( (y - cy), 2 ) );

    if(distance < radius)
    {
        printf("Point (%0.2f, %0.2f) is inside the Circle\n", x, y);
    }
    else if(distance > radius)
    {
        printf("Point (%0.2f, %0.2f) is outside the Circle\n", x, y);
    }
    else
    {
        printf("Point (%0.2f, %0.2f) is on the Circle\n", x, y);
    }

    return 0;
}

Output 1:
Enter the center point(cx, cy)
0
0
Enter radius of the circle
6
Enter the point(x, y) to check its position
2
2
Point (2.00, 2.00) is inside the Circle

Output 2:
Enter the center point(cx, cy)
0
0
Enter radius of the circle
6
Enter the point(x, y) to check its position
12
6
Point (12.00, 6.00) is outside the Circle

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Leave a Reply

Your email address will not be published. Required fields are marked *