OOP-JAVA Practical 2

OOP-JAVA Practical 2
  • Aim: Write a program that solves the following equation and displays the value x and y: 1) 3.4x+50.2y=44.5 2) 2.1x+0.55y=5.9 (Assume Cramers rule to solve equation ax+by=e x=ed-bf/ad-bc cx+dy=f y=af-ec/ad-bc ).
  • Code:
    import java.util.Scanner;
    class practical2 {
        public static void main (String args[])
        {
            double a,b,c,d,e,f,x,y;
            Scanner s=new Scanner(System.in);
            System.out.println("Two Equations are: a*x+b*y=e and c*x+d*y=f\nWhere a,b,c,d,e,f are conctant and x,y are variables\n Now enter the values of contant to Solve Equation");
            System.out.print("Enter value of a:");
            a=s.nextDouble();
            System.out.print("Enter value of b:");
            b=s.nextDouble();
            System.out.print("Enter value of c:");
            c=s.nextDouble();
            System.out.print("Enter value of d:");
            d=s.nextDouble();
            System.out.print("Enter value of e:");
            e=s.nextDouble();
            System.out.print("Enter value of f:");
            f=s.nextDouble();
            x=(e*d-b*f)/(a*d-b*c);
            y=(a*f-e*c)/(a*d-b*c);
            System.out.print("Value of x and y is : "+x+" " +y);
        }
    }
  • Output:
    Output of practical 2

Comments