Un número malvado es un número no negativo que tiene un número par de 1 en su expansión binaria. (Expansión binaria: es la representación de un número en el sistema de numeración binario o sistema de numeración de base 2 que representa valores numéricos usando dos símbolos diferentes: típicamente 0 (cero) y 1 (uno)).
Números odiosos: Los números que no son malvados se llaman números odiosos. Dado un número, la tarea es verificar si es Evil Number o Odious Numbers.
Ejemplos:
Input : 3 Output : Evil Number Explanation: Binary expansion of 3 is 11, the number of 1s in this is 2 i.e even. Input : 16 Output : Odious Number(not an evil number) Explanation: Binary expansion of 16 = 10000, having number of 1s =1 i.e odd. Input : 23 Output : Evil Number Explanation: Binary expansion of 23 is 10111, the number of 1s in this is 4 i.e even.
C++
// C/C++ program to check if a number is // Evil number or Odious Number #include <iostream> using namespace std; #include <math.h> // returns number of 1s from the binary number int count_one(int n) { int c_one = 0; while (n != 0) { int rem = n % 10; // counting 1s if (rem == 1) c_one = c_one + 1; n = n / 10; } return c_one; } // Check if number is evil or not int checkEvil(int n) { int i = 0, bin = 0, n_one = 0; // converting n to binary form while (n != 0) { // calculating remainder int r = n % 2; // storing the remainders in binary // form as a number bin = bin + r * (int)(pow(10, i)); n = n / 2; } // Calling the count_one function to count // and return number of 1s in bin n_one = count_one(bin); if (n_one % 2 == 0) return 1; else return 0; } // Driver Code int main(void) { int i, check, num; num = 32; check = checkEvil(num); if (check == 1) cout << num << " is Evil Number\n"; else cout << num << " is Odious Number\n"; return 0; } // This code is contributed by Nikita Tiwari.
Java
// Java program to check if a number is // Evil number or Odious Number class GFG { // returns number of 1s from the binary number static int count_one(int n) { int c_one = 0; while (n != 0) { int rem = n % 10; // counting 1s if (rem == 1) c_one = c_one + 1; n = n / 10; } return c_one; } // Check if number is evil or not static int checkEvil(int n) { int i = 0, bin = 0, n_one = 0; // converting n to binary form while (n != 0) { // calculating remainder int r = n % 2; // storing the remainders in binary // form as a number bin = bin + r * (int)(Math.pow(10, i)); n = n / 2; } // Calling the count_one function to count // and return number of 1s in bin n_one = count_one(bin); if (n_one % 2 == 0) return 1; else return 0; } // Driver Code public static void main(String[] args) { int i, check, num; num = 32; check = checkEvil(num); if (check == 1) System.out.println(num + " is Evil Number"); else System.out.println(num + " is Odious Number"); } } /* This code is contributed by Mr. Somesh Awasthi */
Python3
# Python program to check if a number is # Evil number or Odious number # returns number of 1s from the binary number def count_one(n): c_one = 0 while n != 0: rem = n % 10 # Counting 1s if rem == 1: c_one = c_one + 1 n = n // 10 return c_one # Check if number is evil or not def checkEvil(n): i = 0 binary = 0 # Converting n to binary form while n != 0: r = n % 2 # Calculating Remainder # Storing the remainders in binary # form as a number binary = binary + r*(int(10**i)) n = n // 2 # Calling the count_one function to count # and return number of 1s in bin n_one = count_one(binary) if n_one % 2 == 0: return True return False # Driver Code num = 32 check = checkEvil(num) if check: print(num, "is Evil Number") else: print (num, "is Odious Number") # Contributed by Harshit Agrawal
C#
// C# program to check if a number is // Evil number or Odious Number using System; class GFG { // Returns number of 1s from // the binary number static int count_one(int n) { int c_one = 0; while (n != 0) { int rem = n % 10; // counting 1s if (rem == 1) c_one = c_one + 1; n = n / 10; } return c_one; } // Check if number is evil or not static int checkEvil(int n) { int i = 0, bin = 0, n_one = 0; // converting n to binary form while (n != 0) { // calculating remainder int r = n % 2; // storing the remainders in // binary form as a number bin = bin + r * (int)(Math.Pow(10, i)); n = n / 2; } // Calling the count_one function to count // and return number of 1s in bin n_one = count_one(bin); if (n_one % 2 == 0) return 1; else return 0; } // Driver Code public static void Main(String[] args) { int check, num; num = 32; check = checkEvil(num); if (check == 1) Console.WriteLine(num + " is Evil Number"); else Console.WriteLine(num + " is Odious Number"); } } // This code is contributed by vt_m.
PHP
<?php // PHP program to check if // a number is Evil number // or Odious Number // returns number of 1s // from the binary number function count_one($n) { $c_one = 0; while ($n != 0) { $rem = $n % 10; // counting 1s if ($rem == 1) $c_one = $c_one + 1; $n = $n / 10; } return $c_one; } // Check if number // is evil or not function checkEvil($n) { $i = 0; $bin = 0; $n_one = 0; // converting n // to binary form while ($n != 0) { // calculating remainder $r = $n % 2; // storing the remainders // in binary form as a number $bin = $bin + $r * (pow(10, $i)); $n = $n / 2; } // Calling the count_one // function to count and // return number of 1s in bin $n_one = count_one($bin); if ($n_one % 2 == 0) return 1; else return 0; } // Driver Code $i; $check; $num; $num = 32; $check = checkEvil($num); if ($check == 1) echo $num, " is Evil Number\n"; else echo $num, " is Odious Number\n"; // This code is contributed by ajit. ?>
Javascript
<script> // Javascript program to check if a number // is Evil number or Odious Number // Returns number of 1s from // the binary number function count_one(n) { let c_one = 0; while (n != 0) { let rem = n % 10; // Counting 1s if (rem == 1) c_one = c_one + 1; n = parseInt(n / 10, 10); } return c_one; } // Check if number is evil or not function checkEvil(n) { let i = 0, bin = 0, n_one = 0; // Converting n to binary form while (n != 0) { // Calculating remainder let r = n % 2; // Storing the remainders in // binary form as a number bin = bin + r * (Math.pow(10, i)); n = parseInt(n / 2, 10); } // Calling the count_one function to count // and return number of 1s in bin n_one = count_one(bin); if (n_one % 2 == 0) return 1; else return 0; } // Driver code let check, num; num = 32; check = checkEvil(num); if (check == 1) document.write(num + " is Evil Number"); else document.write(num + " is Odious Number"); // This code is contributed by suresh07 </script>
Producción :
32 is Odious Number
Este artículo es una contribución de Nikita Tiwari . 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