Find the missing value from the given equation a + b = c

• Difficulty Level : Medium
• Last Updated : 20 May, 2021

Given an equation of the form:

a + b = c

Out of which any one of the terms  or is missing. The task is to find the missing term.

Examples:

Input: 2 + 6 = ?
Output: 8

Input: ? + 3 =6
Output: 3

Approach: Missing numbers can be found simply using the equation . First, we will find two known numbers from the given equation(read as a string in the program) and convert them into integers, and put into the equation. In this way, we can find the third missing number. We can implement it by storing the equation into the string.

Below is the step by step algorithm:

• Split the string into smaller strings from the position of spaces and store in an array. So that the array will contain:
arr = "a"
arr = "+"
arr = "b"
arr = "="
arr = "c"
• The missing character can occur at position 0 or 2 or 4 in the vector. Find the position of missing character.
• Convert known characters to integers.
• Find missing character using the equation.

Below is the implementation of the above approach:

C++

 // C++ program to find the missing number// in the equation a + b = c#include using namespace std; // Function to find the missing number// in the equation a + b = cint findMissing(string str){    // Array of string to store individual strings    // after splitting the strings from spaces    string arrStr;         // Using stringstream to read a string object    // and split    stringstream ss(str);         int i = 0;         while (ss.good() && i < 5) {        ss >> arrStr[i];        ++i;    }         int pos = -1;         // Find position of missing character    if(arrStr == "?")        pos = 0;    else if(arrStr == "?")        pos = 2;    else        pos = 4;         if(pos == 0)    {        string b,c;        b = arrStr;        c = arrStr;                 // Using stoi() to convert strings to int        int a = stoi(c) - stoi(b);                 return a;    }    else if(pos==2)    {        string a,c;        a = arrStr;        c = arrStr;                 // Using stoi() to convert strings to int        int b = stoi(c) - stoi(a);                 return b;    }    else if(pos == 4)    {        string b,a;        a = arrStr;        b = arrStr;                 // Using stoi() to convert strings to int        int c = stoi(a) + stoi(b);                 return c;    }} // Driver codeint main(){    // Equation with missing value    string str = "? + 3 = 7";         cout<

Java

 // Java program to find the missing number// in the equation a + b = cimport java.util.*; class GFG{ // Function to find the missing number// in the equation a + b = cstatic int findMissing(String str){         // Array of String to store individual    // strings after splitting the strings    // from spaces    String arrStr[] = str.split(" ");     int pos = -1;          // Find position of missing character    if (arrStr.equals("?"))        pos = 0;    else if (arrStr.equals("?"))        pos = 2;    else        pos = 4;             if (pos == 0)    {        String b, c;        b = arrStr;        c = arrStr;                  // Using Integer.parseInt() to        // convert strings to int        int a = Integer.parseInt(c) -                Integer.parseInt(b);                  return a;    }         else if (pos == 2)    {        String a, c;        a = arrStr;        c = arrStr;                  // Using Integer.parseInt() to        // convert strings to int        int b = Integer.parseInt(c) -                Integer.parseInt(a);                  return b;    }         else if (pos == 4)    {        String b, a;        a = arrStr;        b = arrStr;                  // Using Integer.parseInt() to        // convert strings to int        int c = Integer.parseInt(a) +                Integer.parseInt(b);                  return c;    }    return 0;}  // Driver codepublic static void main(String []args){         // Equation with missing value    String str = "? + 3 = 7";          System.out.print(findMissing(str));}} // This code is contributed by pratham76

Python3

 # Python3 program to find the missing number# in the equation a + b = c   # Function to find the missing number# in the equation a + b = cdef findMissing(s):     # Array of string to store individual strings    # after splitting the strings from spaces    arrStr = s.split()           # Using stringstream to read a string object    # and split    pos = -1;           # Find position of missing character    if(arrStr == "?"):        pos = 0;    elif(arrStr == "?"):        pos = 2;    else:        pos = 4;           if(pos == 0):                 b = arrStr;        c = arrStr;                   # Using int() to convert strings to int        a = int(c) - int(b);                   return a;         elif(pos == 2):             a = arrStr;        c = arrStr;                   # Using int() to convert strings to int        b = int(c) - int(a);                   return b;         elif(pos == 4):             a = arrStr;        b = arrStr;                   # Using int() to convert strings to int        c = int(a) + int(b);                   return c;       # Driver codeif __name__=='__main__':         # Equation with missing value    s = "? + 3 = 7";         print(findMissing(s))     # This code is contributed by rutvik_56

C#

 // C# program to find the missing number// in the equation a + b = cusing System;class GFG{     // Function to find the missing number    // in the equation a + b = c    static int findMissing(string str)    {         // Array of String to store individual        // strings after splitting the strings        // from spaces        string[] arrStr = str.Split(" ");        int pos = -1;         // Find position of missing character        if (arrStr.Equals("?"))            pos = 0;        else if (arrStr.Equals("?"))            pos = 2;        else            pos = 4;        if (pos == 0)        {            string b, c;            b = arrStr;            c = arrStr;             // Using Integer.parseInt() to            // convert strings to int            int a = int.Parse(c) - int.Parse(b);            return a;        }        else if (pos == 2)        {            string a, c;            a = arrStr;            c = arrStr;             // Using Integer.parseInt() to            // convert strings to int            int b = int.Parse(c) - int.Parse(a);            return b;        }         else if (pos == 4)        {            string b, a;            a = arrStr;            b = arrStr;             // Using Integer.parseInt() to            // convert strings to int            int c = int.Parse(a) + int.Parse(b);            return c;        }        return 0;    }     // Driver code    public static void Main(string[] args)    {         // Equation with missing value        string str = "? + 3 = 7";        Console.WriteLine(findMissing(str));    }} // This code is contributed by chitranayal.

Javascript


Output:
4

My Personal Notes arrow_drop_up