Dados dos números N y M . La tarea de convertir ambos números en forma binaria luego agrega los bits respectivos de ambos números binarios convertidos pero con una condición dada de que no hay ningún sistema de acarreo en esta adición.
Input: N = 37, M = 12 Output: 41 Input: N = 456, M = 854 Output: 670
Acercarse:
- Si no consideramos el acarreo, la suma binaria de dos bits será:
1 + 0 = 1 0 + 1 = 1 0 + 0 = 0 1 + 1 = 0 (No carry)
- Si observa claramente, notará que esto es solo XOR bit a bit de dos números.
A continuación se muestra la implementación del enfoque anterior.
C++
// C++ program to add two binary // number without carry #include<bits/stdc++.h> using namespace std; // Function returns sum of both // the binary number without // carry int NoCarrySum(int N, int M) { // XOR of N and M return N ^ M; } // Driver code int main() { int N = 37; int M = 12; cout << NoCarrySum(N, M); return 0; }
Java
// Java program to add two binary // number without carry import java.util.*; class GFG{ // Function returns sum of both // the binary number without // carry static int NoCarrySum(int N, int M) { // XOR of N and M return N ^ M; } // Driver code public static void main(String[] args) { int N = 37; int M = 12; System.out.print(NoCarrySum(N, M)); } } // This code is contributed by amal kumar choubey
Python3
# Python3 program to add two binary # number without carry # Function returns sum of both # the binary number without # carry def NoCarrySum(N, M): # XOR of N and M return N ^ M # Driver code N = 37 M = 12 print(NoCarrySum(N, M)) # This code is contributed by sayesha
C#
// C# program to add two binary // number without carry using System; class GFG{ // Function returns sum of both // the binary number without // carry static int NoCarrySum(int N, int M) { // XOR of N and M return N ^ M; } // Driver code static public void Main(String[] args) { int N = 37; int M = 12; Console.Write(NoCarrySum(N, M)); } } // This code is contributed by Rajput-Ji
Javascript
<script> // Javascript program to add two binary // number without carry // Function returns sum of both // the binary number without // carry function NoCarrySum(N, M) { // XOR of N and M return N ^ M; } // Driver code let N = 37; let M = 12; document.write(NoCarrySum(N, M)); </script>
Producción:
41
Publicación traducida automáticamente
Artículo escrito por anjalisharma02611 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA