ADVERTISEMENT
ADVERTISEMENT

Rust program to read coordinate points and determine its quadrant

Given coordinate points (x, y), we have to determine its quadrant using the Rust program.
Submitted by Nidhi, on October 01, 2021

Problem Solution:

Here, we will read the coordinate points from the user. Then we will determine the quadrant and print the result.

Quadrant:

There are four Quadrants based on the origin (Meeting point of the two axes).

  • In the first quadrant, both x and y have positive values.
  • In the second quadrant, x is negative and y is positive.
  • In the third quadrant, x and y are negative
  • In the fourth quadrant, x is positive and y is negative.

Consider the following graph,

coordinate points

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.

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

use std::io;

fn main() 
{
    let mut X:i32   =0;
    let mut Y:i32   =0;
    let mut res:i32 =0;
   
    let mut input1 = String::new();
    let mut input2 = String::new();
    
    println!("Enter value of X: ");
    io::stdin().read_line(&mut input1).expect("Not a valid string");
    X = input1.trim().parse().expect("Not a valid number");
    
    println!("Enter value of Y: ");
    io::stdin().read_line(&mut input2).expect("Not a valid string");
    Y = input2.trim().parse().expect("Not a valid number");
    
    if X == 0 && Y == 0
    {
        println!("Point ({0}, {1}) lies at the origin", X, Y);
    }
    else if X > 0 && Y > 0
    {
        println!("Point ({0}, {1}) lies in the First quadrant", X, Y);
    }
    else if X < 0 && Y > 0
    {
        println!("Point ({0}, {1}) lies in the Second quadrant", X, Y);
    }
    else if X < 0 && Y < 0
    {
        println!("Point ({0}, {1}) lies in the Third quadrant", X, Y);
    }
    else if X > 0 && Y < 0
    {
        println!("Point ({0}, {1}) lies in the Fourth quadrant", X, Y);
    }
}

Output:

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

RUN 2:
Enter value of X: 
-5
Enter value of Y: 
-10
Point (-5, -10) lies in the Third quadrant

Explanation:

Here, we read the value of coordinates from the user. Then we determined the quadrant and printed the result.

Rust Basic Programs »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.