Find the last two missing digits of the given phone number

08 Mar, 2022
Given eight digits of a phone number as an integer N, the task is to find the missing last two digits and print the complete number when the last two digits are the sum of given eight digits.Examples:  
Input: N = 98765432 Output: 9876543244Input: N = 10000000 Output: 1000000001  
 
Approach:  
Get the eight digits of the phone number from N one by one using the Modulo 10 operator (%10).
Add these digits in a variable say sum to get the sum of the eight digits.
Now, there are two cases: If sum < 10 then it is a single digit i.e. insert 0 in the beginning to make it a two digit number without affecting the value.Else sum is the number represented by the last two digits.
If sum < 10 then it is a single digit i.e. insert 0 in the beginning to make it a two digit number without affecting the value.
Else sum is the number represented by the last two digits.
Below is the implementation of the above approach:  
C++
Java
Python 3
C#
Javascript
// C++ implementation of the approach #include <iostream>using namespace std; // Function to find the last two// digits of the number and// print the complete numbervoid findPhoneNumber(int n){     int temp = n;    int sum;     // Sum of the first eight    // digits of the number    while (temp != 0) {        sum += temp % 10;        temp = temp / 10;    }     // if sum < 10, then the two digits    // are '0' and the value of sum    if (sum < 10)        cout << n << "0" << sum;     // if sum > 10, then the two digits    // are the value of sum    else        cout << n << sum;} // Driver codeint main(){    long int n = 98765432;     findPhoneNumber(n);     return 0;}
// Java implementation of the approachclass GFG{ // Function to find the last two// digits of the number and// print the complete numberstatic void findPhoneNumber(int n){     int temp = n;    int sum = 0;     // Sum of the first eight    // digits of the number    while (temp != 0)    {        sum += temp % 10;        temp = temp / 10;    }     // if sum < 10, then the two digits    // are '0' and the value of sum    if (sum < 10)        System.out.print(n + "0" + sum);     // if sum > 10, then the two digits    // are the value of sum    else        System.out.print(n +""+ sum);} // Driver codepublic static void main(String[] args){    int n = 98765432;     findPhoneNumber(n);}} // This code is contributed by PrinciRaj1992
# Python 3 implementation of the approach # Function to find the last two# digits of the number and# print the complete numberdef findPhoneNumber(n):    temp = n    sum = 0     # Sum of the first eight    # digits of the number    while (temp != 0):        sum += temp % 10        temp = temp // 10     # if sum < 10, then the two digits    # are '0' and the value of sum    if (sum < 10):        print(n,"0",sum)     # if sum > 10, then the two digits    # are the value of sum    else:        n = str(n)        sum = str(sum)        n += sum        print(n) # Driver codeif __name__ == '__main__':    n = 98765432     findPhoneNumber(n) # This code is contributed by Surendra_Gangwar
// C# implementation of the approachusing System; class GFG{     // Function to find the last two// digits of the number and// print the complete numberstatic void findPhoneNumber(int n){    int temp = n;    int sum = 0;     // Sum of the first eight    // digits of the number    while (temp != 0)    {        sum += temp % 10;        temp = temp / 10;    }     // if sum < 10, then the two digits    // are '0' and the value of sum    if (sum < 10)        Console.Write(n + "0" + sum);     // if sum > 10, then the two digits    // are the value of sum    else        Console.Write(n + "" + sum);} // Driver codestatic public void Main (){    int n = 98765432;     findPhoneNumber(n);}} // This code is contributed by jit_t
<script> // Javascript implementation of the approach // Function to find the last two// digits of the number and// print the complete numberfunction findPhoneNumber(n){     let temp = n;    let sum=0;     // Sum of the first eight    // digits of the number    while (temp != 0) {        sum += temp % 10;        temp = Math.floor(temp / 10);    }     // if sum < 10, then the two digits    // are '0' and the value of sum    if (sum < 10)        document.write(n + "0" + sum);     // if sum > 10, then the two digits    // are the value of sum    else        document.write(n + "" + sum);} // Driver code     let n = 98765432;     findPhoneNumber(n); // This code is contributed by Mayank Tyagi </script>
9876543244
 
Time Complexity: O(log10n)
Auxiliary Space: O(1)
_saitama
SURENDRA_GANGWAR
princiraj1992
jit_t
mayanktyagi1709
subhammahato348
Mathematical
School Programming
Strings
Strings
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Algorithm to solve Rubik's Cube
Program to print prime numbers from 1 to N.
Merge two sorted arrays with O(1) extra space
Segment Tree | Set 1 (Sum of given range)
Fizz Buzz Implementation
Python Dictionary
Reverse a string in Java
Arrays in C/C++
Introduction To PYTHON
Interfaces in Java