2 * 2 linear equations

  2 * 2 linear equations

.

Problem Description


A linear equation can be solved using Cramers rule.
Write a program that prompts the user to enter a, b, c, d, e, and f and displays the result. If ad - bc is 0, report that The equation has no solution.



Logic Test Case 1


Input (stdin)
6
5
4
3
2
1


Expected Output
x=-0.5
y=1.0


Logic Test Case 2


Input (stdin)
2
3
4
1
3
4

Expected Output
x=0.9

y=0.4


Code Area


import java.io.*;
import java.util.Scanner;
public class TestClass {
public static void main(String[] args)
     { 
       Scanner s=new Scanner(System.in);
       double a=s.nextDouble();
       double b=s.nextDouble();
       double c=s.nextDouble();
       double d=s.nextDouble();
       double e=s.nextDouble();
       double f=s.nextDouble();
       if((a*d)-(b*c)==0)
      {
         System.out.println("The equation has no solution");
       }
       else
       { 
         double x=((e*d)-(b*f))/((a*d)-(b*c));
         double y=((a*f)-(e*c))/((a*d)-(b*c));
         System.out.println("x=" +x);
         System.out.println("y=" +y);
      }                   
       
     }
}

Post a Comment

0 Comments