Find the roots of the quadratic equation in Python (2024)

  • Home
  • DS & Algo. ▾
    • Data Structure
    • Algorithms
    • Coding Problems
  • Languages ▾
    • C
    • C++
    • C++ STL
    • Java
    • Python
    • Scala
    • Ruby
    • C#.Net
    • Golang
    • Android
    • Kotlin
    • SQL
  • Web. ▾
    • JavaScript
    • CSS
    • jQuery
    • PHP
    • Node.Js
    • AdonisJs
    • VueJS
    • Ajax
    • HTML
    • Django
  • Programs ▾
    • C
    • C++
    • Data Structure
    • Java
    • C#.Net
    • VB.Net
    • Python
    • PHP
    • Golang
    • Scala
    • Swift
    • Rust
    • Ruby
    • Kotlin
    • C Interview Programs
  • Aptitude ▾
    • C Aptitude
    • C++ Aptitude
    • Java Aptitude
    • C# Aptitude
    • PHP Aptitude
    • Linux Aptitude
    • DBMS Aptitude
    • Networking Aptitude
    • AI Aptitude
    • More...
  • Interview ▾
    • Golang
    • MIS Executive
    • DBMS
    • C
    • Embedded C
    • Java
    • SEO
    • HR
  • Find Output ▾
    • C
    • C++
    • C#.Net
    • Java
    • Go
    • PHP
    • More...
  • MCQs ▾
    • Web Technologie MCQs
    • CS Subjects MCQs
    • Databases MCQs
    • Programming MCQs
    • Testing Software MCQs
    • Digital Mktg Subjects MCQs
    • Cloud Computing S/W MCQs
    • Engineering Subjects MCQs
    • Commerce MCQs
    • More MCQs...
  • CS Subjects ▾
    • Machine Learning/AI
    • Operating System
    • Computer Network
    • Software Engineering
    • Discrete Mathematics
    • Digital Electronics
    • Data Mining
    • MIS
    • DBMS
    • Embedded Systems
    • Cryptography
    • CS Fundamental
    • More Tutorials...
  • More ▾
    • Tech Articles
    • Puzzles
    • Full Forms
    • Code Examples
    • Blogs
    • Guest Post
    • Programmer's Calculator
    • XML Sitemap Generator
    • About
    • Contact

Home »Python »Python Programs

In this tutorial, we will see how to Find the roots of the quadratic equation in Python programming?By Bipin Kumar Last updated : January 04, 2024

Problem statement

Write a Python program to input three values, and find the roots of the quadratic equation.

Quadratic Equation

An equation in the form of Ax^2 +Bx +C is a quadratic equation, where the value of the variables A, B, and C are constant and x is an unknown variable which we have to find through the Python program. The value of the variable A won't be equal to zero for the quadratic equation. If the value of A is zero then the equation will be linear.

Here, we are assuming a quadratic equation x^2-7x+12=0 which roots are 4 and -3.

Finding the roots of the quadratic equation

  1. We store the value of variables A, B and C which is given by the user and we will use the mathematical approach to solve this.
  2. Here, we find the value of ((B*B)-4*A*C) and store in a variable d.
    1. If the value of the variable d is negative then the value of x will be imaginary numbers and print the roots of the equation is imaginary.
    2. If the value of the variable is positive then x will be real.
  3. Since the equation is quadratic, so it has two roots which are x1
  4. and x2.
    x1=(-B+((B*B)-4*A*C) **0.5)/2*Ax2=(-B-((B*B)-4*A*C) **0.5)/2*A
  5. When we will find the value of roots of the equation from the above, it may be decimal or integer but we want the answer in an integer that's why we will take math.floor() of the value of the variable x.

Python program to find the roots of the quadratic equation

# importing math moduleimport math# Input the valuesA, B, C = map(int, input("Input three values: ").split())d = (B**2) - 4 * A * Cif d >= 0: s = (-B + (d) ** 0.5) / (2 * A) p = (-B - (d) ** 0.5) / (2 * A) print(math.floor(s), math.floor(p))else: print("The roots are imaginary")

Output

The output of the above example is:

RUN 1:Input three values: 1 -7 124 3RUN 2:Input three values: 1 5 6-2 -3

To understand the above program, you should have the basic knowledge of the following Python topics:

  • Python Variables
  • Python Data Types
  • Python print() Method
  • Read input as an int in Python
  • Python int() Method
  • Python input() Method
  • Python map() Method
  • Python math.floor() Method

Python Basic Programs »

Python program to find the least multiple from given N numbers

Python program to check the given Date is valid or not

Related Programs

  • Python | Find the factorial of a number using recursion
  • Python | Declare any variable without assigning any value.
  • Python | BMI (Body Mass Index) calculator.
  • Python | Program to print Odd and Even numbers from the list of integers.
  • Python | Program to print Palindrome numbers from the given list.
  • Python | Compute the net amount of a bank account based on the transactions.
  • Python | Count total number of bits in a number.
  • Python | Generate random number using numpy library.
  • Generate random integers between 0 and 9 in Python
  • Python | Program to calculate n-th term of a Fibonacci Series
  • Python program for sum of square of first N natural numbers
  • Python program for sum of cube of first N natural numbers
  • Python program to check prime number
  • Python program to find the largest prime factor of a number
  • Python program to find prime numbers using sieve of Eratosthenes
  • Python program to calculate prime numbers (using different algorithms) upto n
  • Python program to print all prime numbers in an interval
  • Python program to print all positive or negative numbers in a range
  • Python program for not None test
  • Python program for pass statement
  • Python program for Zip, Zap and Zoom game
  • Python program to convert temperature from Celsius to Fahrenheit and vice-versa
  • Python program to find the number of required bits to represent a number in O(1) complexity
  • Python program to count number of trailing zeros in Factorial of number N
  • Python program for swapping the value of two integers
  • Python program for swapping the value of two integers without third variable
  • Python program to find winner of the day
  • Python program for Tower of Hanoi
  • Python program to find standard deviation
  • Python program to find the variance
  • Python program Tower of Hanoi (modified)
  • Python program to convert Centimeter to Inches
  • Python program to convert meters into yards
  • Python program to convert yards into meters
  • Python program to capitalize the character without using a function
  • Python program to lowercase the character without using a function
  • Python program to find perfect number
  • Python program to print perfect numbers from the given list of integers
  • Python program to find greatest integer using floor() method
  • Python program to find the maximum EVEN number
  • Python program to find the maximum ODD number
  • Python program to find the solution of a special sum series
  • Python | Convert the binary number to decimal without using library function
  • Python | Convert the decimal number to binary without using library function
  • Create a stopwatch using Python
  • Python program to find the maximum multiple from given N numbers
  • Python program to find the least multiple from given N numbers
  • Python program to check the given Date is valid or not
  • Python program to print the calendar of any year

Comments and Discussions!

Load comments ↻


Find the roots of the quadratic equation in Python (2024)
Top Articles
Latest Posts
Article information

Author: Rubie Ullrich

Last Updated:

Views: 6034

Rating: 4.1 / 5 (52 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Rubie Ullrich

Birthday: 1998-02-02

Address: 743 Stoltenberg Center, Genovevaville, NJ 59925-3119

Phone: +2202978377583

Job: Administration Engineer

Hobby: Surfing, Sailing, Listening to music, Web surfing, Kitesurfing, Geocaching, Backpacking

Introduction: My name is Rubie Ullrich, I am a enthusiastic, perfect, tender, vivacious, talented, famous, delightful person who loves writing and wants to share my knowledge and understanding with you.