Write a C program that asks the user to enter a positive integer N, and a real value R representing the radius of a circle C of center O(0,0).

The program should then prompt the user to enter a sequence of points’ coordinates (x and y) until the number of points inside C is equal to N. The program should display the coordinates of the farthest point from the center O(0,0) (among the entered points).

Note that:

  1. The distance between a point M(x,y) and the point O(0,0) is calculated using the formula $$\sqrt{x^2+y^2}$$
  2. $$sqrt(t)$$ of the library math.h gives the value of $$\sqrt{t}$$

Difficulty level
Video recording
This exercise is mostly suitable for students
#include <stdio.h>
#include <math.h>

int main()
{
	int N, count;
	double R , x, y, dist , maxdis, xmax, ymax;
    
	do{
		printf("Enter N: ");
		scanf("%d",&N);
	}while(N<=0);
    
	do{
		printf("Enter R: ");
		scanf("%lf",&R);
	}while(R<=0);
    
	count = 0;
	maxdis = 0; 
	do
	{
		printf("Enter x and y: ");
		scanf("%lf %lf", &x, &y);
        
		dist = sqrt(pow(x,2) + pow(y,2)) ;
		if(dist < R)
			count++;
		if(dist > maxdis)
		{
			maxdis = dist  ;
			xmax = x;
			ymax =y;
		}
        
	}while(count < N);
    
	printf("Farthest point = (%.2lf,%.2lf)\n",xmax, ymax);
    
	return 0;
}

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Binary search trees in levels