Python Program to Calculate the Discriminant Value

In the previous article, we have discussed Python Program to Check Tech Number or Not
Discriminant:

The distinguishing feature is the naming convention applied to the mathematical expression that appears beneath the root (radical) sign up the quadratic formula.

Formula :

Discriminant = (b**2) – (4*a*c)

a, b, c are the three different points.

Given three points a, b, c, and the task is to calculate the given discriminant value.

Examples:

Example 1:

Input:

Given  x= 1
Given  y= 4
Given  z= 4

Output: 

The given Discrimant value =  0
The obtained discriminant has only one solution

Example 2:

Input:

Given  x= 5
Given  y= 20
Given  z= 7

Output: 

The given Discrimant value = 260
The obtained discriminant has two possible solutions

Program to Calculate the Discriminant Value

Below are the ways to calculate the given discriminant value.

Method #1: Using Mathematical Formula (Static Input)

Approach:

  • Give the first number as static input and store it in a variable.
  • Give the second number as static input and store it in another variable.
  • Give the third number as static input and store it in another variable.
  • Calculate the discriminant value for the given three points and store it in another variable.
  • Print the obtained discriminant value.
  • Check if the value of the discriminant is greater than zero using the if conditional statement.
  • If the statement is true, then print “The obtained discriminant has two possible solutions”.
  • Check if the value of the discriminant is equal to zero using the elif conditional statement.
  • If the statement is true, then print “The obtained discriminant has only one solution”.
  • Check if the value of the discriminant is less than zero using the elif conditional statement.
  • If the statement is true, then print “The obtained discriminant has No solutions”.
  • The Exit of the program.

Below is the implementation:

# Give the first number as static input and store it in a variable.
x = 2
# Give the second number as static input and store it in another variable.
y = 4
# Give the third number as static input and store it in another variable.
z = 6
# Calculate the discriminant value for the given three points and
# store it in another variable.
Disimint = (y**2) - (4*x*z)
# Print the obtained discriminant value.
print("The given Discrimant value = ", Disimint)
# Check if the value of the discriminant is greater than zero using the
# if conditional statement.
if Disimint > 0:
 # If the statement is true, then print "The given obtained has two possible solutions".
    print("The obtained discriminant has two possible solutions")
# Check if the value of the discriminant is equal to zero using the elif
# conditional statement.
elif Disimint == 0:
  # If the statement is true, then print "The obtained discriminant has only one solution".
    print("The obtained discriminant has only one solution")
# Check if the value of the discriminant is less than zero using the elif conditional
# statement.
elif Disimint < 0:
  # If the statement is true, then print "The obtained discriminant has No solutions".
    print("The obtained discriminant has No solutions")

Output: 

The given Discrimant value =  -32
The obtained discriminant has No solutions

Method #2: Using Mathematical Formula (User Input)

Approach:

  • Give the first number as user input using the int(input()) function and store it in a variable.
  • Give the second number as user input using the int(input()) function and store it in another variable.
  • Give the third number as user input using the int(input()) function and store it in another variable.
  • Calculate the discriminant value for the given three points and store it in another variable.
  • Print the obtained discriminant value.
  • Check if the value of the discriminant is greater than zero using the if conditional statement.
  • If the statement is true, then print “The obtained discriminant has two possible solutions”.
  • Check if the value of the discriminant is equal to zero using the elif conditional statement.
  • If the statement is true, then print “The obtained discriminant has only one solution”.
  • Check if the value of the discriminant is less than zero using the elif conditional statement.
  • If the statement is true, then print “The obtained discriminant has No solutions”.
  • The Exit of the program.

Below is the implementation:

# Give the first number as user input using int(input()) function and store it in a variable.
x = int(input("Enter some random number = "))
# Give the second number as user input using int(input()) function and store it in another variable.
y = int(input("Enter some random number = "))
# Give the third number as user input using int(input()) function and store it in another variable.
z = int(input("Enter some random number = "))
# Calculate the discriminant value for the given three points and
# store it in another variable.
Disimint = (y**2) - (4*x*z)
# Print the obtained discriminant value.
print("The given Discrimant value = ", Disimint)
# Check if the value of the discriminant is greater than zero using the
# if conditional statement.
if Disimint > 0:
 # If the statement is true, then print "The given obtained has two possible solutions".
    print("The obtained discriminant has two possible solutions")
# Check if the value of the discriminant is equal to zero using the elif
# conditional statement.
elif Disimint == 0:
  # If the statement is true, then print "The obtained discriminant has only one solution".
    print("The obtained discriminant has only one solution")
# Check if the value of the discriminant is less than zero using the elif conditional
# statement.
elif Disimint < 0:
  # If the statement is true, then print "The obtained discriminant has No solutions".
    print("The obtained discriminant has No solutions")

Output: 

Enter some random number = 5
Enter some random number = 20
Enter some random number = 7
The given Discrimant value = 260
The obtained discriminant has two possible solutions

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.