×

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

Call Back Function in Embedded C

Callback function are one the most remarkable systems in C. A callback function is any code that is passed as a contention to another code, so this is the last...

3 minutes read.

Ftell() Function in C

Ftell(): In File Handling we have some special functions like Ftell(), Fseek(), rewind() etc.. while you are randomly accessing the file these functions play very important role and these functions...

3 minutes read.

How to use sine() function in C

What is Function? The function is a set of statements. It takes input and performs some computation to produce output. The process is a set of codes only achieved when it is...

3 minutes read.

Execution flow of C program

There are various steps of the execution of the C program that is given below. The following step of the execution Write source codes Preprocess Compile Link edit Load Execute Editor or...

1 minute read.

Difference between Scope and Lifetime in C

In this article, we will discuss about the key differences between scope and lifetime in C, including their definitions, applications, and how they relate to one another. By the end...

8 minutes read.

Pointer to pointer in C

A pointer to another pointer is another type of multiple indirections and a chain of many pointers. Generally, a pointer consists of the address of the variable. Once a pointer to...

4 minutes read.

Compilation Process in C language

What is the Compilation Process?  The compilation is a method whereby the source code is converted into object code. It is achieved with compiler assistance. The compiler tests the source code for syntactic...

3 minutes read.

What is the main in C?

In the C programming language, "main" is a predefined term or function. It is the primary function in every C program and is in charge of beginning and terminating the...

6 minutes read.

Stack implementation in C

Stack implementation in C Stack stores the data in a particular order. It is a linear data structure that follows the principle of the Last In First Out (LIFO) technique where...

3 minutes read.

How to get ASCII value in C

For text data on computers and the internet, ASCII (American Standard Code for Information Interchange). It is the most widely used character encoding standard. One hundred twenty-eight alphabetic, numeric, special...

3 minutes read.

Nested if-else statement in C

If we use an if-else statement within another if statement in a C program, it is called a nested if-else statement in C. It helps to check the condition inside...

3 minutes read.

Associativity of Operators in C

What is an Associativity operator? Two operators with equal priority are connected by employing their association when they are present in an expression. There are two different types of associativity: left to rightright...

4 minutes read.

Difference between Array and List in C

C# Array vs List is where the abstraction and implementation of human computing meet.  Arrays are incredibly related to the hardware concept of contiguous and contiguous memory, where each part is...

3 minutes read.

Binary Search in C with Best and Worst Time Complexity

What is Binary Search? Binary search is an algorithm that is used to find an element in a sorted array efficiently. It has a time complexity of O(log n). It means...

3 minutes read.

Palindrome Number in C

What is a Palindrome? A number that doesn't change when reversed is known as a palindrome. We reverse a number and compare it to the original number to determine whether it is...

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.

Nested loop in C

Definition of Nested Loop A nested loop is generally used when we want to run a loop statement inside another loop statement. This kind of loop is also known as a...

3 minutes read.

Floor() Function in C

Floor() function is a built-in function in C which is defined in the math.h header file. This function is used to return the nearest integer value which is less than...

4 minutes read.

Star Program in C Language

Star Program in C Star patterns are a sequence of * or any other character used to construct any pattern or any like-square geometric form, triangle, hollow square, pyramid, rhombus, etc.  Many programmers worldwide highly...

4 minutes read.

Add two numbers using the function in C

Here we will learn how to add two numbers by creating function in C. Let’s learn this with help of example. Code: - #include <stdio.h> int add_two_no(int a, int b); int main(){   int first_num,...

1 minute read.