Program to find the Magnitude of a Vector - GeeksforGeeks

19 Apr, 2021
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 = 2Output: 3Explanation: Magnitude of the vector is equal to √9 =3
Input: X = 0, Y = 4, Z = 3Output: 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
Java
Python3
C#
Javascript
// C++ program for the above approach#include <bits/stdc++.h>using namespace std; // Function to calculate magnitude// of a 3 dimensional vectorfloat 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 Codeint main(){    int x = 1;    int y = 2;    int z = 3;       cout << vectorMagnitude(x, y, z);       return 0;}
// Java program for the above approachclass GFG{     // Function to calculate magnitude// of a 3 dimensional vectorprivate 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 codepublic 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 approachfrom math import sqrt # Function to calculate magnitude# of a 3 dimensional vectordef 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 = 1y = 2z = 3 print(vectorMagnitude(x, y, z)) # This code is contributed by abhinavjain194
// C# program for the above approachusing System; class GFG{     // Function to calculate magnitude// of a 3 dimensional vectorprivate 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 codestatic 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 vectorfunction 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 Codevar 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)
 
abhinavjain194
ankita_saini
Mathematical
School Programming
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Find all factors of a natural number | Set 1
Check if a number is Palindrome
Program to print prime numbers from 1 to N.
Fizz Buzz Implementation
Program to multiply two matrices
Python Dictionary
Arrays in C/C++
Reverse a string in Java
Inheritance in C++
Constructors in C++