Join Regular Classroom : Visit ClassroomTech

Accenture Coding Question | Quadratic Equation Roots | CodeWindow

Quadratic Equation Roots

Write a function to calculate roots of a quadratic equation of the from ax^2+bx+c=0
The formula to find the roots of a quadratic equation is given below:
X+ = (-b + root of (b^2 – 4ac))/2a
X- = (-b – root of (b^2 – 4ac))/2a

Input specification:
Input 1: A double value representing the coefficient value of x^2 in a quadratic equation.
Input 2: A double value representing the coefficient value of x in a quadratic equation.
Input 3: A double value representing the coefficient value of 1 in a quadratic equation.

Output Specification:
Return a double array representing the value of the two roots.

Note: Consider the output root values off to 3 decimal places. Assume all roots are real.

Example 1:
Input 1: 1
Input 2: -2
Input 3: -3

Output: {3.0,-1.0}

Explanation:
On substituting the values of a,b and c in the formula, the roots will be as follows:
x+ = 3.0
x- = -1.0

Example 2:
Input 1: 2
Input 2: 4
Input 3: 1

Output: {-1.707, -0.293}

Solution in Python 3:

#https://codewindow.in
#join our telegram channel

import math
a=int(input())
b=int(input())
c=int(input())

pos = (-b + math.sqrt((b**2) - (4*a*c)))/(2*a)

neg = (-b - math.sqrt((b**2) - (4*a*c)))/(2*a)

print(round(neg,3),round(pos,3))

# Telegram @codewindow

Solution in C :

//https://codewindow.in
//join our telegram channel @codewindow

#include <math.h>
#include <stdio.h>
#include <stdlib.h>

void findRoots(int a, int b, int c)
{
    if (a == 0) {
        printf("Invalid");
        return;
    }
 
    int d = b * b - 4 * a * c;
    double sqrt_val = sqrt(abs(d));
 
    if (d > 0) {
        printf("%.2f %.2f", (double)(-b + sqrt_val) / (2 * a), (double)(-b - sqrt_val) / (2 * a));
    }
    else if (d == 0) {
        printf("%.2f", -(double)b / (2 * a));
    }
    else
    {
        printf("%.2f + i%.2f %.2f - i%.2f", -(double)b / (2 * a),sqrt_val/(2 * a), -(double)b / (2 * a), sqrt_val/(2 * a));
    }
}

int main()
{
    int a, b, c;
    scanf("%d",&a);
    scanf("%d",&b);
    scanf("%d",&c);
    findRoots(a, b, c);
    return 0;
}

Solution in C++ :

//https://codewindow.in
//join our telegram channel @codewindow

#include <bits/stdc++.h>
#include <iostream>

using namespace std;
 
void findRoots(int a, int b, int c)
{
    if (a == 0) {
        cout << "Invalid";
        return;
    }
 
    int d = b * b - 4 * a * c;
    double sqrt_val = sqrt(abs(d));
 
    if (d > 0) {
        cout << (double)(-b + sqrt_val) / (2 * a) << " " << (double)(-b - sqrt_val) / (2 * a);
    }
    else if (d == 0) {
        cout << -(double)b / (2 * a);
    }
    else
    {
        cout << -(double)b / (2 * a) << " + i" << sqrt_val << " " << -(double)b / (2 * a) << " - i" << sqrt_val;
    }
}
 
int main()
{
    int a, b, c;
    cin >> a;
    cin >> b;
    cin >> c;
    findRoots(a, b, c);
    return 0;
}

Solution in JAVA :

//https://codewindow.in
//join our telegram channel @codewindow

import java.io.*;
import java.lang.*;
import java.util.*;

class CodeWindow {
    static void findRoots(int a, int b, int c)
    {
        if (a == 0) {
            System.out.println("Invalid");
            return;
        }
 
        int d = b * b - 4 * a * c;
        double sqrt_val = Math.sqrt(Math.abs(d));
 
        if (d > 0) {
            System.out.println((double)(-b + sqrt_val) / (2 * a) + " " + (double)(-b - sqrt_val) / (2 * a));
        }
        else if (d == 0) {
            System.out.println(-(double)b / (2 * a) + " " + -(double)b / (2 * a));
        }
        else
        {
            System.out.println(-(double)b / (2 * a) + " + i" + sqrt_val + " " + -(double)b / (2 * a) + " - i" + sqrt_val);
        }
    }
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        findRoots(a,b,c);
    }
}

Output:

-1
2
3
3.0 -1.0
2
4
1
-1.707 -0.293
Recent Posts
Pages