🧭

If-Else Condition Quadrant Problem

Jul 1, 2024

If-Else Condition - Solving Quadrant Problem

Problem Statement

  • Write a program that asks the user to enter two integer values.
  • Determine in which quadrant these coordinates lie or if they are at the origin based on the conditions:
    • Print "origin" if both coordinates are zero.
    • Print the corresponding quadrant based on the values of x and y.

Quadrant Conditions

  • Origin Condition: Both x and y are 0.
  • Quadrant 1: x > 0 and y > 0.
  • Quadrant 2: x < 0 and y > 0.
  • Quadrant 3: x < 0 and y < 0.
  • Quadrant 4: x > 0 and y < 0.

Steps to Implement the Solution

Step 1: Import Scanner

import java.util.Scanner;

Step 2: Create Scanner Object

Scanner sc = new Scanner(System.in);

Step 3: Prompt User for Input

  • X Coordinate
System.out.println("Enter the value of x:"); int x = sc.nextInt();
  • Y Coordinate
System.out.println("Enter the value of y:"); int y = sc.nextInt();

Step 4: Check Conditions and Print Result

  • Origin Condition
if (x == 0 && y == 0) { System.out.println("origin"); }
  • Quadrant Conditions
else if (x > 0 && y > 0) { System.out.println("quadrant 1"); } else if (x < 0 && y > 0) { System.out.println("quadrant 2"); } else if (x < 0 && y < 0) { System.out.println("quadrant 3"); } else if (x > 0 && y < 0) { System.out.println("quadrant 4"); }

Testing the Code

  • Compile and Run the Code
javac FileName.java java FileName
  • Sample Inputs and Outputs
    • Input: x = 0, y = 0 -> Output: origin
    • Input: x = 5, y = 8 -> Output: quadrant 1
    • Input: x = -5, y = 8 -> Output: quadrant 2
    • Input: x = -5, y = -8 -> Output: quadrant 3
    • Input: x = 5, y = -8 -> Output: quadrant 4

Conclusion

Successfully implemented a program to determine the quadrant or origin based on user-input coordinates using simple if-else conditions.