Monday 18 May 2020

TO CHECK WHETHER A POINT IS WITHIN THE CIRCLE OR NOT USING JAVA LANGUAGE

Q-40-  Write a java program that prompts the user to enter a point (x, y) and checks whether the point is within the circle centered at (0, 0) with radius 10. For example, (4, 5) is inside the circle and (9, 9) is outside the circle, 

 (Hint: A point is in the circle if its distance to (0, 0) is less than or equal to 10. The formula for computing the distance is √(𝑥2 − 𝑥1)2 + (𝑦2 − 𝑦1)2 .Test your program to cover all cases.) 

INPUT :

import java.util.Scanner;

public class NCM {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
double x1=0;
double y1=0;
double radius=10;
double x2 ,y2;
System.out.println("Enter The Point :");
x2=sc.nextDouble();
y2=sc.nextDouble();
double r=(x2-x1)*(x2-x1)+(y2-y1)*(y2-y1);
double distance=Math.pow(r, 0.5);
if(distance<=radius)
{
System.out.println("Point ("+x2+", "+y2+") Is In The Circle .");
}
else
{
System.out.println("Point ("+x2+", "+y2+") Is Not In The Circle .");
}

}

}

OUTPUT :

Enter The Point :
4 5
Point (4.0, 5.0) Is In The Circle .

Enter The Point :
9 9
Point (9.0, 9.0) Is Not In The Circle .