Related Articles

# Program to calculate angle between two N-Dimensional vectors

• Last Updated : 26 Apr, 2021

Given an array arr[] consisting of magnitudes of two N-Dimensional vectors A and B, the task is to find the angle between the two vectors.

Examples:

Input: arr[] = {-0.5, -2, 1}, brr[] = {-1, -1, -0.3}
Output: 0.845289
Explanation:
Placing the values in the formula , the required result is obtained.

Input: arr[] = {1, -2, 3}, brr[] = {2, 3, -1}
Output: -0.5

Approach: The idea is based on the mathematical formula of finding the dot product of two vectors and dividing it by the product of the magnitude of vectors A, B.

Formula:

Considering the two vectors to be separated by angle θ. the dot product of the two vectors is given by the equation: Therefore, Below is the implementation of the above approach:

## C++

 // C++ program for the above approach #include using namespace std; // Function to find the magnitude// of the given vectordouble magnitude(double arr[], int N){    // Stores the final magnitude    double magnitude = 0;     // Traverse the array    for (int i = 0; i < N; i++)        magnitude += arr[i] * arr[i];     // Return square root of magnitude    return sqrt(magnitude);} // Function to find the dot// product of two vectorsdouble dotProduct(double arr[],                  double brr[], int N){    // Stores dot product    double product = 0;     // Traverse the array    for (int i = 0; i < N; i++)        product = product + arr[i] * brr[i];     // Return the product    return product;} void angleBetweenVectors(double arr[],                         double brr[], int N){    // Stores dot product of two vectors    double dotProductOfVectors        = dotProduct(arr, brr, N);     // Stores magnitude of vector A    double magnitudeOfA        = magnitude(arr, N);     // Stores magnitude of vector B    double magnitudeOfB        = magnitude(brr, N);     // Stores angle between given vectors    double angle = dotProductOfVectors                   / (magnitudeOfA * magnitudeOfB);     // Print the angle    cout << angle;} // Driver Codeint main(){    // Given magnitude arrays    double arr[] = { -0.5, -2, 1 };    double brr[] = { -1, -1, 0.3 };     // Size of the array    int N = sizeof(arr) / sizeof(arr);     // Function call to find the    // angle between two vectors    angleBetweenVectors(arr, brr, N);     return 0;}

## Java

 // Java program for the above approachclass GFG{ // Function to find the magnitude// of the given vectorstatic double magnitude(double arr[], int N){         // Stores the final magnitude    double magnitude = 0;     // Traverse the array    for(int i = 0; i < N; i++)        magnitude += arr[i] * arr[i];     // Return square root of magnitude    return Math.sqrt(magnitude);} // Function to find the dot// product of two vectorsstatic double dotProduct(double[] arr,                         double[] brr, int N){         // Stores dot product    double product = 0;     // Traverse the array    for(int i = 0; i < N; i++)        product = product + arr[i] * brr[i];     // Return the product    return product;} static void angleBetweenVectors(double[] arr,                                double[] brr, int N){         // Stores dot product of two vectors    double dotProductOfVectors = dotProduct(arr, brr, N);     // Stores magnitude of vector A    double magnitudeOfA = magnitude(arr, N);     // Stores magnitude of vector B    double magnitudeOfB = magnitude(brr, N);     // Stores angle between given vectors    double angle = dotProductOfVectors /                   (magnitudeOfA * magnitudeOfB);     // Print the angle    System.out.println(angle);} // Driver Codepublic static void main(String[] args){         // Given magnitude arrays    double[] arr = { -0.5, -2, 1 };    double[] brr = { -1, -1, 0.3 };     // Size of the array    int N = arr.length;     // Function call to find the    // angle between two vectors    angleBetweenVectors(arr, brr, N);}} // This code is contributed by user_qa7r

## Python3

 # Python3 program for the above approachimport math # Function to find the magnitude# of the given vectordef magnitude(arr, N):     # Stores the final magnitude    magnitude = 0     # Traverse the array    for i in range(N):        magnitude += arr[i] * arr[i]     # Return square root of magnitude    return math.sqrt(magnitude) # Function to find the dot# product of two vectors  def dotProduct(arr, brr, N):     # Stores dot product    product = 0     # Traverse the array    for i in range(N):        product = product + arr[i] * brr[i]     # Return the product    return product  def angleBetweenVectors(arr, brr, N):     # Stores dot product of two vectors    dotProductOfVectors = dotProduct(arr, brr, N)     # Stores magnitude of vector A    magnitudeOfA = magnitude(arr, N)     # Stores magnitude of vector B    magnitudeOfB = magnitude(brr, N)     # Stores angle between given vectors    angle = (dotProductOfVectors             / (magnitudeOfA * magnitudeOfB))     # Print the angle    print('%.5f'%angle) # Driver Codeif __name__ == "__main__":     # Given magnitude arrays    arr = [-0.5, -2, 1]    brr = [-1, -1, 0.3]     # Size of the array    N = len(arr)     # Function call to find the    # angle between two vectors    angleBetweenVectors(arr, brr, N)     # This code is contributed by ukasp.

## C#

 // C# program for the above approachusing System;using System.Collections.Generic; class GFG{  // Function to find the magnitude// of the given vectorstatic double magnitude(double []arr, int N){         // Stores the final magnitude    double magnitude = 0;     // Traverse the array    for(int i = 0; i < N; i++)        magnitude += arr[i] * arr[i];     // Return square root of magnitude    return Math.Sqrt(magnitude);} // Function to find the dot// product of two vectorsstatic double dotProduct(double []arr,                         double []brr, int N){         // Stores dot product    double product = 0;     // Traverse the array    for(int i = 0; i < N; i++)        product = product + arr[i] * brr[i];     // Return the product    return product;} static void angleBetweenVectors(double []arr,                                double []brr, int N){         // Stores dot product of two vectors    double dotProductOfVectors = dotProduct(arr, brr, N);     // Stores magnitude of vector A    double magnitudeOfA = magnitude(arr, N);     // Stores magnitude of vector B    double magnitudeOfB = magnitude(brr, N);     // Stores angle between given vectors    double angle = dotProductOfVectors /     (magnitudeOfA * magnitudeOfB);     // Print the angle    Console.Write(angle);} // Driver Codepublic static void Main(){         // Given magnitude arrays    double []arr = { -0.5, -2, 1 };    double []brr = { -1, -1, 0.3 };     // Size of the array    int N = arr.Length;     // Function call to find the    // angle between two vectors    angleBetweenVectors(arr, brr, N);}} // This code is contributed by bgangwar59

## Javascript

 
Output:
0.845289

Time Complexity: O(1)
Auxiliary Space: O(1)

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up