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 <bits/stdc++.h> 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 |
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)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!