Related Articles

# Program to determine the Quadrant of a Complex number

• Last Updated : 19 May, 2021

Given a complex number in the form of the string str, the task is to determine the quadrant of the cartesian plane in which this complex number lies.

Examples:

Input: str = “1 + 1i”

Input: str = “0 + 0i”
Output: Origin

Approach:

The idea is to first find the real and imaginary parts of a complex number. Let’s say the point is (x, iy), then the following table illustrates the position of the point with respect to the coordinates:

Below is the implementation of the above approach:

## C++

 `// C++ program to determine the quadrant``// of a complex number` `#include ``using` `namespace` `std;` `// Function to determine the quadrant``// of a complex number``void` `quadrant(string s)``{``    ``int` `l = s.length();``    ``int` `i;` `    ``// Storing the index of '+'``    ``if` `(s.find(``'+'``) < l) {``        ``i = s.find(``'+'``);``    ``}` `    ``// Storing the index of '-'``    ``else` `{``        ``i = s.find(``'-'``);``    ``}` `    ``// Finding the real part``    ``// of the complex number``    ``string real = s.substr(0, i);` `    ``// Finding the imaginary part``    ``// of the complex number``    ``string imaginary = s.substr(i + 1, l - 1);` `    ``int` `x = stoi(real);``    ``int` `y = stoi(imaginary);` `    ``if` `(x > 0 and y > 0)``        ``cout << ``"Quadrant 1"``;` `    ``else` `if` `(x < 0 and y > 0)``        ``cout << ``"Quadrant 2"``;` `    ``else` `if` `(x < 0 and y < 0)``        ``cout << ``"Quadrant 3"``;` `    ``else` `if` `(x > 0 and y < 0)``        ``cout << ``"Quadrant 4"``;` `    ``else` `if` `(x == 0 and y > 0)``        ``cout << ``"Lies on positive"``             ``<< ``" Imaginary axis"``;` `    ``else` `if` `(x == 0 and y < 0)``        ``cout << ``"Lies on negative"``             ``<< ``" Imaginary axis"``;` `    ``else` `if` `(y == 0 and x < 0)``        ``cout << ``"Lies on negative"``             ``<< ``" X-axis"``;` `    ``else` `if` `(y == 0 and x > 0)``        ``cout << ``"Lies on positive"``             ``<< ``" X-axis"``;` `    ``else``        ``cout << ``"Lies on the Origin"``;``}` `// Driver code``int` `main()``{``    ``string s = ``"5+3i"``;``    ``quadrant(s);``    ``return` `0;``}`

## Java

 `// Java program to determine the quadrant``// of a complex number``import` `java.util.*;` `class` `GFG{`` ` `// Function to determine the quadrant``// of a complex number``static` `void` `quadrant(String s)``{``    ``int` `l = s.length();``    ``int` `i;`` ` `    ``// Storing the index of '+'``    ``if` `(s.contains(``"+"``)) {``        ``i = s.indexOf(``'+'``);``    ``}`` ` `    ``// Storing the index of '-'``    ``else` `{``        ``i = s.indexOf(``'-'``);``    ``}`` ` `    ``// Finding the real part``    ``// of the complex number``    ``String real = s.substring(``0``, i);`` ` `    ``// Finding the imaginary part``    ``// of the complex number``    ``String imaginary = s.substring(i + ``1``, l - ``1``);`` ` `    ``int` `x = Integer.valueOf(real);``    ``int` `y = Integer.valueOf(imaginary);`` ` `    ``if` `(x > ``0` `&& y > ``0``)``        ``System.out.print(``"Quadrant 1"``);`` ` `    ``else` `if` `(x < ``0` `&& y > ``0``)``        ``System.out.print(``"Quadrant 2"``);`` ` `    ``else` `if` `(x < ``0` `&& y < ``0``)``        ``System.out.print(``"Quadrant 3"``);`` ` `    ``else` `if` `(x > ``0` `&& y < ``0``)``        ``System.out.print(``"Quadrant 4"``);`` ` `    ``else` `if` `(x == ``0` `&& y > ``0``)``        ``System.out.print(``"Lies on positive"``            ``+ ``" Imaginary axis"``);`` ` `    ``else` `if` `(x == ``0` `&& y < ``0``)``        ``System.out.print(``"Lies on negative"``            ``+ ``" Imaginary axis"``);`` ` `    ``else` `if` `(y == ``0` `&& x < ``0``)``        ``System.out.print(``"Lies on negative"``            ``+ ``" X-axis"``);`` ` `    ``else` `if` `(y == ``0` `&& x > ``0``)``        ``System.out.print(``"Lies on positive"``            ``+ ``" X-axis"``);`` ` `    ``else``        ``System.out.print(``"Lies on the Origin"``);``}`` ` `// Driver code``public` `static` `void` `main(String[] args)``{``    ``String s = ``"5+3i"``;``    ``quadrant(s);``}``}` `// This code is contributed by Rajput-Ji`

## Python3

 `# Python 3 program to determine the quadrant``# of a complex number` `# Function to determine the quadrant``# of a complex number``def` `quadrant(s):``    ``l ``=` `len``(s)``    ``# Storing the index of '+'``    ``if` `(``'+'` `in` `s):``        ``i ``=` `s.index(``'+'``)` `    ``# Storing the index of '-'``    ``else``:``        ``i ``=` `s.index(``'-'``)` `    ``# Finding the real part``    ``# of the complex number``    ``real ``=` `s[``0``:i]` `    ``# Finding the imaginary part``    ``# of the complex number``    ``imaginary ``=` `s[i ``+` `1``:l ``-` `1``]` `    ``x ``=` `int``(real)``    ``y ``=` `int``(imaginary)` `    ``if` `(x > ``0` `and` `y > ``0``):``        ``print``(``"Quadrant 1"``)` `    ``elif``(x < ``0` `and` `y > ``0``):``        ``print``(``"Quadrant 2"``)` `    ``elif` `(x < ``0` `and` `y < ``0``):``        ``print``(``"Quadrant 3"``)` `    ``elif` `(x > ``0` `and` `y < ``0``):``        ``print``(``"Quadrant 4"``)` `    ``elif` `(x ``=``=` `0` `and` `y > ``0``):``        ``print``(``"Lies on positive"``,``"Imaginary axis"``)` `    ``elif` `(x ``=``=` `0` `and` `y < ``0``):``        ``print``(``"Lies on negative"``,``"Imaginary axis"``)` `    ``elif` `(y ``=``=` `0` `and` `x < ``0``):``        ``print``(``"Lies on negative"``,``"X-axis"``)` `    ``elif` `(y ``=``=` `0` `and` `x > ``0``):``        ``print``(``"Lies on positive"``,``"X-axis"``)` `    ``else``:``        ``print``(``"Lies on the Origin"``)` `# Driver code``if` `__name__ ``=``=` `'__main__'``:``    ``s ``=` `"5+3i"``    ``quadrant(s)``    ` `# This code is contributed by Surendra_Gangwar`

## C#

 `// C# program to determine the quadrant``// of a complex number``using` `System;` `class` `GFG{``  ` `// Function to determine the quadrant``// of a complex number``static` `void` `quadrant(String s)``{``    ``int` `l = s.Length;``    ``int` `i;``  ` `    ``// Storing the index of '+'``    ``if` `(s.Contains(``"+"``)) {``        ``i = s.IndexOf(``'+'``);``    ``}``  ` `    ``// Storing the index of '-'``    ``else` `{``        ``i = s.IndexOf(``'-'``);``    ``}``  ` `    ``// Finding the real part``    ``// of the complex number``    ``String real = s.Substring(0, i);``  ` `    ``// Finding the imaginary part``    ``// of the complex number``    ``String imaginary = s.Substring(i + 1, l - 2 - i);``  ` `    ``int` `x = Int32.Parse(real);``    ``int` `y = Int32.Parse(imaginary);``  ` `    ``if` `(x > 0 && y > 0)``        ``Console.Write(``"Quadrant 1"``);``  ` `    ``else` `if` `(x < 0 && y > 0)``        ``Console.Write(``"Quadrant 2"``);``  ` `    ``else` `if` `(x < 0 && y < 0)``        ``Console.Write(``"Quadrant 3"``);``  ` `    ``else` `if` `(x > 0 && y < 0)``        ``Console.Write(``"Quadrant 4"``);``  ` `    ``else` `if` `(x == 0 && y > 0)``        ``Console.Write(``"Lies on positive"``            ``+ ``" Imaginary axis"``);``  ` `    ``else` `if` `(x == 0 && y < 0)``        ``Console.Write(``"Lies on negative"``            ``+ ``" Imaginary axis"``);``  ` `    ``else` `if` `(y == 0 && x < 0)``        ``Console.Write(``"Lies on negative"``            ``+ ``" X-axis"``);``  ` `    ``else` `if` `(y == 0 && x > 0)``        ``Console.Write(``"Lies on positive"``            ``+ ``" X-axis"``);``  ` `    ``else``        ``Console.Write(``"Lies on the Origin"``);``}``  ` `// Driver code``public` `static` `void` `Main(String[] args)``{``    ``String s = ``"5+3i"``;``    ``quadrant(s);``}``}`` ` `// This code is contributed by sapnasingh4991`

## Javascript

 ``
Output:
`Quadrant 1`

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up