📜  查找给定电话号码的最后两位

📅  最后修改于: 2021-04-22 00:45:16             🧑  作者: Mango

给定电话号码的八位数作为整数N ,任务是查找丢失的后两位数,并在后两位数是给定八位数之和时打印完整号码。
例子:

方法:

  • 使用Modulo 10运算符(%10)从N一位一位获得电话号码的八位数字。
  • 将这些数字加到变量say sum中,以得到八个数字的总和。
  • 现在,有两种情况:
    • 如果sum <10 ,则为一位数字,即在开头插入0 ,使其成为两位数,而不会影响该值。
    • 其他总和是由后两位数字表示的数字。

下面是上述方法的实现:

C++
// C++ implementation of the approach
 
#include 
using namespace std;
 
// Function to find the last two
// digits of the number and
// print the complete number
void 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 code
int main()
{
    long int n = 98765432;
 
    findPhoneNumber(n);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
 
// Function to find the last two
// digits of the number and
// print the complete number
static 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 code
public static void main(String[] args)
{
    int n = 98765432;
 
    findPhoneNumber(n);
}
}
 
// This code is contributed by PrinciRaj1992


Python 3
# Python 3 implementation of the approach
 
# Function to find the last two
# digits of the number and
# print the complete number
def 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 code
if __name__ == '__main__':
    n = 98765432
 
    findPhoneNumber(n)
 
# This code is contributed by Surendra_Gangwar


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
// Function to find the last two
// digits of the number and
// print the complete number
static 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 code
static public void Main ()
{
    int n = 98765432;
 
    findPhoneNumber(n);
}
}
 
// This code is contributed by jit_t


Javascript


输出:
9876543244