Dado un número entero N, la tarea es verificar si el número es divisible por la suma de sus dígitos o no. Si es divisible, escriba «SÍ», de lo contrario, escriba «NO».
Ejemplos:
Input: N = 12 Output: YES Explanation: As sum of digits of 12 = 1 + 2 = 3 and 12 is divisible by 3 So the output is YES Input: N = 123 Output: NO
Planteamiento: La idea para resolver el problema es extraer los dígitos del número y sumarlos. Luego verifica si el número es divisible por la suma de su dígito. Si es divisible, escriba SÍ; de lo contrario, escriba NO.
A continuación se muestra la implementación del enfoque anterior:
Implementación:
C++
// C++ implementation of above approach #include <bits/stdc++.h> using namespace std; // Function to check // if the given number is divisible // by sum of its digits string isDivisible(long long int n) { long long int temp = n; // Find sum of digits int sum = 0; while (n) { int k = n % 10; sum += k; n /= 10; } // check if sum of digits divides n if (temp % sum == 0) return "YES"; return "NO"; } // Driver Code int main() { long long int n = 123; cout << isDivisible(n); return 0; }
Java
// Java implementation of above approach class GFG { // Function to check if the // given number is divisible // by sum of its digits static String isDivisible(long n) { long temp = n; // Find sum of digits int sum = 0; while (n != 0) { int k = (int) n % 10; sum += k; n /= 10; } // check if sum of digits divides n if (temp % sum == 0) return "YES"; return "NO"; } // Driver Code public static void main(String []args) { long n = 123; System.out.println(isDivisible(n)); } } // This code is contributed by Ryuga
Python3
# Python 3 implementation of above approach # Function to check if the given number # is divisible by sum of its digits def isDivisible(n): temp = n # Find sum of digits sum = 0; while (n): k = n % 10; sum += k; n /= 10; # check if sum of digits divides n if (temp % sum == 0): return "YES"; return "NO"; # Driver Code n = 123; print(isDivisible(n)); # This code is contributed by # Akanksha Rai
C#
// C# implementation of above approach using System; class GFG { // Function to check if the // given number is divisible // by sum of its digits static String isDivisible(long n) { long temp = n; // Find sum of digits int sum = 0; while (n != 0) { int k = (int) n % 10; sum += k; n /= 10; } // check if sum of digits divides n if (temp % sum == 0) return "YES"; return "NO"; } // Driver Code public static void Main() { long n = 123; Console.WriteLine(isDivisible(n)); } } // This code is contributed by anuj_67..
PHP
<?php // PHP implementation of above approach // Function to check if the given number // is divisible by sum of its digits function isDivisible($n) { $temp = $n; // Find sum of digits $sum = 0; while ($n) { $k = $n % 10; $sum += $k; $n = (int)($n / 10); } // check if sum of digits divides n if ($temp % $sum == 0) return "YES"; return "NO"; } // Driver Code $n = 123; print(isDivisible($n)); // This code is contributed // by chandan_jnu ?>
Javascript
<script> // Javascript implementation of above approach // Function to check if the given number // is divisible by sum of its digits function isDivisible(n) { temp = n; // Find sum of digits sum = 0; while (n) { k = n % 10; sum += k; n = parseInt(n / 10); } // Check if sum of digits divides n if (temp % sum == 0) return "YES"; return "NO"; } // Driver Code let n = 123; document.write(isDivisible(n)); // This code is contributed by sravan kumar </script>
NO
Complejidad de tiempo: O (log 10 n)
Espacio Auxiliar: O(1)
Método #2: Usando una string:
- Convierta 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.
- Si el número es divisible por la suma, imprima Sí
- De lo contrario imprimir No
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(long long int n) { // Converting integer to String string st = std::to_string(n); // Initialising sum to 0 int sum = 0; // Traversing through the String for(char i : st) { // Converting character to int sum = sum + (int) i; } // Comparing number and sum if (n % sum == 0) return "Yes"; else return "No"; } // Driver Code int main() { int n = 123; // Passing this number to get result function cout << getResult(n); return 0; } // This code is contributed by 29AjayKumar
Java
// Java implementation of above approach class GFG{ static String getResult(int n) { // Converting integer to String String st = String.valueOf(n); // Initialising sum to 0 int sum = 0; // Traversing through the String for(char i : st.toCharArray()) { // Converting character to int sum = sum + (int) i; } // Comparing number and sum if (n % sum == 0) return "Yes"; else 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 29AjayKumar
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 if (n % sum == 0): return "Yes" else: return "No" # Driver Code n = 123 # passing this number to get result function print(getResult(n)) # this code is contributed by vikkycirus
C#
// C# implementation of above approach using System; public class GFG{ static String getResult(int n) { // Converting integer to String String st = String.Join("",n); // Initialising sum to 0 int sum = 0; // Traversing through the String foreach(char i in st.ToCharArray()) { // Converting character to int sum = sum + (int) i; } // Comparing number and sum if (n % sum == 0) return "Yes"; else return "No"; } // Driver Code public static void Main(String[] args) { int n = 123; // Passing this number to get result function Console.WriteLine(getResult(n)); } } // This code is contributed by Amit Katiyar
Javascript
<script> // JavaScript implementation of above approach function getResult(n) { // Converting integer to String let st = (n).toString(); // Initialising sum to 0 let sum = 0; // Traversing through the String for(let i of st.split("")) { // Converting character to int sum = sum + parseInt(i); } // Comparing number and sum if (n % sum == 0) return "Yes"; else return "No"; } // Driver Code let n = 123; // Passing this number to get result function document.write(getResult(n)+"<br>"); // This code is contributed by unknown2108 </script>
Producción:
No
Complejidad de tiempo: O(N)
Publicación traducida automáticamente
Artículo escrito por sahilshelangia y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA