Skip to content

techtrendings.com

Let's explore

Menu
Menu

Program to find roots of Quadratic Equation C++

Posted on November 9, 2022November 9, 2022 by Avidlearner

To find roots of Quadratic equation , we need to first calculate the determinant. Once the determinant is calculated then we will get the roots using the formula. Below is the program

#include <iostream>
#include <cmath>
using namespace std;
 
int main()
{
 
    float a, b, c, x1, x2, determinant, realPart, imaginaryPart;
    cout << "Enter coefficient a :- ";
    cin >> a ;
    cout << "\nEnter coefficient b :- ";
    cin >> b ;
    cout << "\nEnter coefficient c :- ";
    cin >> c ;
 
    determinant = b*b - 4*a*c;
 
    if (determinant > 0)
    {
        x1 = (-b + sqrt(determinant)) / (2*a);
        x2 = (-b - sqrt(determinant)) / (2*a);
        cout << "\nRoots are real and different." << endl;
        cout << "\nx1 = " << x1 << endl;
        cout << "\nx2 = " << x2 << endl;
    }
 
    else if (determinant == 0)
    {
        cout << "\nRoots are real and same." << endl;
        x1 = (-b + sqrt(determinant)) / (2*a);
        cout << "\nx1 = x2 = " << x1 << endl;
    }
 
    else
    {
        realPart = -b/(2*a);
        imaginaryPart =sqrt(-determinant)/(2*a);
        cout << "\nRoots are complex and different."  << endl;
        cout << "\nx1 = " << realPart << "+" << imaginaryPart << "i" << endl;
        cout << "\nx2 = " << realPart << "-" << imaginaryPart << "i" << endl;
    }
 
    return 0;
}

Related

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Implement Trie Data Structure in C++- LeetCode
  • How TLS Works
  • C++ – Factory Design Pattern – Creation Design Pattern
  • C++ – Strategy Design Pattern – Behavioral Design Pattern
  • LFU Cache Implementation – LeetCode

Recent Comments

  • automatically like friends photos on instagram on Program to find unpaired element in an Array in C++|Leetcode |techtrendings
  • Twicsy on Program to find unpaired element in an Array in C++|Leetcode |techtrendings

Archives

  • January 2023
  • November 2022
  • August 2022
  • June 2022
  • May 2022
  • March 2022
  • February 2022
  • January 2022

Categories

  • Algorithm
  • Algorithm
  • C++
  • Design Patterns
  • Multithreading
  • OS Concepts
  • Programming
  • Uncategorized

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Join Our Mailing List for the Latest News and Updates.

© 2023 techtrendings.com | Powered by Superbs Personal Blog theme