Program to determine the octant of the axial plane

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 octant

Input: -4, 2, -8
Output: Point lies in 6th octant

Input: -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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


C

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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) 

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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.
?> 

chevron_right


Output:

Point lies in 1st octant
Point lies in 6th octant
Point lies in 3rd octant

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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.