Given three integers representing the X, Y, and Z coordinates of a 3-D vector, the task is to find the magnitude of that vector.
Example:
Input: X = 1, Y = 2, Z = 2
Output: 3
Explanation: Magnitude of the vector is equal to ?9 =3Input: X = 0, Y = 4, Z = 3
Output: 5
Approach: The magnitude of a vector can be calculated by solving the equation ?(X2 + Y2 + Z2). Follow the steps below to solve the problem:
- Stores the sum of the squares of the X, Y and Z coordinates in a variable, say sum.
- Initialize a variable, say magnitude, to store the square root of sum.
- Print the value of magnitude as the required result.
Below is the implementation of the above approach:
// C++ program for the above approach #include <bits/stdc++.h> using namespace std;
// Function to calculate magnitude // of a 3 dimensional vector float vectorMagnitude( int x, int y, int z)
{ // Stores the sum of squares
// of coordinates of a vector
int sum = x * x + y * y + z * z;
// Return the magnitude
return sqrt (sum);
} // Driver Code int main()
{ int x = 1;
int y = 2;
int z = 3;
cout << vectorMagnitude(x, y, z);
return 0;
} |
// Java program for the above approach class GFG{
// Function to calculate magnitude // of a 3 dimensional vector private static double vectorMagnitude( int x, int y,
int z)
{ // Stores the sum of squares
// of coordinates of a vector
int sum = x * x + y * y + z * z;
// Return the magnitude
return Math.sqrt(sum);
} // Driver code public static void main(String[] args)
{ int x = 1 ;
int y = 2 ;
int z = 3 ;
System.out.print(vectorMagnitude(x, y, z));
} } // This code is contributed by abhinavjain194 |
# Python3 program for the above approach from math import sqrt
# Function to calculate magnitude # of a 3 dimensional vector def vectorMagnitude(x, y, z):
# Stores the sum of squares
# of coordinates of a vector
sum = x * x + y * y + z * z
# Return the magnitude
return sqrt( sum )
# Driver code x = 1
y = 2
z = 3
print (vectorMagnitude(x, y, z))
# This code is contributed by abhinavjain194 |
// C# program for the above approach using System;
class GFG{
// Function to calculate magnitude // of a 3 dimensional vector private static double vectorMagnitude( int x, int y,
int z)
{ // Stores the sum of squares
// of coordinates of a vector
int sum = x * x + y * y + z * z;
// Return the magnitude
return Math.Sqrt(sum);
} // Driver code static void Main()
{ int x = 1;
int y = 2;
int z = 3;
Console.Write(vectorMagnitude(x, y, z));
} } // This code is contributed by abhinavjain194 |
<script> // Javascript program for the above approach // Function to calculate magnitude // of a 3 dimensional vector function vectorMagnitude(x, y, z)
{ // Stores the sum of squares
// of coordinates of a vector
var sum = x * x + y * y + z * z;
// Return the magnitude
return Math.sqrt(sum);
} // Driver Code var x = 1;
var y = 2;
var z = 3;
document.write(vectorMagnitude(x, y, z)); // This code is contributed by Ankita saini </script> |
3.74166
Time Complexity: O(1)
Auxiliary Space: O(1)
Using the mathematical formula for magnitude:
Approach:
We can find the magnitude of a vector using the mathematical formula:
|V| = sqrt(X^2 + Y^2 + Z^2)
where X, Y, and Z are the components of the vector.
Import the math module for using the sqrt() function.
Get the components of the vector from the user as input using the input() function and store them in the variables X, Y, and Z.
Calculate the magnitude of the vector using the formula |V| = sqrt(X^2 + Y^2 + Z^2) and store it in the variable magnitude.
Print the magnitude of the vector using the print() function.
#include <iostream> #include <cmath> // Include the math library for sqrt() using namespace std;
int main() {
double X = 0;
double Y = 4;
double Z = 3;
// Calculate the magnitude using the formula: sqrt(X^2 + Y^2 + Z^2)
double magnitude = sqrt (X * X + Y * Y + Z * Z);
// Print the magnitude of the vector
cout << "The magnitude of the vector is: " << magnitude << endl;
return 0;
} |
import java.lang.Math;
public class VectorMagnitude {
public static void main(String[] args) {
double X = 0 ;
double Y = 4 ;
double Z = 3 ;
double magnitude = Math.sqrt(X * X + Y * Y + Z * Z);
System.out.println( "The magnitude of the vector is: " + magnitude);
}
} |
import math
X = 0
Y = 4
Z = 3
magnitude = math.sqrt(X * * 2 + Y * * 2 + Z * * 2 )
print ( "The magnitude of the vector is:" , magnitude)
|
using System;
public class VectorMagnitude
{ public static void Main( string [] args)
{
double X = 0;
double Y = 4;
double Z = 3;
double magnitude = Math.Sqrt(X * X + Y * Y + Z * Z);
Console.WriteLine( "The magnitude of the vector is: " + magnitude);
}
} |
// Define the components of the vector const X = 0; const Y = 4; const Z = 3; // Calculate the magnitude using the formula: sqrt(X^2 + Y^2 + Z^2) const magnitude = Math.sqrt(X * X + Y * Y + Z * Z); // Print the magnitude of the vector console.log( "The magnitude of the vector is: " + magnitude);
|
The magnitude of the vector is: 5.0
Time complexity: O(1)
Auxiliary Space: O(1)