Solving Quadratic Equation in easiest way using Python

in utopian-io •  7 years ago  (edited)

DQmeff3JttPUUbcFzo9pJAmLM9jQuwa86TMYPaaeHHtFAeX.gif
image source

Python is a high-level programming language that is very simple and easy to learn yet powerful. It has efficient high-level data structures and a simple but effective approach to object-oriented programming. The Python interpreter and the extensive standard library can be downloaded at https://www.python.org/
This tutorial aims to solve quadratic equation in easiest way.

What Will I Learn?

Write here briefly the details of what the user is going to learn in a bullet list.

  • Solving Quadratic Equation using Python
  • Used of cmath module

Requirements

  • Python IDLE

Difficulty

  • Basic

Tutorial Contents

The Code:

First, import the cmath module which is already a package from the interpreter. This provides access to mathematical functions for complex numbers and it accepts integers, floating-point numbers or complex numbers as arguments.

import cmath

Second, take the coefficients a, b, and c from the user. It is treated as floating-point numbers so that it can accept non-decimal integers.

a = float(input('Enter value of a: '))
b = float(input('Enter value of b: '))
c = float(input('Enter value of c: '))

Third, calculate the discriminant with the formula b^2 - 4ac and assign it to a variable. In my case i used variable d.

d = (b**2) - (4*a*c)

Then with the discriminant value we can get our two solutions that can be solve using the formula x1 = -b+√(discriminant value) / 2a and x2 = -b-√(discriminant value) / 2a

solution1 = (-b-cmath.sqrt(d))/(2*a)
solution2 = (-b+cmath.sqrt(d))/(2*a)

And lastly, we print the answers of the solution.

print('The solution are {0} and {1}'.format(solution1,solution2))

Save the file and we can now run the code. Just press F5. The result will look like this:
DQmV9C4SyvxMbbYPALAYrKp3SDgYSuSMDs1kW2f38vfAYwi.png

This is my first contribution of an application using Python.

Thanks for reading! Hope you learn something.



Posted on Utopian.io - Rewarding Open Source Contributors

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

nice one sir!

thanks man!

great sharing information :) keep it up!

i hate math as i cant understand these algebric numbers

Your contribution cannot be approved because it does not follow the Utopian Rules.

  • Wrong repository. Should have been python.
  • Very short and not very informative. (Less detailed)
  • Very basic too.
    You can contact us on Discord.
    [utopian-moderator]