Dado un número, comprueba si un número dado es divisible por 36 o no. El número puede ser muy grande y puede no caber en ningún tipo de datos numéricos (int, long int, float, etc.).
Ejemplos:
Input : 72 Output : Yes Input : 244 Output : No Input : 11322134 Output : No Input : 92567812197966231384 Output : Yes
Un número es divisible por 36 si el número es divisible por 4 y 9
- Un número es divisible por 4 si el número formado por sus 2 últimas cifras es divisible por 4
- Un número es divisible por 9 si la suma de las cifras del número es divisible por 9
A continuación se muestra la implementación basada en la idea anterior.
C++
// C++ implementation to check divisibility by 36 #include <bits/stdc++.h> using namespace std; // Function to check whether a number // is divisible by 36 or not string divisibleBy36(string num) { int l = num.length(); // null number cannot // be divisible by 36 if (l == 0) return "No"; // single digit number other than // 0 is not divisible by 36 if (l == 1 && num[0] != '0') return "No"; // number formed by the last 2 digits int two_digit_num = (num[l-2] - '0')*10 + (num[l-1] - '0') ; // if number is not divisible by 4 if (two_digit_num%4 != 0) return "No"; // number is divisible by 4 calculate // sum of digits int sum = 0; for (int i=0; i<l; i++) sum += (num[i] - '0'); // sum of digits is not divisible by 9 if (sum%9 != 0) return "No"; // number is divisible by 4 and 9 // hence, number is divisible by 36 return "Yes"; } // Driver program int main() { string num = "92567812197966231384"; cout << divisibleBy36(num); return 0; }
Java
// Java program to find if a number is // divisible by 36 or not class IsDivisible { // Function to check whether a number // is divisible by 36 or not static boolean divisibleBy36(String num) { int l = num.length(); // null number cannot // be divisible by 36 if (l == 0) return false; // single digit number other than // 0 is not divisible by 36 if (l == 1 && num.charAt(0) != '0') return false; // number formed by the last 2 digits int two_digit_num = (num.charAt(l-2) - '0')*10 + (num.charAt(l-1) - '0') ; // if number is not divisible by 4 if (two_digit_num%4 != 0) return false; // number is divisible by 4 calculate // sum of digits int sum = 0; for (int i=0; i<l; i++) sum += (num.charAt(i) - '0'); // sum of digits is not divisible by 9 if (sum%9 != 0) return false; // number is divisible by 4 and 9 // hence, number is divisible by 36 return true; } // main function public static void main (String[] args) { String num = "92567812197966231384"; if(divisibleBy36(num)) System.out.println("Yes"); else System.out.println("No"); } }
Python3
# Python 3 implementation to # check divisibility by 36 # Function to check whether a # number is divisible by # 36 or not def divisibleBy36(num) : l = len(num) # null number cannot # be divisible by 36 if (l == 0) : return ("No") # single digit number other # than 0 is not divisible # by 36 if (l == 1 and num[0] != '0') : return ("No") # number formed by the # last 2 digits two_digit_num = (((int)(num[l - 2])) * 10 +(int)(num[l - 1])) # if number is not # divisible by 4 if (two_digit_num%4 != 0) : return "No" # number is divisible # by 4 calculate sum # of digits sm = 0 for i in range(0,l) : sm = sm + (int)(num[i]) # sum of digits is not # divisible by 9 if (sm%9 != 0) : return ("No") # Number is divisible # by 4 and 9 hence, # number is divisible # by 36 return ("Yes") # Driver program num = "92567812197966231384" print(divisibleBy36(num)) # This code is contributed by Nikita Tiwari.
C#
// C# program to find if a number is // divisible by 36 or not using System; class GFG { // Function to check whether // a number is divisible by // 36 or not static bool divisibleBy36(String num) { int l = num.Length; // null number cannot // be divisible by 36 if (l == 0) return false; // single digit number other than // 0 is not divisible by 36 if (l == 1 && num[0] != '0') return false; // number formed by the last // 2 digits int two_digit_num = (num[l-2] - '0') * 10 + (num[l-1] - '0') ; // if number is not divisible by 4 if (two_digit_num % 4 != 0) return false; // number is divisible by 4 calculate // sum of digits int sum = 0; for (int i = 0; i < l; i++) sum += (num[i] - '0'); // sum of digits is not divisible by 9 if (sum % 9 != 0) return false; // number is divisible by 4 and 9 // hence, number is divisible by 36 return true; } // main function public static void Main () { String num = "92567812197966231384"; if(divisibleBy36(num)) Console.Write("Yes"); else Console.Write("No"); } } // This code is contributed by parashar.
PHP
<?php // PHP implementation to // check divisibility by 36 // Function to check whether a number // is divisible by 36 or not function divisibleBy36($num) { $l = strlen($num); // null number cannot // be divisible by 36 if ($l == 0) return "No"; // single digit number other than // 0 is not divisible by 36 if ($l == 1 && $num[0] != '0') return "No"; // number formed by the // last 2 digits $two_digit_num = ($num[$l - 2] - '0') * 10 + ($num[$l - 1] - '0') ; // if number is not // divisible by 4 if ($two_digit_num%4 != 0) return "No"; // number is divisible by 4 // calculate sum of digits $sum = 0; for ($i = 0; $i < $l; $i++) $sum += ($num[$i] - '0'); // sum of digits is not // divisible by 9 if ($sum % 9 != 0) return "No"; // number is divisible by 4 and 9 // hence, number is divisible by 36 return "Yes"; } // Driver Code $num = "92567812197966231384"; echo(divisibleBy36($num)); // This code is contributed by Ajit. ?>
Javascript
<script> // Javascript implementation to // check divisibility by 36 // Function to check whether a number // is divisible by 36 or not function divisibleBy36(num) { let l = num.length; // null number cannot // be divisible by 36 if (l == 0) return "No"; // single digit number other than // 0 is not divisible by 36 if (l == 1 && num[0] != '0') return "No"; // number formed by the // last 2 digits let two_digit_num = (num[l - 2] - '0') * 10 + (num[l - 1] - '0') ; // if number is not // divisible by 4 if (two_digit_num%4 != 0) return "No"; // number is divisible by 4 // calculate sum of digits let sum = 0; for (let i = 0; i < l; i++) sum += (num[i] - '0'); // sum of digits is not // divisible by 9 if (sum % 9 != 0) return "No"; // number is divisible by 4 and 9 // hence, number is divisible by 36 return "Yes"; } // Driver Code let num = "92567812197966231384"; document.write(divisibleBy36(num)); // This code is contributed by _saurabh_jaiswal. </script>
Yes
Complejidad de tiempo: O(n)
Método 2: Comprobar si el número dado es divisible por 36 o no mediante el operador de división de módulo «%».
Python3
# Python code # To check whether the given number is divisible by 36 or not #input n=72 # the above input can also be given as n=input() -> taking input from user # finding given number is divisible by 36 or not if int(n)%36==0: print("Yes") else: print("No") # this code is contributed by gangarajula laxmi
Javascript
<script> // JavaScript code for the above approach // To check whether the given number is divisible by 36 or not //input n = 72 // finding given number is divisible by 36 or not if (n % 36 == 0) document.write("Yes") else document.write("No") // This code is contributed by Potta Lokesh </script>
Yes
Este artículo es una contribución de Ayush Jauhari . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA