Quadrant Finder for Coordinate Points in C#
This C# program accepts a coordinate point (x, y) in an XY coordinate system and determines in which quadrant the point lies. By evaluating the signs of the x and y values, the program identifies whether the point is in the first, second, third, or fourth quadrant, or if it lies on one of the axes. This task introduces coordinate systems and conditional logic in C#.
On this page
Source Code 💻
using System;
class Program
{
static void Main()
{
// Prompt user to enter the X and Y coordinates
Console.Write("Enter the X coordinate: ");
int x = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the Y coordinate: ");
int y = Convert.ToInt32(Console.ReadLine());
// Determine which quadrant the point lies in
if (x > 0 && y > 0)
{
Console.WriteLine($"The point ({x}, {y}) lies in the First Quadrant.");
}
else if (x < 0 && y > 0)
{
Console.WriteLine($"The point ({x}, {y}) lies in the Second Quadrant.");
}
else if (x < 0 && y < 0)
{
Console.WriteLine($"The point ({x}, {y}) lies in the Third Quadrant.");
}
else if (x > 0 && y < 0)
{
Console.WriteLine($"The point ({x}, {y}) lies in the Fourth Quadrant.");
}
else if (x == 0 && y != 0)
{
Console.WriteLine($"The point ({x}, {y}) lies on the Y-axis.");
}
else if (y == 0 && x != 0)
{
Console.WriteLine($"The point ({x}, {y}) lies on the X-axis.");
}
else
{
Console.WriteLine($"The point ({x}, {y}) is at the origin.");
}
}
}
Explanation
- The program prompts the user to input the X and Y coordinates.
- It then uses if-else statements to check the following:
- First Quadrant: X > 0 and Y > 0
- Second Quadrant: X < 0 and Y > 0
- Third Quadrant: X < 0 and Y < 0
- Fourth Quadrant: X > 0 and Y < 0
- If either X or Y is 0, the point lies on one of the axes.
- If both X and Y are 0, the point is at the origin.
- Based on the conditions, the program prints the appropriate quadrant, axis, or origin.
Example
-
Input: X = 5, Y = -3
-
Output: The point (5, -3) lies in the Fourth Quadrant.
-
Input: X = 0, Y = 7
-
Output: The point (0, 7) lies on the Y-axis.
-
Input: X = 0, Y = 0
-
Output: The point (0, 0) is at the origin.
🔚 end of document 🔚