Java Program to Find Cross Product of Two Vectors

This is a Java Program to compute cross product of two vectors. In mathematics, the cross product or vector product is a binary operation on two vectors in three-dimensional space. It results in a vector that is perpendicular to both and therefore normal to the plane containing them.

Here is the source code of the Java Program to Compute Cross Product of Two Vectors. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. //This is a java program to find the cross product of two vectors
  2. import java.util.Random;
  3.  
  4. public class Cross_Product
  5. {
  6.     public static void main(String args[])
  7.     {
  8.         Random random = new Random();
  9.         int u1, u2, u3, v1, v2, v3;
  10.         u1 = random.nextInt(10);
  11.         u2 = random.nextInt(10);
  12.         u3 = random.nextInt(10);
  13.         v1 = random.nextInt(10);
  14.         v2 = random.nextInt(10);
  15.         v3 = random.nextInt(10);
  16.  
  17.         int uvi, uvj, uvk;
  18.         uvi = u2 * v3 - v2 * u3;
  19.         uvj = v1 * u3 - u1 * v3;
  20.         uvk = u1 * v2 - v1 * u2;
  21.  
  22.         System.out.println("The cross product of the 2 vectors \n u = " + u1
  23.                 + "i + " + u2 + "j + " + u3 + "k and \n v = " + u1 + "i + "
  24.                 + u2 + "j + " + u3 + "k \n ");
  25.         System.out.println("u X v : " + uvi + "i +" + uvj + "j+ " + uvk + "k ");
  26.     }
  27. }

Output:

$ javac Cross_Product.java
$ java Cross_Product
 
The cross product of the 2 vectors 
 u = 3i + 8j + 9k and 
 v = 3i + 8j + 9k 
 
u X v : -2i +48j+ -42k

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.