Dados ocho dígitos de un número de teléfono como un número entero N , la tarea es encontrar los dos últimos dígitos que faltan e imprimir el número completo cuando los dos últimos dígitos son la suma de los ocho dígitos dados.
Ejemplos:
Entrada: N = 98765432
Salida: 9876543244
Entrada: N = 10000000
Salida: 1000000001
Acercarse:
- Obtenga los ocho dígitos del número de teléfono de N uno por uno usando el operador Modulo 10 (%10).
- Suma estos dígitos en una variable, digamos sum , para obtener la suma de los ocho dígitos.
- Ahora bien, hay dos casos:
- Si la suma < 10 , entonces es un solo dígito, es decir, inserte 0 al principio para convertirlo en un número de dos dígitos sin afectar el valor.
- De lo contrario , la suma es el número representado por los dos últimos dígitos.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <iostream> 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
<script> // Javascript implementation of the approach // Function to find the last two // digits of the number and // print the complete number function 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>
Producción:
9876543244
Complejidad de tiempo: O (log 10 n)
Espacio Auxiliar: O(1)