Java program to read coordinate points and determine its quadrant

Given the values of coordinates, we have to read coordinate points and determine its quadrant.
Submitted by Nidhi, on February 28, 2022

Problem Solution:

In this program, we will read X, Y coordinate points from the user and determine the quadrant.

Program/Source Code:

The source code to read coordinate points and determine their quadrant is given below. The given program is compiled and executed successfully.

// Java program to read coordinate points 
// and determine its quadrant

import java.util.Scanner;

public class Main {
  static int determineQuadrant(int X, int Y) {
    if (X == 0 && Y == 0)
      return 0;
    else if (X > 0 && Y > 0)
      return 1;
    else if (X < 0 && Y > 0)
      return 2;
    else if (X < 0 && Y < 0)
      return 3;
    else if (X > 0 && Y < 0)
      return 4;
    return -1;
  }

  public static void main(String[] args) {
    Scanner SC = new Scanner(System.in);

    int X;
    int Y;

    int ret = 0;

    System.out.printf("Enter the value of X: ");
    X = SC.nextInt();

    System.out.printf("Enter the value of Y: ");
    Y = SC.nextInt();

    ret = determineQuadrant(X, Y);

    if (ret == 0)
      System.out.printf("Point (%d, %d) lies at the origin\n", X, Y);
    else if (ret == 1)
      System.out.printf("Point (%d, %d) lies in the First quadrant\n", X, Y);
    else if (ret == 2)
      System.out.printf("Point (%d, %d) lies in the Second quadrant\n", X, Y);
    else if (ret == 3)
      System.out.printf("Point (%d, %d) lies in the Third quadrant\n", X, Y);
    else if (ret == 4)
      System.out.printf("Point (%d, %d) lies in the Fourth quadrant\n", X, Y);
  }
}

Output:

Enter the value of X: 20
Enter the value of Y: 30
Point (20, 30) lies in the First quadrant

Explanation:

In the above program, we imported the "java.util.Scanner" package to read input from the user. And, created a public class Main. It contains two static methods determineQuadrant() and main().

The determineQuadrant() method is used to determine the quadrant based on given coordinates.

The main() method is an entry point for the program. Here, we read coordinates X, Y from the user using the Scanner class. Then we determined the quadrant and printed the result.

Java Basic Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.