Dado un número entero N , la tarea es verificar si la suma de los dígitos del número dado es divisible por todos sus dígitos o no. Si es divisible, imprima Sí ; de lo contrario, imprima No.
Ejemplos:
Entrada: N = 12
Salida: No
Suma de dígitos = 1 + 2 = 3
3 es divisible por 1 pero no por 2.Entrada: N = 123
Salida: Sí
Enfoque: primero encuentre la suma de los dígitos del número y luego verifique uno por uno si la suma calculada es divisible por todos los dígitos del número. Si para algún dígito no es divisible, imprima No ; de lo contrario, imprima Sí .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function that returns true if all the digits // of n divide the sum of the digits of n bool isDivisible(long long int n) { // Store a copy of the original number long long int temp = n; // Find the sum of the digits of n int sum = 0; while (n) { int digit = n % 10; sum += digit; n /= 10; } // Restore the original value n = temp; // Check if all the digits divide // the calculated sum while (n) { int digit = n % 10; // If current digit doesn't // divide the sum if (sum % digit != 0) return false; n /= 10; } return true; } // Driver code int main() { long long int n = 123; if (isDivisible(n)) cout << "Yes"; else cout << "No"; return 0; }
Java
// Java implementation of the approach class GFG { // Function that returns true if all the digits // of n divide the sum of the digits of n static boolean isDivisible(long n) { // Store a copy of the original number long temp = n; // Find the sum of the digits of n int sum = 0; while (n != 0) { int digit = (int) n % 10; sum += digit; n /= 10; } // Restore the original value n = temp; // Check if all the digits divide // the calculated sum while (n != 0) { int digit = (int)n % 10; // If current digit doesn't // divide the sum if (sum % digit != 0) return false; n /= 10; } return true; } // Driver code public static void main (String[] args) { long n = 123; if (isDivisible(n)) System.out.println("Yes"); else System.out.println("No"); } } // This code is contributed by AnkitRai01
Python
# Python implementation of the approach # Function that returns true if all the digits # of n divide the sum of the digits of n def isDivisible(n): # Store a copy of the original number temp = n # Find the sum of the digits of n sum = 0 while (n): digit = n % 10 sum += digit n //= 10 # Restore the original value n = temp # Check if all the digits divide # the calculated sum while(n): digit = n % 10 # If current digit doesn't # divide the sum if(sum % digit != 0): return False n //= 10; return True # Driver code n = 123 if(isDivisible(n)): print("Yes") else: print("No")
C#
// C# implementation of the approach using System; class GFG { // Function that returns true if all the digits // of n divide the sum of the digits of n static bool isDivisible(long n) { // Store a copy of the original number long temp = n; // Find the sum of the digits of n int sum = 0; while (n != 0) { int digit = (int) n % 10; sum += digit; n /= 10; } // Restore the original value n = temp; // Check if all the digits divide // the calculated sum while (n != 0) { int digit = (int)n % 10; // If current digit doesn't // divide the sum if (sum % digit != 0) return false; n /= 10; } return true; } // Driver code static public void Main () { long n = 123; if (isDivisible(n)) Console.Write("Yes"); else Console.Write("No"); } } // This code is contributed by @tushil.
Javascript
<script> // Javascript implementation of the approach // Function that returns true if all the // digits of n divide the sum of the digits // of n function isDivisible(n) { // Store a copy of the original number var temp = n; // Find the sum of the digits of n var sum = 0; while (n != 0) { var digit = parseInt(n % 10); sum += digit; n = parseInt(n / 10); } // Restore the original value n = temp; // Check if all the digits divide // the calculated sum while (n != 0) { var digit = parseInt(n % 10); // If current digit doesn't // divide the sum if (sum % digit != 0) return false; n = parseInt(n/10); } return true; } // Driver code var n = 123; if (isDivisible(n)) document.write("Yes"); else document.write("No"); // This code is contributed by todaysgaurav </script>
Yes
Complejidad de tiempo: O(log(N))
Espacio auxiliar: O(1)
Método #2: Usando una string:
- Tenemos que convertir el número dado en una string tomando una nueva variable.
- Atraviese la string, convierta cada elemento en un número entero y agréguelo a la suma.
- Atraviesa la cuerda de nuevo
- Comprueba si la suma no es divisible por ninguno de los dígitos
- Si es cierto, devuelve Falso
- De lo contrario, devuelve True
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of above approach #include <bits/stdc++.h> using namespace std; string getResult(int n) { // Converting integer to string string st = to_string(n); // Initialising sum to 0 int sum = 0; int length = st.size(); // Traversing through the string for (int i = 0; i < st.size(); i++) { // Converting character to int sum = sum + (int(st[i]) - 48); } // Comparing number and sum // Traversing again for (int i = 0; i < st.size(); i++) { // Check if any digit is // not dividing the sum if (sum % (int(st[i]) - 48) != 0) { // Return false return "No"; } // If any value is not returned // then all the digits are dividing the sum // SO return true else { return "Yes"; } } } // Driver Code int main() { int n = 123; // passing this number to get result function cout << getResult(n); return 0; } // This code is contributed by subhamkumarm348.
Java
// Java implementation of the approach import java.io.*; import java.util.*; class GFG { // Function that returns true if all the digits // of n divide the sum of the digits of n public static String getResult(int n) { // Converting integer to string String st = String.valueOf(n); // Initialising sum to 0 int sum = 0; int length = st.length(); // Traversing through the string for (int i = 0; i < length; i++) { // Converting character to int int c = st.charAt(i)-48; sum += c; } // Comparing number and sum // Traversing again for (int i = 0; i < length; i++) { // Check if any digit is // not dividing the sum int c = st.charAt(i)-48; if (sum % c != 0) { // Return false return "No"; } // If any value is not returned // then all the digits are dividing the sum // SO return true else { return "Yes"; } } return "No"; } // Driver code public static void main (String[] args) { int n = 123; // passing this number to get result function System.out.println(getResult(n)); } } // This code is contributed by aditya942003patil
Python3
# Python implementation of above approach def getResult(n): # Converting integer to string st = str(n) # Initialising sum to 0 sum = 0 length = len(st) # Traversing through the string for i in st: # Converting character to int sum = sum + int(i) # Comparing number and sum # Traversing again for i in st: # Check if any digit is # not dividing the sum if(sum % int(i) != 0): # Return false return 'No' # If any value is not returned # then all the digits are dividing the sum # SO return true return 'Yes' # Driver Code n = 123 # passing this number to get result function print(getResult(n)) # this code is contributed by vikkycirus
C#
// C# program to implement above approach using System; using System.Collections.Generic; class GFG { static string getResult(int n) { // Converting integer to string string st = n.ToString(); // Initialising sum to 0 int sum = 0; int length = st.Length; // Traversing through the string for (int i = 0; i < length; i++) { // Converting character to int sum = sum + ((int)st[i] - 48); } // Comparing number and sum // Traversing again for (int i = 0; i < length; i++) { // Check if any digit is // not dividing the sum if (sum % ((int)st[i] - 48) != 0) { return "No"; } // If any value is not returned // then all the digits are dividing the sum // SO return true else { return "Yes"; } } return "No"; } // Driver Code public static void Main(string[] args){ int n = 123; // Function call Console.Write(getResult(n)); } } // This code is contributed by adityapatil12.
Javascript
<script> // JavaScript implementation of above approach function getResult(n){ // Converting integer to string var st = n.toString(); // Initialising sum to 0 var sum = 0 var length = st.length; // Traversing through the string for (var i= 0 ;i<st.length ; i++){ // Converting character to int sum = sum + Number(st[i]) } // Comparing number and sum // Traversing again for( var i = 0 ; i < st.length ; i++){ // Check if any digit is // not dividing the sum if(sum % Number(st[i]) != 0){ // Return false return 'No' } // If any value is not returned // then all the digits are dividing the sum // SO return true else{ return 'Yes'; } } } // Driver Code var n = 123; // passing this number to get result function document.write(getResult(n)); </script>
Yes
Complejidad de tiempo: O(n), donde n representa la longitud de la string dada.
Espacio auxiliar: O(d), donde d representa el número de dígitos en la string.
Publicación traducida automáticamente
Artículo escrito por SHUBHAMSINGH10 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA