Dado un número N representado como una string, la tarea es imprimir ‘Sí’ si la suma de dígitos es par y es divisible por 4 o si la suma de dígitos es impar y es divisible por 3, de lo contrario, ‘No’.
Ejemplos:
Input: 12345 Output: Yes Input: 894561 Output: Yes
A continuación se muestra el algoritmo paso a paso :
- Calcular la suma de todos los dígitos.
- Si la suma es par:
- Comprueba si la suma es divisible por 4
- De lo contrario, si la suma es impar:
- Comprueba si es divisible por 3.
- Escriba Sí, si alguno de los casos en el paso 2 o el paso 3 satisface lo contrario, escriba No.
C++
// C++ implementation of above algorithm #include <bits/stdc++.h> using namespace std; // Function to check the sum bool checkSum(string num) { int sum = 0; // Traverse each digit for (int i = 0; i < num.length(); i++) { // converting a character to integer by // taking difference of their ASCII value int digit = num[i] - '0'; sum += digit; } // Check if sum is even and divisible by 4 // or if sum is odd and divisible by 3 then // return true, else return false if ((sum % 2 == 0 && sum % 4 == 0) || (sum % 2 != 0 && sum % 3 == 0)) return true; return false; } // Driver code int main() { string num = "12347"; checkSum(num) ? cout << "Yes" : cout << "No"; return 0; }
Java
// Java implementation of above algorithm import java.lang.*; class Geeks { // Function to check the sum static boolean checkSum(String num) { int sum = 0; // Traverse each digit for (int i = 0; i < num.length(); i++) { // converting a character to integer by // taking difference of their ASCII value int digit = num.charAt(i) - '0'; sum += digit; } // Check if sum is even and divisible by 4 // or if sum is odd and divisible by 3 then // return true, else return false if ((sum % 2 == 0 && sum % 4 == 0) || (sum % 2 !=0 && sum % 3 == 0)) return true; return false; } // Driver code public static void main(String args[]) { String num = "12347"; System.out.println(checkSum(num) ? "Yes" : "No"); } } // This code is contributed by ankita_saini.
Python 3
# Python 3 implementation of # above algorithm # Function to check the sum def checkSum(num): sum = 0 # Traverse each digit for i in range(len(num)): # converting a character to # integer by taking difference # of their ASCII value digit = ord(num[i]) - ord('0') sum += digit # Check if sum is even and # divisible by 4 or if sum # is odd and divisible by 3 # then return true, else # return false if ((sum % 2 == 0 and sum % 4 == 0) or (sum % 2 != 0 and sum % 3 == 0)): return True return False # Driver code if __name__ == "__main__": num = "12347" print("Yes") if checkSum(num) else print("No") # This code is contributed # by ChitraNayal
C#
// C# implementation of above algorithm using System; class GFG { // Function to check the sum static bool checkSum(String num) { int sum = 0; // Traverse each digit for (int i = 0; i < num.Length; i++) { // converting a character to // integer by taking difference // of their ASCII value int digit = num[i] - '0'; sum += digit; } // Check if sum is even and // divisible by 4 or if sum // is odd and divisible by 3 // then return true, else // return false if ((sum % 2 == 0 && sum % 4 == 0) || (sum % 2 !=0 && sum % 3 == 0)) return true; return false; } // Driver code public static void Main(String []args) { String num = "12347"; Console.WriteLine(checkSum(num) ? "Yes" : "No"); } } // This code is contributed // by ankita_saini.
PHP
<?php // PHP implementation of above algorithm // Function to check the sum function checkSum($num) { $sum = 0; // Traverse each digit for ($i = 0; $i < sizeof($num); $i++) { // converting a character to // integer by taking difference // of their ASCII value $digit = $num[$i] - '0'; $sum += $digit; } // Check if sum is even and divisible // by 4 or if sum is odd and divisible // by 3 then return true, else return false if (($sum % 2 == 0 && $sum % 4 == 0) || ($sum % 2 != 0 && $sum % 3 == 0)) return true; return false; } // Driver code $num = "12347"; if(checkSum($num)) echo "Yes"; else echo "No"; // This code is contributed by // Akanksha Rai(Abby_akku)
Javascript
<script> // JavaScript implementation of above algorithm // Function to check the sum function checkSum(num) { let sum = 0; // Traverse each digit for (let i = 0; i < num.length; i++) { // converting a character to integer by // taking difference of their ASCII value let digit = num.charAt(i) - '0'; sum += digit; } // Check if sum is even and divisible by 4 // or if sum is odd and divisible by 3 then // return true, else return false if ((sum % 2 == 0 && sum % 4 == 0) || (sum % 2 != 0 && sum % 3 == 0)) return true; return false; } // Driver code let num = "12347"; document.write(checkSum(num) ? "Yes" : "No"); // This code is contributed by Surbhi Tyagi. </script>
Producción:
No
Complejidad de tiempo: O(N)
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.
- Si la suma es par, comprueba si la suma es divisible por 4
- De lo contrario, si la suma es impar, compruebe si es divisible por 3.
- Escriba Sí, si alguno de los casos en el paso 3 o el paso 4 satisface lo contrario, escriba No.
A continuación se muestra la implementación del enfoque anterior:
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) if ((sum % 2 == 0 and sum % 4 == 0) or (sum % 2 != 0 and sum % 3 == 0)): return 'Yes' return 'No' # Driver Code n = 202 # passing this number to get result function print(getResult(n)) # this code is contributed by vikkycirus
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(let i=0 ; i< st.length ; i++ ){ // Converting character to int sum = sum + Number(st[i]) } if ((sum % 2 == 0 && sum % 4 == 0) || (sum % 2 != 0 && sum % 3 == 0)){ return 'Yes' } else{ return 'No'; } } // Driver Code var n = 202; // passing this number to get result function document.write(getResult(n)) </script>
Producción:
Yes
Complejidad de tiempo: O(N)
Publicación traducida automáticamente
Artículo escrito por vaarigupta y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA