Check if the given vectors are at equilibrium or not

Given the x, y and z coordinates of three vectors, the task is to check if they are at equilibrium or not.

Examples:

Input: x1 = -2, y1 = 1, z1 = 0, x2 = 5, y2 = 0, z2 = 5, x3 = -3, y3 = -1, z3 = -5
Output: The vectors are at equilibrium.

Input: x1 = 2, y1 = -17, z1 = 0, x2 = 5, y2 = 1, z2 = -5, x3 = 4, y3 = 2, z3 = -4
Output: The vectors are not at equilibrium.



When Three vectors are at equilibrium

Approach: Three vectors are at equilibrium when the results of those three vectors is a Null vector, i.e. it has no magnitude and direction. Resultant of three vectors is equal to the vector sum of the vectors. The resultant vector is Null when, ∑x = 0, ∑y = 0 and ∑ z = 0. Thus we can say that when the said condition satisfies then the vectors are at equilibrium and otherwise not.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to check the equlibrium of three vectors
#include <bits/stdc++.h>
using namespace std;
  
// Function to check the equlibrium of three vectors
bool checkEqulibrium(int x1, int y1, int z1, int x2, int y2,
                     int z2, int x3, int y3, int z3)
{
  
    // summing the x coordinates
    int resx = x1 + x2 + x3;
  
    // summing the y coordinates
    int resy = y1 + y2 + y3;
  
    // summing the z coordinates
    int resz = z1 + z2 + z3;
  
    // Checking the condition for equlibrium
    if (resx == 0 and resy == 0 and resz == 0)
        return true;
    else
        return false;
}
  
// Driver code
int main()
{
    int x1 = -2, y1 = -7, z1 = -9, x2 = 5, y2 = -14, z2 = 14,
        x3 = -3, y3 = 21, z3 = -5;
  
    // Checking for equlibrium
    if (checkEqulibrium(x1, y1, z1, x2, y2, z2, x3, y3, z3))
        cout << "The vectors are at equilibrium.";
    else
        cout << "The vectors are not at equilibrium.";
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to check the equilibrium of three vectors
  
public class GFG {
  
    // Function to check the equilibrium of three vectors
    static boolean checkEqulibrium(int x1, int y1, int z1, int x2, int y2,
                        int z2, int x3, int y3, int z3)
    {
  
        // summing the x coordinates
        int resx = x1 + x2 + x3;
  
        // summing the y coordinates
        int resy = y1 + y2 + y3;
  
        // summing the z coordinates
        int resz = z1 + z2 + z3;
  
        // Checking the condition for equilibrium
        if (resx == 0 & resy == 0 & resz == 0)
            return true;
        else
            return false;
    }
  
  
    // Driver code
    public static void main(String args[])
    {
        int x1 = -2, y1 = -7, z1 = -9, x2 = 5, y2 = -14,
              z2 = 14, x3 = -3, y3 = 21, z3 = -5;
  
                // Checking for equlibrium
                if (checkEqulibrium(x1, y1, z1, x2, y2,
                                    z2, x3, y3, z3))
                    System.out.println("The vectors are at equilibrium.");
                else
                    System.out.println("The vectors are not at equilibrium.");
  
    }
      
}
// This code is contributed by ANKITRAI1

chevron_right


Python 3

# Python 3 program to check the
# equlibrium of three vectors

# Function to check the equlibrium
# of three vectors
def checkEqulibrium(x1, y1, z1, x2, y2,
z2, x3, y3, z3) :

# summing the x coordinates
resx = x1 + x2 + x3

# summing the y coordinates
resy = y1 + y2 + y3

# summing the z coordinates
resz = z1 + z2 + z3

# Checking the condition for equlibrium
if (resx == 0 and resy == 0 and
resz == 0):
return True
else:
return False

# Driver code
x1 = -2; y1 = -7; z1 = -9
x2 = 5; y2 = -14; z2 = 14
x3 = -3; y3 = 21; z3 = -5

# Checking for equlibrium
if (checkEqulibrium(x1, y1, z1,
x2, y2, z2,
x3, y3, z3)):
print(“The vectors are at equilibrium.”)

else:
print(“The vectors are not at equilibrium.”)

# This code is contributed
# by Akanksha Rai

C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to check the equilibrium 
// of three vectors
class GFG 
{
  
// Function to check the equilibrium 
// of three vectors
static bool checkEqulibrium(int x1, int y1, int z1, 
                            int x2, int y2,    int z2, 
                            int x3, int y3, int z3)
{
  
    // summing the x coordinates
    int resx = x1 + x2 + x3;
  
    // summing the y coordinates
    int resy = y1 + y2 + y3;
  
    // summing the z coordinates
    int resz = z1 + z2 + z3;
  
    // Checking the condition for equilibrium
    if (resx == 0 & resy == 0 & resz == 0)
        return true;
    else
        return false;
}
  
// Driver code
public static void Main()
{
    int x1 = -2, y1 = -7, z1 = -9, 
        x2 = 5, y2 = -14, z2 = 14, 
        x3 = -3, y3 = 21, z3 = -5;
  
    // Checking for equlibrium
    if (checkEqulibrium(x1, y1, z1, x2, y2,
                        z2, x3, y3, z3))
        System.Console.WriteLine("The vectors are "
                                  "at equilibrium.");
    else
        System.Console.WriteLine("The vectors are not "
                                      "at equilibrium.");
  
}
}
  
// This code is contributed by mits

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to check the equlibrium 
// of three vectors 
  
// Function to check the equlibrium 
// of three vectors 
function checkEqulibrium($x1, $y1, $z1
                         $x2, $y2, $z2,
                         $x3, $y3, $z3
  
    // summing the x coordinates 
    $resx = $x1 + $x2 + $x3
  
    // summing the y coordinates 
    $resy = $y1 + $y2 + $y3
  
    // summing the z coordinates 
    $resz = $z1 + $z2 + $z3
  
    // Checking the condition
    // for equlibrium 
    if ($resx == 0 and $resy == 0 and 
                       $resz == 0) 
        return true; 
    else
        return false; 
  
// Driver code 
$x1 = -2; $y1 = -7; $z1 = -9;
$x2 = 5; $y2 = -14; $z2 = 14;
$x3 = -3; $y3 = 21; $z3 = -5; 
  
// Checking for equlibrium 
if (checkEqulibrium($x1, $y1, $z1
                    $x2, $y2, $z2
                    $x3, $y3, $z3)) 
    echo "The vectors are at equilibrium."
else
    echo "The vectors are not at equilibrium."
  
// This code is contributed by akt_mit
?>

chevron_right


Output:

The vectors are at equilibrium.


My Personal Notes arrow_drop_up

Coder Machine Learner Social Activist Vocalist

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.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.