alon2k2

Equations

Mar 5th, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.21 KB | None | 0 0
  1. /**
  2.  * Write a description of class Equations here.
  3.  *
  4.  * @id xxxxxxxxx
  5.  * @author Alon
  6.  */
  7.  
  8. import java.util.Scanner;
  9.  
  10. public class Equations
  11. {
  12.     public static void main(String[] args) {
  13.        
  14.         System.out.println("This program solves a system of 2 linear equations");
  15.         System.out.println("Enter the coefficients a11 a12 a21 a22 b1 b2");
  16.         Scanner scan = new Scanner(System.in);
  17.         int a11 = scan.nextInt();
  18.         int a12 = scan.nextInt();
  19.         int a21 = scan.nextInt();
  20.         int a22 = scan.nextInt();
  21.         int b1 = scan.nextInt();
  22.         int b2 = scan.nextInt();
  23.        
  24.         System.out.println("Eq1: " + a11 + "*x1+" + a12 + "*x2=" + b1);
  25.         System.out.println("Eq2: " + a21 + "*x1+" + a22 + "*x2=" + b2);
  26.        
  27.         Boolean isOneResult = (a11*a22 - a12*a21) != 0;
  28.        
  29.         // Single solution
  30.         if (isOneResult) {
  31.             System.out.println("Single solution: (" + ((double)((b1*a22) - (b2*a12)) / ((a11*a22) - (a12*a21))) + "," +
  32.                                                       ((double)((b2*a11) - (b1*a21)) / ((a11*a22) - (a12*a21))) + ")");
  33.         }
  34.        
  35.     } // end of class main
  36. } // end of class Equations
Add Comment
Please, Sign In to add comment