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 number 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[0] = "a" arr[1] = "+" arr[2] = "b" arr[3] = "=" arr[4] = "c"

- The missing character can occur at the position 0 or 2 or 4 in the vector. Find the position of missing character.
- Convert known characters to integer.
- Find missing character using the equation.

Below is the implementation of the above approach:

`// CPP program to find the missing number ` `// in the equation a + b = c ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the missing number ` `// in the equation a + b = c ` `int` `findMissing(string str) ` `{ ` ` ` `// Array of string to store individual strings ` ` ` `// after splitting the strings from spaces ` ` ` `string arrStr[5]; ` ` ` ` ` `// 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[0] == ` `"?"` `) ` ` ` `pos = 0; ` ` ` `else` `if` `(arrStr[2] == ` `"?"` `) ` ` ` `pos = 2; ` ` ` `else` ` ` `pos = 4; ` ` ` ` ` `if` `(pos == 0) ` ` ` `{ ` ` ` `string b,c; ` ` ` `b = arrStr[2]; ` ` ` `c = arrStr[4]; ` ` ` ` ` `// Using stoi() to convert strings to int ` ` ` `int` `a = stoi(c) - stoi(b); ` ` ` ` ` `return` `a; ` ` ` `} ` ` ` `else` `if` `(pos==2) ` ` ` `{ ` ` ` `string a,c; ` ` ` `a = arrStr[0]; ` ` ` `c = arrStr[4]; ` ` ` ` ` `// Using stoi() to convert strings to int ` ` ` `int` `b = stoi(c) - stoi(a); ` ` ` ` ` `return` `b; ` ` ` `} ` ` ` `else` `if` `(pos == 4) ` ` ` `{ ` ` ` `string b,a; ` ` ` `a = arrStr[0]; ` ` ` `b = arrStr[2]; ` ` ` ` ` `// Using stoi() to convert strings to int ` ` ` `int` `c = stoi(a) + stoi(b); ` ` ` ` ` `return` `c; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// Equation with missing value ` ` ` `string str = ` `"? + 3 = 7"` `; ` ` ` ` ` `cout<<findMissing(str); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

4

## Recommended Posts:

- Find count of numbers from 0 to n which satisfies the given equation for a value K
- Least root of given quadratic equation for value greater than equal to K
- Boundary Value Analysis : Nature of Roots of a Quadratic equation
- Sum of minimum value of x and y satisfying the equation ax + by = c
- Find the number of solutions to the given equation
- Find the quadratic equation from the given roots
- Find the equation of plane which passes through two points and parallel to a given axis
- Find the integral roots of a given Cubic equation
- Find the missing digit x from the given expression
- Find the missing elements from 1 to M in given N ranges
- Find the missing elements from 1 to M in given N ranges | Set-2
- Find the last two missing digits of the given phone number
- Find temperature of missing days using given sum and average
- Find all missing numbers from a given sorted array
- Find number of solutions of a linear equation of n variables
- Program to find the Roots of Quadratic equation
- Program to find number of solutions in Quadratic Equation
- Find initial integral solution of Linear Diophantine equation if finite solution exists
- Sort an array after applying the given equation
- Validation of Equation Given as String

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.