Un número entero en base 10 que es divisible por la suma de sus dígitos se dice que es un Número Harshad. Un número n-harshad es un número entero divisible por la suma de su dígito en base n .
A continuación se muestran los primeros Números de Harshad representados en base 10:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 18, 20………
Dado un número en base 10, nuestra tarea es para comprobar si es un Número Harshad o no.
Ejemplos:
Input: 3 Output: 3 is a Harshad Number Input: 18 Output: 18 is a Harshad Number Input: 15 Output: 15 is not a Harshad Number
1. Extrae todos los dígitos del número usando el operador % y calcula la suma.
2. Comprueba si el número es divisible por la suma.
A continuación se muestra la implementación de la idea anterior:
C++
// C++ program to check if a number is Harshad // Number or not. #include <bits/stdc++.h> using namespace std; // function to check Harshad Number bool checkHarshad(int n) { // calculate sum of digits int sum = 0; for (int temp = n; temp > 0; temp /= 10) sum += temp % 10; // Return true if sum of digits is multiple // of n return (n % sum == 0); } // driver program to check above function int main() { checkHarshad(12) ? cout << "Yes\n" : cout << "No\n"; checkHarshad(15) ? cout << "Yes\n" : cout << "No\n"; return 0; }
Java
// Java program to check if a number is Harshad // Number or not public class GFG { // method to check Harshad Number static boolean checkHarshad(int n) { // calculate sum of digits int sum = 0; for (int temp = n; temp > 0; temp /= 10) sum += temp % 10; // Return true if sum of digits is multiple // of n return (n % sum == 0); } // Driver program to test above functions public static void main(String[] args) { System.out.println(checkHarshad(12) ? "Yes" : "No"); System.out.println(checkHarshad(15) ? "Yes" : "No"); } }
Python3
# Python program to check # if a number is Harshad # Number or not. def checkHarshad( n ) : sum = 0 temp = n while temp > 0 : sum = sum + temp % 10 temp = temp // 10 # Return true if sum of # digits is multiple of n return n % sum == 0 # Driver Code if(checkHarshad(12)) : print("Yes") else : print ("No") if (checkHarshad(15)) : print("Yes") else : print ("No") # This code is contributed # by Nikita Tiwari
C#
// C# program to check if a number is Harshad // Number or not using System; public class GFG { // method to check Harshad Number static bool checkHarshad(int n) { // calculate sum of digits int sum = 0; for (int temp = n; temp > 0; temp /= 10) sum += temp % 10; // Return true if sum of digits is // multiple of n return (n % sum == 0); } // Driver program to test above functions public static void Main() { Console.WriteLine(checkHarshad(12) ? "Yes" : "No"); Console.WriteLine(checkHarshad(15) ? "Yes" : "No"); } } // This code is contributed by vt_m.
PHP
<?php // php program to check if // a number is Harshad // Number or not. // function to check // Harshad Number function checkHarshad($n) { // calculate sum of digits $sum = 0; for ($temp = $n; $temp > 0; $temp /= 10) $sum += $temp % 10; // Return true if sum of // digits is multiple of n return ($n % $sum == 0); } // Driver Code $k = checkHarshad(12) ? "Yes\n" : "No\n"; echo($k); $k = checkHarshad(15) ? "Yes\n" : "No\n"; echo($k); // This code is contributed by ajit. ?>
Javascript
<script> // Javascript program to check if a number is Harshad Number or not // method to check Harshad Number function checkHarshad(n) { // calculate sum of digits let sum = 0; for (let temp = n; temp > 0; temp = parseInt(temp / 10, 10)) sum += temp % 10; // Return true if sum of digits is // multiple of n return (n % sum == 0); } document.write(checkHarshad(12) ? "Yes" + "</br>" : "No" + "</br>"); document.write(checkHarshad(15) ? "Yes" + "</br>" : "No" + "</br>"); </script>
Producción :
Yes No
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 el número es divisible por la suma, entonces es el número de Harshad.
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 checkHarshad(int n) { // Converting integer to string string st = to_string(n); // Initialising sum to 0 int sum = 0; int length = st.length(); // Traversing through the string for(char i : st) { // Converting character to int sum = sum + (i - '0'); } // Comparing number and sum if (n % sum == 0) { return "Yes"; } else { return "No"; } } // Driver Code int main() { int number = 18; // Passing this number to get result function cout << checkHarshad(number) << endl; } // This code is contributed by rrrtnx
Java
import java.io.*; // java code to check the given number is Harshad or not class GFG { // function to check that given number // is Harshad or not. static String checkHarshad(int n) { // converting the integer to string String st = Integer.toString(n); int sum = 0; // calculating total number of digits // in a number int length=st.length(); // adding the all digits of a number for(int i = 0; i < length; i++){ sum += st.charAt(i)-'0'; } // checking that sum is divisior of n or not if(n % sum == 0){ return "YES"; } else{ return "NO"; } } // driver code public static void main(String args[]){ int number = 18; // function call System.out.println(checkHarshad(number)); } } // This code is contributed by Machhaliya Muhammad
Python3
# Python implementation of above approach def checkHarshad(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 number = 18 # passing this number to get result function print(checkHarshad(number)) # This code is contributed by vikkycirus
C#
// C# program to find the radii // of the three tangent circles // of equal radius when the radius // of the circumscribed circle is given using System; class GFG { static String checkHarshad(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 foreach(char i in st) { // Converting character to int sum = sum + (i - '0'); } // Comparing number and sum if (n % sum == 0) { return "Yes"; } else { return "No"; } } // Driver code public static void Main() { int number = 18; // Passing this number to get result function Console.WriteLine(checkHarshad(number)); } } // This code is contributed by Nidhi goel
Javascript
<script> // Javascript implementation of above approach function checkHarshad(n){ // Converting integer to string let st = String(n) // Initialising sum to 0 let sum = 0 let length = st.length // Traversing through the string for(i in st){ // Converting character to int sum = sum + parseInt(i) } // Comparing number and sum if (n % sum == 0){ return "Yes" } else{ return "No" } } // Driver Code let number = 18 // passing this number to get result function document.write(checkHarshad(number)) // This code is contributed by _saurabh_jaiswal </script>
Yes
Complejidad de tiempo: O(n)
Referencias:
https://en.wikipedia.org/wiki/Harshad_number
Este artículo es una contribución de Harsh Agarwal . 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