SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Y Daniel Lang
Chapter:
.selections
Exercise:
Programming Excercises
Question:3 | ISBN:978013274719 | Edition: 6

Question

(Algebra: solve 2 x 2 linear equations) You can use Cramer’s rule to solve the following 2 x 2 system of linear equation:

ax+by =e 

cx+dy = f

x = \frac{ed-bf}{ad-bc}

y = \frac{af-ec}{ad-bc}

Write a program that prompts the user to enter a, b, c, d, e, and f and display the result. If ad – bc is 0, report that The equation has no solution.

Enter a, b, c, d, e, f: 9.0, 4.0, 3.0, -5.0, -6.0, -21.0
x is -2.0 and y is 3.0

Enter a, b, c, d, e, f: 1.0, 2.0, 2.0, 4.0, 4.0, 5.0
The equation has no solution

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

#enter a,b,c,d,e,f values
print("Enter a,b,c,d,e,f:")
a=eval(input())
b=eval(input())
c=eval(input())
d=eval(input())
e=eval(input())
f=eval(input())
#checking the condition ad-bc=0
#if the condition is true the statement will printed
if((a * d) - (b * c) == 0):
    print("The equation has no solution")
#if the condition is false below code will be executed    
else:
    x=(e * d) - (b * f) / (a * d) - (b * c)
    y=(a * f) - (e * c) / (a * d) - (b * c)
    print(" x  is",format(x,".1f"),"and y is ",format(y,".1f"))

OUTPUT:
 Enter a,b,c,d,e,f:
 1
 2
 3
 4
 5
 6
 x is 11.0 and y is -3.8

 Enter a,b,c,d,e,f:
 2
 3
 4
 5 
 6
 7
 x is 15.9 and y is -0.4


 

0 0

Discussions

Post the discussion to improve the above solution.