×

C Program to find the Roots of a Quadratic Equation

What is Quadratic Equation:

Equations of degree 2 in polynomial form are called quadratic equations. A, B, and C are the coefficient variables in the equation, which is written as ax2 + bx + c = 0. The value of "a" cannot be zero, according to the quadratic equation's universal rule, and the value of "x" is what's used to determine the quadratic equation's roots (a, b). Real and distinct, real and equal, and real and imaginary roots are three different methods to describe a quadratic equation's roots.

Quadratic Equation:

ax2 + bx + c = 0
(x = (-b ± √ (b2 - 4ac)) /(2a)

Characteristics of Roots:

  1. If Discriminant> 0, the roots are real and not equal.
  2. If Discriminant = 0, the roots are real and equal.
  3. If Discriminant < 0, the roots are real and imaginary.

How to Solve the Quadratic Equation's Square Roots:

  1. Considering some variables which will be used to solve quadratic equation problem.
  2. Variables should be initialized.
  3. The user will put three values as input,
  4. Those values will be stored in the variables.
  5. Then we have to find discriminant(d) of the quadratic equation.
    Formula: d = j * j - 4 * i * k;
  6. If (d > 0), then
    rt1 = (-j + sqrt(d)) / (2 * i); 
     rt2 = (-j - sqrt(d)) / (2 * i);
    Roots of quadratic Equation are real and not equal.
  7. Else if (d==0), then
    rt1 = rt2 = -j / (2 * i)
    Roots of quadratic Equation are real and equal.
  8. Else (d < 0), then
    real_num = -j / (2 * i); 
    img_num = sqrt(-d) / (2 * i)
    Roots of quadratic equation are imaginary.
  9. Exit

C Program to calculate the roots of a quadratic equation:

//Code
 
#include<stdio.h>  
#include<math.h> 
#include<conio.h>  




int main()  
{  
//considering variables
    float i, j, k, d, rt1, rt2, real_num, img_num;  
    
    //asks user for three inputs
    printf("\n Enter the values of the coefficients i, j and k: \n ");  
    
    //storing values in the variables
    scanf("%f %f %f", &i, &j, &k);  
    
//formula to find the discriminant(d) of the quadratic equation 
    d = j * j - 4 * i * k;   
   
    // defines the conditions that the roots of the quadratic equation are real and not same 
    if (d > 0)  
    {  
    rt1 = (-j + sqrt(d)) / (2 * i);  
    rt2 = (-j - sqrt(d)) / (2 * i);  
    printf("\n Root 1 value = %.2f and Root 2 value = %.2f", rt1, rt2);  
    }  
    // elseif condition defines both roots real are equal in the quadratic equation  
    else if (d == 0)  
    {  
        rt1 = rt2 = -j / (2 * i); // both roots are equal;  
        printf("\nRoot 1 value = %.2f and Root 2 value = %.2f", rt1, rt2);  
    }  
   //else condition defines both the roots are imaginary
    else {  
        real_num = -j / (2 * i);  
        img_num = sqrt(-d) / (2 * i);  
        printf("\nRoot 1 value = %.2f + %.2fi and Root 2 value = %.2f - %.2fi ", real_num, img_num, real_num, img_num);  
    }  
    return 0;  
}

Example 1:

If the user puts 3, 4, 5 as inputs;

Output:

Enter the values of the coefficients i, j and k:
 3
4
5




Root 1 value = -0.67 + 1.11i and Root 2 value = -0.67 - 1.11i

Example 2:

If the user puts 1, -2, 1 as inputs;

Output:

 Enter the values of the coefficients i, j and k:
 1
-2
1




Root 1 value = 1.00 and Root 2 value = 1.00

Example 3:

If the user puts 1, -5, 6 as inputs;

Output:

Enter the values of the coefficients i, j and k:
 1
-5
6




 Root 1 value = 3.00 and Root 2 value = 2.00

Example 4:

If the user puts 2, -9, -5 as inputs;

Output:

Enter the values of the coefficients i, j and k:
 2
-9
-5




 Root 1 value = 5.00 and Root 2 value = -0.50

Time Complexity:

Let us consider, d = discriminant.

Time complexity will be O ( log (d) )

Space Complexity:

Space complexity will be O ( 1 )


Related Topics

C printf and Scanf

Input-Output functions in C Programming In C Language, the printf() and scanf() are inbuilt library functions that used for input and output. It is defined in the header file“<stdio.h>”. printf() Function: In C...

2 minutes read.

Character Class Tests in C

Introduction The <ctype.h> is a header file in C which is used to declare functions for testing characters. This header file of the C standard library is used to declare multiple...

4 minutes read.

OpenGL in C

OpenGL stands for Open Graphics Library. It is a low-level, widely supported modeling and rendering software package that is widely available across all the platforms. It is an API used...

4 minutes read.

Difference between If and Switch Statement in C

Key Contrast: In the event that assertion is utilizes a Boolean articulation to execute the capability and can frequently be utilized to really look at various circumstances all at once.  The change...

4 minutes read.

How to initialize array to zero in C

In this article, you will learn how to initialise an array to 0 in C In C, an array is declared as: char ZEROARRAY[2022]; The global scope changes at runtime to all zeros....

3 minutes read.

How to return a Pointer from a Function in C?

In the C programming language, pointers are variables that hold the address memory location of another variable. We could also input pointers to a function and return pointers from a...

3 minutes read.

Branching Statements in C

What is branching in c? Branching gets its name from the fact that the computer can select which branch to follow.Programs written in the C language execute statements one after the...

6 minutes read.

Random function in C

Random function in C In the C programming language, the rand() is a function used for Pseudo Random Number Generator (PRNG). The random number generated by the rand() function is not...

4 minutes read.

Type qualifiers in C

Type qualifiers in C: In the C programming language, type qualifiers are the keywords that prepend to the variables to change their accessibility, i.e., we can tell that the type...

4 minutes read.

File Handling in C

File Handling is used to open, read, write, search and close file.  C files I/O functions handles data on secondary storage device. File handling function: There are varioushandling functions in C. Function Operation fopen() It is...

4 minutes read.

Call by Value and Call by Reference in C

Call by reference and call by value are two different ways of passing arguments to a function in C programming language. Call by reference means that the called function is...

4 minutes read.

Function in C

Function is a group of statements that are used to perform any task. In other words, we can say that a function is a self- contained a block of programs...

3 minutes read.

Armstrong program in C using function

In this article we will learn how to find Armstrong of a number using function in C. An Armstrong number is a three-digit number that is the sum of its separate...

1 minute read.

Int in C

The keyword int in C programming stands for integer and it is a data type which is used for variable declarations or declaration of functions of different types. Similar to...

4 minutes read.

C Program to find the Roots of a Quadratic Equation

What is Quadratic Equation: Equations of degree 2 in polynomial form are called quadratic equations. A, B, and C are the coefficient variables in the equation, which is written as ax2...

3 minutes read.

Fibonacci series program in C using Recursion

In this tutorial, we’ll explore how to utilise recursion to build the Fibonacci sequence in C language. What does the term "Fibonacci Series" mean? The following number in a Fibonacci number series is...

2 minutes read.

Entry Control Loop in C

Initially, an entry control loop verifies the termination condition at the entry point. After that, its control passes to the main body of the while or for loop if the...

3 minutes read.

Bitwise XOR Operator in C

What is Bitwise? As we know, bits are the smallest unit of a number and are mostly used in a computing system. So, in bitwise, we do operations on bits instead...

3 minutes read.

Volatile in C

Introduction A volatile keyword is a qualifier in C. Qualifiers are nothing but keywords which are used to modify the properties of a variable. Qualifiers are of two types: 1) Const The const type...

3 minutes read.

CRC Program in C

CRC (Cyclic Redundancy Check) is an error-detection algorithm that is used to detect any errors that may have occurred during the transmission or storage of data. The basic idea behind...

4 minutes read.