Skip to main content

Write a C Program to Determine the roots of the quadratic equation (ax^2 + bx + c = 0) [Code and Explanation]

Determine the roots of the quadratic equation
Using the well-known quadratic formula
Allow for the possibility that one of the constants has a value of zero, and that the quantity
is less than or equal to zero. Test the program using the following sets of data:


(Code explanation at the bottom of the post)

#include "stdafx.h"
#include <stdio.h>
#include <cmath>

int main()
{
double a = 0, b = 0, c = 0, d, sqD, x1, x2;

printf("Enter a: ");                                    // Enter the value for a
scanf_s("%lf" , &a);
printf("\n");

printf("Enter b: ");                                    // Enter the value for b
scanf_s("%lf", &b);
printf("\n");

printf("Enter c: ");                                    // Enter the value for c
scanf_s("%lf", &c);
printf("\n");

d = pow(b, 2) - (4 * a * c); // we assign (b^2) - 4ac to variable d

if (d < 0) // we can't have imaginary number, so if d < 0, it gives an error
{
printf("Undetermined, imaginary number");
}

else
{
sqD = sqrt(d); // Square root of d

x1 = ((-b) + (sqD)) / (2 * a); // Root 1

x2 = ((-b) - (sqD)) / (2 * a); // Root 2

printf("Root 1 = %lf\n", x1);
printf("Root 2 = %lf\n", x2);
}



    return 0;
}


Once you run the program in Visual Studio, you will get something like this:

After you input a number for a and press enter, you will be asked to input a number for b, then same process for c:


After you enter the number, if d is imaginary, then an error message will show stating the number is undetermined:

However, if d is real then the program will display the roots just fine:


Ok! let's breakdown the code line by line:


We include the header <cmath> because we are dealing with double\floating numbers , in addition to  using the mathematical functions pow and sqrt. We can't use sqrt or pow without the cmath header.
--------------------------------------------------------------------------

printf("Enter a: ");                                    // Enter the value for a
scanf_s("%lf" , &a);
printf("\n");

printf("Enter b: ");                                    // Enter the value for b
scanf_s("%lf", &b);
printf("\n");

printf("Enter c: ");                                    // Enter the value for c
scanf_s("%lf", &c);
printf("\n");

In this part, we are simply asking the user to enter the values that will be stored in a, b, and c.
--------------------------------------------------------------------------

d = pow(b, 2) - (4 * a * c);
Here, we will calculate the equation below and store the value to a variable named d
pow(b,2) means that we are rising b to the power of 2, b^2
--------------------------------------------------------------------------


if (d < 0) // we can't have imaginary number, so if d < 0, it gives an error
{
printf("Undetermined, imaginary number");
}

After d is calculated, we are gonna compare it with 0. If d < 0, then we can't determine the roots because we will have to take square root of d, which will be an imaginary number since d < 0.

If that's the case, then we will have the program prints out a message stating that you can't determine the roots.
--------------------------------------------------------------------------

else
{
sqD = sqrt(d); // Square root of d

x1 = ((-b) + (sqD)) / (2 * a); // Root 1

x2 = ((-b) - (sqD)) / (2 * a); // Root 2

printf("Root 1 = %lf\n", x1);
printf("Root 2 = %lf\n", x2);
}


If the calculated value of d is not less than 0, then we proceed to finding the roots using the below equation
as we know [(b^2) - 4ac] is assigned to d, to find the square root of it we use a math function sqrt (stands for Square Root), sqrt(d). We will take that and find the roots and print them out with this code:

x1 = ((-b) + (sqD)) / (2 * a); // Root 1
x2 = ((-b) - (sqD)) / (2 * a); // Root 2
printf("Root 1 = %lf\n", x1);                                  // Print out Root 1
printf("Root 2 = %lf\n", x2);                                  // Print out Root 2



and that's it....

Popular posts from this blog

Logic Gate Truth Tables - Reference Guide (Cheat Sheet)

A logic gate is the implementation of a Boolean function. It performs a logic operation on one or more binary input. By binary I mean that the input can either be 1 or 0, nothing else. This guide is intended to be quick guide\reference, I will dive into more details about each gate on transistor\diode level in other posts. The guide cover gates with 2 inputs, meaning that each truth table will have 4 cases (2^2 = 4) AND Gate: Boolean Logic Operator: Q = A  •  B  Input Output A B Q 0 0 0 0 1 0 1 0 0 1 1 1 Summary: in order to achieve 1 on the output, both inputs A AND B must be 1 OR Gate: Boolean Logic Operator: Q = A  +  B  Input Output A B Q 0 0 0 0 1 1 1 0 1 1 1 1 Summary: in order to ...

LTspice Tutorial: How to Design and Simulate a Circuit in LTspice

 If you do not have LTSpice already installed on your machine, please head over to LTSpice's page on Analog Device's website (Link: LTspice Information Center | Analog Devices ). Note: a video version of this tutorial is available here  Click on the applicable download button that suits your machine's operating system. Upon successfully downloading and launching LTspice, you will be greeted with a window that looks something like in the figure below. Alright, now we will need to create a new schematic. That can be done by either of the following approaches:  click on File then choose New Schematic . click on the New Schematic symbol   on the toolbar. on your keyboard, press Ctrl+N . Upon doing so, a new grey-ish screen will pop up... that will be your new empty schematic (just like the one in the figure below). In this example, we will try to design and simulate a very basic voltage divider.  To recap: a voltage divider is a circuit that takes a certain i...

How to Connect the Hercules Dual 15A 6-20V Motor Controller, Load its Library and Program it

The Hercules Motor Controller Board First, if you don't have the Arduino Integrated Development Environment (IDE) installed, please download and install it from this following link: https://www.arduino.cc/en/Main/Software Once you go to the link above, scroll down to the following section: If you are using a  Windows PC, click on Windows Installer link  for fast and easy installation process.  If you are using  a Raspberry Pi , click on the Linux ARM link. For other Linux distros (Ubuntu, etc...) use either Linux 64 or Linux 32 depending on what operating system you have installed. Second, download the software library provided by the manufacturer. Don't use the link on the official website of the product as the library there doesn't work. Instead, use the one on github. Here is the link:  https://github.com/Seeed-Studio/Hercules_Motor_Driver Once you open the link above, click on 'clone or download' then 'Download Z...