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 ...

How Does an Inverter (NOT) Gate Work - NOT Gate Explained

For a video version of this tutorial, please refer to this link . An inverter gate, or NOR gate for short, is one of the essential logic gates used constructed digital circuits. As implied by its name, the NOT gate works by taking an input, inverting it, then outputting the inverted value. The NOT gate schematic symbol looks like a triangle with clown nose on the Output side (as shown in the Figure above). In logic circuits, inputs and outputs are binary, which means their value can either be 0 or 1. So: If NOT receives a 0 at the input, it will output 1 . If NOT receives 1 at the input, it will output 0 . To sum it up, the truth table for the NOT gate will be as the following: Input Output X F 0 1 1 0 The NOT gate can be algebraically represented as the following:  F = X' (or you can have a bar above the X) where :     F is the output and X is the input

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...