Skip to main content

Boundary Value Analysis : Nature of Roots of a Quadratic equation

Consider a problem for the determination of the nature of the roots of a quadratic equation where the inputs are 3 variables (a, b, c) and their values may be from the interval [0, 100]. The output may be one of the following depending on the values of the variables:

  • Not a quadratic equation,
  • Real roots,
  • Imaginary roots,
  • Equal roots

Our objective is to design the boundary value test cases. Boundary value analysis is a software testing technique in which tests are designed to include representatives of boundary values in a range. A boundary value analysis has a total of 4*n+1 distinct test cases, where n is the number of variables in a problem. Here we have to consider all three variables and design all the distinct possible test cases. We will have a total of 13 test cases as n = 3.

  • Roots are real if (b2 – 4ac) > 0
  • Roots are imaginary if (b2 – 4ac) < 0
  • Roots are equal if (b2 – 4ac) = 0
  • Equation is not quadratic if a = 0

How do we design the test cases ? For each variable we consider below 5 cases:

  • amin = 0
  • amin+1 = 1
  • anominal = 50
  • amax-1 = 99
  • amax = 100

When we are considering these 5 cases for a variable, rest of the variables have the nominal values, like in the above case where the value of ‘a’ is varying from 0 to 100, the value of ‘b’ and ‘c’ will be taken as the nominal or average value. Similarly, when the values of variable ‘b’ are changing from 0 to 100, the values of ‘a’ and ‘c’ will be nominal or average i.e 50. The possible test cases for the nature of roots of a Quadratic Equation in a Boundary Value Analysis can be: Below is the program that verifies the test cases considered in the table shown above. The program takes user-defined inputs so that you can check for any of the test cases mentioned above. 

C++




// C++ program to check the nature of the roots
 
#include
using namespace std;
 
// BVA for nature of roots of a quadratic equation
void nature_of_roots(int a, int b, int c)
 
    // If a = 0, D/2a will yield exception
    // Hence it is not a valid Quadratic Equation
    if (a == 0)
        cout << "Not a Quadratic Equation"
             << endl;
        return;
    
 
    int D = b * b - 4 * a * c;
 
    // If D > 0, it will be Real Roots
    if (D > 0)
        cout << "Real Roots" << endl;
    
 
    // If D == 0, it will be Equal Roots
    else if (D == 0)
        cout << "Equal Roots" << endl;
    
 
    // If D < 0, it will be Imaginary Roots
    else
        cout << "Imaginary Roots" << endl;
    
 
// Function to check for all testcases
void checkForAllTestCase()
 
    cout << "Testcase"
         << "\ta\tb\tc\tActual Output"
         << endl;
    cout << endl;
    int a, b, c;
    int testcase = 1;
    while (testcase <= 13)
        if (testcase == 1)
            a = 0;
            b = 50;
            c = 50;
        
        else if (testcase == 2)
            a = 1;
            b = 50;
            c = 50;
        
        else if (testcase == 3)
            a = 50;
            b = 50;
            c = 50;
        
        else if (testcase == 4)
            a = 99;
            b = 50;
            c = 50;
        
        else if (testcase == 5)
            a = 100;
            b = 50;
            c = 50;
        
        else if (testcase == 6)
            a = 50;
            b = 0;
            c = 50;
        
        else if (testcase == 7)
            a = 50;
            b = 1;
            c = 50;
        
        else if (testcase == 8)
            a = 50;
            b = 99;
            c = 50;
        
        else if (testcase == 9)
            a = 50;
            b = 100;
            c = 50;
        
        else if (testcase == 10)
            a = 50;
            b = 50;
            c = 0;
        
        else if (testcase == 11)
            a = 50;
            b = 50;
            c = 1;
        
        else if (testcase == 12)
            a = 50;
            b = 50;
            c = 99;
        
        else if (testcase == 13)
            a = 50;
            b = 50;
            c = 100;
        
        cout << "\t" << testcase << "\t"
             << a << "\t" << b << "\t"
             << c << "\t";
        nature_of_roots(a, b, c);
        cout << endl;
        testcase++;
    
 
// Driver Code
int main()
    checkForAllTestCase();
    return 0;

Java




// Java program to check the nature of the roots
import java.util.*;
 
class GFG
 
// BVA for nature of roots of a quadratic equation
static void nature_of_roots(int a, int b, int c)
 
    // If a = 0, D/2a will yield exception
    // Hence it is not a valid Quadratic Equation
    if (a == 0)
    
        System.out.print("Not a Quadratic Equation"
            +"\n");
        return;
    
 
    int D = b * b - 4 * a * c;
 
    // If D > 0, it will be Real Roots
    if (D > 0)
        System.out.print("Real Roots" +"\n");
    
 
    // If D == 0, it will be Equal Roots
    else if (D == 0)
        System.out.print("Equal Roots" +"\n");
    
 
    // If D < 0, it will be Imaginary Roots
    else
        System.out.print("Imaginary Roots" +"\n");
    
 
// Function to check for all testcases
static void checkForAllTestCase()
 
    System.out.print("Testcase"
        + "\ta\tb\tc\tActual Output"
        +"\n");
    System.out.println();
    int a, b, c;
    a = b = c = 0;
    int testcase = 1;
    while (testcase <= 13)
        if (testcase == 1)
            a = 0;
            b = 50;
            c = 50;
        
        else if (testcase == 2)
            a = 1;
            b = 50;
            c = 50;
        
        else if (testcase == 3)
            a = 50;
            b = 50;
            c = 50;
        
        else if (testcase == 4)
            a = 99;
            b = 50;
            c = 50;
        
        else if (testcase == 5)
            a = 100;
            b = 50;
            c = 50;
        
        else if (testcase == 6)
            a = 50;
            b = 0;
            c = 50;
        
        else if (testcase == 7)
            a = 50;
            b = 1;
            c = 50;
        
        else if (testcase == 8)
            a = 50;
            b = 99;
            c = 50;
        
        else if (testcase == 9)
            a = 50;
            b = 100;
            c = 50;
        
        else if (testcase == 10)
            a = 50;
            b = 50;
            c = 0;
        
        else if (testcase == 11)
            a = 50;
            b = 50;
            c = 1;
        
        else if (testcase == 12)
            a = 50;
            b = 50;
            c = 99;
        
        else if (testcase == 13)
            a = 50;
            b = 50;
            c = 100;
        
        System.out.print("\t" + testcase+ "\t"
            + a+ "\t" + b+ "\t"
            + c+ "\t");
        nature_of_roots(a, b, c);
        System.out.println();
        testcase++;
    
 
// Driver Code
public static void main(String[] args)
    checkForAllTestCase();
 
// This code is contributed by 29AjayKumar

Python3




# Python3 program to check the nature of the roots
 
# BVA for nature of roots of a quadratic equation
def nature_of_roots(a, b, c):
 
    # If a = 0, D/2a will yield exception
    # Hence it is not a valid Quadratic Equation
    if (a == 0):
        print("Not a Quadratic Equation");
        return;
     
    D = b * b - 4 * a * c;
 
    # If D > 0, it will be Real Roots
    if (D > 0):
        print("Real Roots");
     
    # If D == 0, it will be Equal Roots
    elif(D == 0):
        print("Equal Roots");
     
    # If D < 0, it will be Imaginary Roots
    else:
        print("Imaginary Roots");
     
# Function to check for all testcases
def checkForAllTestCase():
 
    print("Testcase\ta\tb\tc\tActual Output");
    print();
    a = b = c = 0;
    testcase = 1;
    while (testcase <= 13):
        if (testcase == 1):
            a = 0;
            b = 50;
            c = 50;
        elif(testcase == 2):
            a = 1;
            b = 50;
            c = 50;
        elif(testcase == 3):
            a = 50;
            b = 50;
            c = 50;
        elif(testcase == 4):
            a = 99;
            b = 50;
            c = 50;
        elif(testcase == 5):
            a = 100;
            b = 50;
            c = 50;
        elif(testcase == 6):
            a = 50;
            b = 0;
            c = 50;
        elif(testcase == 7):
            a = 50;
            b = 1;
            c = 50;
        elif(testcase == 8):
            a = 50;
            b = 99;
            c = 50;
        elif(testcase == 9):
            a = 50;
            b = 100;
            c = 50;
        elif(testcase == 10):
            a = 50;
            b = 50;
            c = 0;
        elif(testcase == 11):
            a = 50;
            b = 50;
            c = 1;
        elif(testcase == 12):
            a = 50;
            b = 50;
            c = 99;
        elif(testcase == 13):
            a = 50;
            b = 50;
            c = 100;
         
        print("\t" , testcase , "\t" , a , "\t" , b , "\t" , c , "\t", end="");
        nature_of_roots(a, b, c);
        print();
        testcase += 1;
     
# Driver Code
if __name__ == '__main__':
    checkForAllTestCase();
 
# This code is contributed by 29AjayKumar

C#




// C# program to check the nature of the roots
using System;
 
class GFG
 
// BVA for nature of roots of a quadratic equation
static void nature_of_roots(int a, int b, int c)
 
    // If a = 0, D/2a will yield exception
    // Hence it is not a valid Quadratic Equation
    if (a == 0)
    
        Console.Write("Not a Quadratic Equation"
                       +"\n");
        return;
    
 
    int D = b * b - 4 * a * c;
 
    // If D > 0, it will be Real Roots
    if (D > 0)
        Console.Write("Real Roots" +"\n");
    
 
    // If D == 0, it will be Equal Roots
    else if (D == 0)
        Console.Write("Equal Roots" +"\n");
    
 
    // If D < 0, it will be Imaginary Roots
    else
        Console.Write("Imaginary Roots" +"\n");
    
 
// Function to check for all testcases
static void checkForAllTestCase()
 
    Console.Write("Testcase"
        + "\ta\tb\tc\tActual Output"
        +"\n");
    Console.WriteLine();
    int a, b, c;
    a = b = c = 0;
    int testcase = 1;
    while (testcase <= 13)
        if (testcase == 1)
            a = 0;
            b = 50;
            c = 50;
        
        else if (testcase == 2)
            a = 1;
            b = 50;
            c = 50;
        
        else if (testcase == 3)
            a = 50;
            b = 50;
            c = 50;
        
        else if (testcase == 4)
            a = 99;
            b = 50;
            c = 50;
        
        else if (testcase == 5)
            a = 100;
            b = 50;
            c = 50;
        
        else if (testcase == 6)
            a = 50;
            b = 0;
            c = 50;
        
        else if (testcase == 7)
            a = 50;
            b = 1;
            c = 50;
        
        else if (testcase == 8)
            a = 50;
            b = 99;
            c = 50;
        
        else if (testcase == 9)
            a = 50;
            b = 100;
            c = 50;
        
        else if (testcase == 10)
            a = 50;
            b = 50;
            c = 0;
        
        else if (testcase == 11)
            a = 50;
            b = 50;
            c = 1;
        
        else if (testcase == 12)
            a = 50;
            b = 50;
            c = 99;
        
        else if (testcase == 13)
            a = 50;
            b = 50;
            c = 100;
        
        Console.Write("\t" + testcase+ "\t"
                        + a+ "\t" + b+ "\t"
                        + c+ "\t");
        nature_of_roots(a, b, c);
        Console.WriteLine();
        testcase++;
    
 
// Driver Code
public static void Main(String[] args)
    checkForAllTestCase();
 
// This code is contributed by 29AjayKumar

Javascript




// JavaScript program to check the nature of the roots
 
 
// BVA for nature of roots of a quadratic equation
function nature_of_roots(a, b, c)
 
    // If a = 0, D/2a will yield exception
    // Hence it is not a valid Quadratic Equation
    if (a == 0)
        console.log("Not a Quadratic Equation")
        return;
    
 
    let D = b * b - 4 * a * c;
 
    // If D > 0, it will be Real Roots
    if (D > 0)
        console.log("Real Roots");
    
 
    // If D == 0, it will be Equal Roots
    else if (D == 0)
        console.log("Equal Roots");
    
 
    // If D < 0, it will be Imaginary Roots
    else
        console.log("Imaginary Roots");
    
 
// Function to check for all testcases
function checkForAllTestCase()
 
    console.log("Testcase\ta\tb\tc\tActual Output\n");
    let a, b, c;
    let testcase = 1;
    while (testcase <= 13)
        if (testcase == 1)
            a = 0;
            b = 50;
            c = 50;
        
        else if (testcase == 2)
            a = 1;
            b = 50;
            c = 50;
        
        else if (testcase == 3)
            a = 50;
            b = 50;
            c = 50;
        
        else if (testcase == 4)
            a = 99;
            b = 50;
            c = 50;
        
        else if (testcase == 5)
            a = 100;
            b = 50;
            c = 50;
        
        else if (testcase == 6)
            a = 50;
            b = 0;
            c = 50;
        
        else if (testcase == 7)
            a = 50;
            b = 1;
            c = 50;
        
        else if (testcase == 8)
            a = 50;
            b = 99;
            c = 50;
        
        else if (testcase == 9)
            a = 50;
            b = 100;
            c = 50;
        
        else if (testcase == 10)
            a = 50;
            b = 50;
            c = 0;
        
        else if (testcase == 11)
            a = 50;
            b = 50;
            c = 1;
        
        else if (testcase == 12)
            a = 50;
            b = 50;
            c = 99;
        
        else if (testcase == 13)
            a = 50;
            b = 50;
            c = 100;
        
        process.stdout.write("\ttestcase\t" + a + "\t" + b + "\t" + c + "\t");
        nature_of_roots(a, b, c);
        testcase++;
    
 
// Driver Code
checkForAllTestCase();
 
 
// This code is contributed bt phasing17

Output:
Testcase    a    b    c    Actual Output

    1    0    50    50    Not a Quadratic Equation

    2    1    50    50    Real Roots

    3    50    50    50    Imaginary Roots

    4    99    50    50    Imaginary Roots

    5    100    50    50    Imaginary Roots

    6    50    0    50    Imaginary Roots

    7    50    1    50    Imaginary Roots

    8    50    99    50    Imaginary Roots

    9    50    100    50    Equal Roots

    10    50    50    0    Real Roots

    11    50    50    1    Real Roots

    12    50    50    99    Imaginary Roots

    13    50    50    100    Imaginary Roots

Time complexity: O(1)
Auxiliary space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

https://neveropen.tech/boundary-value-analysis-nature-of-roots-of-a-quadratic-equation/?feed_id=14&_unique_id=683ca4eee7055

Comments

Popular posts from this blog

Bare Metal Billing Client Portal Guide

Contents Order a Bare Metal Server My Custom / Contract Pricing View Contract Details Location Management Order History & Status View Order Details Introduction The phoenixNAP Client Portal allows you to purchase bare metal servers and other phoenixNAP products and services. Using the intuitive interface and its essential tools, you can also easily manage your infrastructure. This quick guide will show you how to use the new form to order a bare metal server and how to navigate through new bare metal features within the phoenixNAP Client Portal. Order a Bare Metal Server An order form is an accordion-based process for purchasing phoenixNAP products. Our order form allows you to view the pricing and order multiple products from the same category at the same time. Note: The prices on the form are per month . A contract is not required. However, if you want a contracted price, you may be eligible for a discount depending on the quantity and ...

Add an element in Array to make the bitwise XOR as K

Given an array arr[] containing N positive integers, the task is to add an integer such that the bitwise Xor of the new array becomes K. Examples: Input: arr[] = 1, 4, 5, 6, K = 4 Output: 2 Explanation: Bit-wise XOR of the array is 6.  And bit-wise XOR of 6 and 2 is 4. Input: arr[] = 2, 7, 9, 1, K = 5 Output: 8   Approach: The solution to the problem is based on the following idea of bitwise Xor: If for two numbers X and Y , the bitwise Xor of X and Y is Z then the bitwise Xor of X and Z is Y. Follow the steps to solve the problem: Let the bitwise XOR of the array elements be X .  Say the required value to be added is Y such that X Xor Y = K . From the above observation, it is clear that the value to be added (Y) is the same as X Xor K . Below is the implementation of the above approach: C++ // C++ code to implement the above approach   #include using namespace std;   // Function to find the required value int find_...

Mahotas – Template Matching

In this article we will see how we can do template matching in mahotas. Template is basically a part or structure of image. In this tutorial we will use “lena” image, below is the command to load it.   mahotas.demos.load('lena') Below is the lena image      In order to do this we will use mahotas.template_match method Syntax : mahotas.template_match(img, template) Argument : It takes image object and template as argument Return : It returns image object    Note : Input image should be filtered or should be loaded as grey In order to filter the image we will take the image object which is numpy.ndarray and filter it with the help of indexing, below is the command to do this   image = image[:, :, 0] Below is the implementation    Python3 # importing required libraries import mahotas import mahotas.demos from pylab import gray, imshow, show import numpy as np import matplotlib.pyplot as plt      # loading image ...