Given 3 coordinates x, y and z, the task is to determine the octant of the axial plane.
Examples:
Input: 2, 3, 4
Output: Point lies in 1st octantInput: -4, 2, -8
Output: Point lies in 6th octantInput: -6, -2, 8
Output: Point lies in 3rd octant
Approach: Given below are the conditions which need to be checked in order to determine the octant of the axial plane.
- Check if x >= 0 and y >= 0 and z >= 0, then Point lies in 1st octant.
- Check x < 0 and y >= 0 and z >= 0, then Point lies in 2nd octant.
- Check if x < 0 and y < 0 and z >= 0, then Point lies in 3rd octant.
- Check if x >= 0 and y < 0 and z >= 0, then Point lies in 4th octant.
- Check if x >= 0 and y >= 0 and z < 0, then Point lies in 5th octant.
- Check if x < 0 and y >= 0 and z < 0, then Point lies in 6th octant.
- Check if x < 0 and y < 0 and z < 0, then Point lies in 7th octant.
- Check if x >= 0 and y < 0 and z < 0, then Point lies in 8th octant.
Below is the implementation of the above approach:
C++
// C++ program to print octant // of a given point. #include <bits/stdc++.h> #include<math.h> using namespace std; // Function to print octant void octant( float x, float y, float z) { if (x >= 0 && y >= 0 && z >= 0) cout << "Point lies in 1st octant\n" ; else if (x < 0 && y >= 0 && z >= 0) cout << "Point lies in 2nd octant\n" ; else if (x < 0 && y < 0 && z >= 0) cout << "Point lies in 3rd octant\n" ; else if (x >= 0 && y < 0 && z >= 0) cout << "Point lies in 4th octant\n" ; else if (x >= 0 && y >= 0 && z < 0) cout << "Point lies in 5th octant\n" ; else if (x < 0 && y >= 0 && z < 0) cout << "Point lies in 6th octant\n" ; else if (x < 0 && y < 0 && z < 0) cout << "Point lies in 7th octant\n" ; else if (x >= 0 && y < 0 && z < 0) cout << "Point lies in 8th octant\n" ; } // Driver Code int main() { float x = 2, y = 3, z = 4; octant(x, y, z) ; x = -4, y = 2, z = -8; octant(x, y, z); x = -6, y = -2, z = 8; octant(x, y, z); return 0; } // This code is contributed // by Amber_Saxena. |
C
// C program to print octant // of a given point. #include <stdio.h> // Function to print octant void octant( float x, float y, float z) { if (x >= 0 && y >= 0 && z >= 0) printf ( "Point lies in 1st octant\n" ); else if (x < 0 && y >= 0 && z >= 0) printf ( "Point lies in 2nd octant\n" ); else if (x < 0 && y < 0 && z >= 0) printf ( "Point lies in 3rd octant\n" ); else if (x >= 0 && y < 0 && z >= 0) printf ( "Point lies in 4th octant\n" ); else if (x >= 0 && y >= 0 && z < 0) printf ( "Point lies in 5th octant\n" ); else if (x < 0 && y >= 0 && z < 0) printf ( "Point lies in 6th octant\n" ); else if (x < 0 && y < 0 && z < 0) printf ( "Point lies in 7th octant\n" ); else if (x >= 0 && y < 0 && z < 0) printf ( "Point lies in 8th octant\n" ); } // Driver Code int main() { float x = 2, y = 3, z = 4; octant(x, y, z) ; x = -4, y = 2, z = -8; octant(x, y, z); x = -6, y = -2, z = 8; octant(x, y, z); } // This code is contributed // by Amber_Saxena. |
Java
// Java program to print octant // of a given point. import java.util.*; class solution { // Function to print octant static void octant( float x, float y, float z) { if (x >= 0 && y >= 0 && z >= 0 ) System.out.println( "Point lies in 1st octant" ); else if (x < 0 && y >= 0 && z >= 0 ) System.out.println( "Point lies in 2nd octant" ); else if (x < 0 && y < 0 && z >= 0 ) System.out.println( "Point lies in 3rd octant" ); else if (x >= 0 && y < 0 && z >= 0 ) System.out.println( "Point lies in 4th octant" ); else if (x >= 0 && y >= 0 && z < 0 ) System.out.println( "Point lies in 5th octant" ); else if (x < 0 && y >= 0 && z < 0 ) System.out.println( "Point lies in 6th octant" ); else if (x < 0 && y < 0 && z < 0 ) System.out.println( "Point lies in 7th octant" ); else if (x >= 0 && y < 0 && z < 0 ) System.out.println( "Point lies in 8th octant" ); } // Driver Code public static void main(String args[]) { float x = 2 , y = 3 , z = 4 ; octant(x, y, z) ; x = - 4 ; y = 2 ; z = - 8 ; octant(x, y, z); x = - 6 ; y = - 2 ; z = 8 ; octant(x, y, z); } } //This code is contributed by Surendra_Gangwar |
Python
# Python program to print octant of a # given point. # Function to print octant def octant(x, y, z): if x > = 0 and y > = 0 and z > = 0 : print "Point lies in 1st octant" elif x < 0 and y > = 0 and z > = 0 : print "Point lies in 2nd octant" elif x < 0 and y < 0 and z > = 0 : print "Point lies in 3rd octant" elif x > = 0 and y < 0 and z > = 0 : print "Point lies in 4th octant" elif x > = 0 and y > = 0 and z < 0 : print "Point lies in 5th octant" elif x < 0 and y > = 0 and z < 0 : print "Point lies in 6th octant" elif x < 0 and y < 0 and z < 0 : print "Point lies in 7th octant" elif x > = 0 and y < 0 and z < 0 : print "Point lies in 8th octant" # Driver Code x, y, z = 2 , 3 , 4 octant(x, y, z) x, y, z = - 4 , 2 , - 8 octant(x, y, z) x, y, z = - 6 , - 2 , 8 octant(x, y, z) |
C#
// C# program to print octant // of a given point. using System; class GFG { // Function to print octant static void octant( float x, float y, float z) { if (x >= 0 && y >= 0 && z >= 0) Console.WriteLine( "Point lies in 1st octant" ); else if (x < 0 && y >= 0 && z >= 0) Console.WriteLine( "Point lies in 2nd octant" ); else if (x < 0 && y < 0 && z >= 0) Console.WriteLine( "Point lies in 3rd octant" ); else if (x >= 0 && y < 0 && z >= 0) Console.WriteLine( "Point lies in 4th octant" ); else if (x >= 0 && y >= 0 && z < 0) Console.WriteLine( "Point lies in 5th octant" ); else if (x < 0 && y >= 0 && z < 0) Console.WriteLine( "Point lies in 6th octant" ); else if (x < 0 && y < 0 && z < 0) Console.WriteLine( "Point lies in 7th octant" ); else if (x >= 0 && y < 0 && z < 0) Console.WriteLine( "Point lies in 8th octant" ); } // Driver Code static public void Main () { float x = 2, y = 3, z = 4; octant(x, y, z) ; x = -4; y = 2; z = -8; octant(x, y, z); x = -6; y = -2; z = 8; octant(x, y, z); } } // This code is contributed by ajit |
PHP
<?php // PHP program to print octant // of a given point. // Function to print octant function octant( $x , $y , $z ) { if ( $x >= 0 && $y >= 0 && $z >= 0) echo "Point lies in 1st octant\n" ; else if ( $x < 0 && $y >= 0 && $z >= 0) echo "Point lies in 2nd octant\n" ; else if ( $x < 0 && $y < 0 && $z >= 0) echo "Point lies in 3rd octant\n" ; else if ( $x >= 0 && $y < 0 && $z >= 0) echo "Point lies in 4th octant\n" ; else if ( $x >= 0 && $y >= 0 && $z < 0) echo "Point lies in 5th octant\n" ; else if ( $x < 0 && $y >= 0 && $z < 0) echo "Point lies in 6th octant\n" ; else if ( $x < 0 && $y < 0 && $z < 0) echo "Point lies in 7th octant\n" ; else if ( $x >= 0 && $y < 0 && $z < 0) echo "Point lies in 8th octant\n" ; } // Driver Code $x = 2; $y = 3; $z = 4; octant( $x , $y , $z ) ; $x = -4; $y = 2; $z = -8; octant( $x , $y , $z ); $x = -6; $y = -2; $z = 8; octant( $x , $y , $z ); // This code is contributed // by Amber_Saxena. ?> |
Javascript
<script> // Javascript program to print octant // of a given point. // Function to print octant function octant( x, y, z) { if (x >= 0 && y >= 0 && z >= 0) document.write( "Point lies in 1st octant" + "</br>" ); else if (x < 0 && y >= 0 && z >= 0) document.write( "Point lies in 2nd octant" + "</br>" ); else if (x < 0 && y < 0 && z >= 0) document.write( "Point lies in 3rd octant" + "</br>" ); else if (x >= 0 && y < 0 && z >= 0) document.write( "Point lies in 4th octant" + "</br>" ); else if (x >= 0 && y >= 0 && z < 0) document.write( "Point lies in 5th octant" + "</br>" ); else if (x < 0 && y >= 0 && z < 0) document.write( "Point lies in 6th octant" + "</br>" ); else if (x < 0 && y < 0 && z < 0) document.write( "Point lies in 7th octant" + "</br>" ); else if (x >= 0 && y < 0 && z < 0) document.write( "Point lies in 8th octant" + "</br>" ); } // driver code let x = 2, y = 3, z = 4; octant(x, y, z) ; x = -4, y = 2, z = -8; octant(x, y, z); x = -6, y = -2, z = 8; octant(x, y, z); // This code is contributed by jana_sayantan. </script> |
Point lies in 1st octant Point lies in 6th octant Point lies in 3rd octant
Time complexity: O(1)
Auxiliary space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!