Find the last two missing digits of the given phone number
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: 9876543244
Input: 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.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
void findPhoneNumber( int n)
{
int temp = n;
int sum;
while (temp != 0) {
sum += temp % 10;
temp = temp / 10;
}
if (sum < 10)
cout << n << "0" << sum;
else
cout << n << sum;
}
int main()
{
long int n = 98765432;
findPhoneNumber(n);
return 0;
}
|
Java
class GFG
{
static void findPhoneNumber( int n)
{
int temp = n;
int sum = 0 ;
while (temp != 0 )
{
sum += temp % 10 ;
temp = temp / 10 ;
}
if (sum < 10 )
System.out.print(n + "0" + sum);
else
System.out.print(n + "" + sum);
}
public static void main(String[] args)
{
int n = 98765432 ;
findPhoneNumber(n);
}
}
|
Python 3
def findPhoneNumber(n):
temp = n
sum = 0
while (temp ! = 0 ):
sum + = temp % 10
temp = temp / / 10
if ( sum < 10 ):
print (n, "0" , sum )
else :
n = str (n)
sum = str ( sum )
n + = sum
print (n)
if __name__ = = '__main__' :
n = 98765432
findPhoneNumber(n)
|
C#
using System;
class GFG
{
static void findPhoneNumber( int n)
{
int temp = n;
int sum = 0;
while (temp != 0)
{
sum += temp % 10;
temp = temp / 10;
}
if (sum < 10)
Console.Write(n + "0" + sum);
else
Console.Write(n + "" + sum);
}
static public void Main ()
{
int n = 98765432;
findPhoneNumber(n);
}
}
|
Javascript
<script>
function findPhoneNumber(n)
{
let temp = n;
let sum=0;
while (temp != 0) {
sum += temp % 10;
temp = Math.floor(temp / 10);
}
if (sum < 10)
document.write(n + "0" + sum);
else
document.write(n + "" + sum);
}
let n = 98765432;
findPhoneNumber(n);
</script>
|
Time Complexity: O(log10n)
Auxiliary Space: O(1)
Last Updated :
08 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...