Ruby program to read coordinate points and determine its quadrant

Ruby Example: Write a program to read coordinate points and determine its quadrant.
Submitted by Nidhi, on November 30, 2021

Problem Solution:

In this program, we will read the X, Y coordinate points from the user. Then we will determine its quadrant and print the appropriate message.

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.

# Ruby program to read coordinate points 
# and determine its quadrant

X=0.0;
Y=0.0;

print "Enter value of X: ";
X = gets.chomp.to_f;  

print "Enter value of Y: ";
Y = gets.chomp.to_f;  

if X == 0 && Y == 0
    print "Point (",X,",",Y,") lies at the origin";
elsif X > 0 && Y > 0
    print "Point (",X,",",Y,") lies in the First quadrant";
elsif X < 0 && Y > 0
    print "Point (",X,",",Y,") lies in the Second quadrant";
elsif X < 0 && Y < 0
    print "Point (",X,",",Y,") lies in the Third quadrant";
elsif X > 0 && Y < 0
    print "Point (",X,",",Y,") lies in the Fourth quadrant";
end

Output:

Enter value of X: 12
Enter value of Y: 15
Point (12,15) lies in the First quadrant

Explanation:

In the above program, we created two variables X and Y, that are initialized with 0.0. Then we read  X, Y coordinate from the user and determined its quadrant, and printed the appropriate message.

Ruby Basic Programs »





Comments and Discussions!

Load comments ↻






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