Program to find the Magnitude of a Vector
Last Updated :
11 Nov, 2023
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 =3
Input: 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++14
#include <bits/stdc++.h>
using namespace std;
float vectorMagnitude( int x, int y, int z)
{
int sum = x * x + y * y + z * z;
return sqrt (sum);
}
int main()
{
int x = 1;
int y = 2;
int z = 3;
cout << vectorMagnitude(x, y, z);
return 0;
}
|
Java
class GFG{
private static double vectorMagnitude( int x, int y,
int z)
{
int sum = x * x + y * y + z * z;
return Math.sqrt(sum);
}
public static void main(String[] args)
{
int x = 1 ;
int y = 2 ;
int z = 3 ;
System.out.print(vectorMagnitude(x, y, z));
}
}
|
Python3
from math import sqrt
def vectorMagnitude(x, y, z):
sum = x * x + y * y + z * z
return sqrt( sum )
x = 1
y = 2
z = 3
print (vectorMagnitude(x, y, z))
|
C#
using System;
class GFG{
private static double vectorMagnitude( int x, int y,
int z)
{
int sum = x * x + y * y + z * z;
return Math.Sqrt(sum);
}
static void Main()
{
int x = 1;
int y = 2;
int z = 3;
Console.Write(vectorMagnitude(x, y, z));
}
}
|
Javascript
<script>
function vectorMagnitude(x, y, z)
{
var sum = x * x + y * y + z * z;
return Math.sqrt(sum);
}
var x = 1;
var y = 2;
var z = 3;
document.write(vectorMagnitude(x, y, z));
</script>
|
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.
C++
#include <iostream>
#include <cmath> // Include the math library for sqrt()
using namespace std;
int main() {
double X = 0;
double Y = 4;
double Z = 3;
double magnitude = sqrt (X * X + Y * Y + Z * Z);
cout << "The magnitude of the vector is: " << magnitude << endl;
return 0;
}
|
Java
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);
}
}
|
Python3
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)
|
C#
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);
}
}
|
Javascript
const X = 0;
const Y = 4;
const Z = 3;
const magnitude = Math.sqrt(X * X + Y * Y + Z * Z);
console.log( "The magnitude of the vector is: " + magnitude);
|
Output
The magnitude of the vector is: 5.0
Time complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...