Addition of two numbers without carry - GeeksforGeeks

10 May, 2022
You are given two positive number n and m. You have to find simply addition of both number but with a given condition that there is not any carry system in this addition. That is no carry is added at higher MSBs.Examples :  
Input : m = 456, n = 854
Output : 200

Input : m = 456, n = 4
Output : 450
Algorithm :  
Input n, m while(n||m)
    {
        // Add each bits 
        bit_sum = (n%10) + (m%10);

        // Neglect carry
        bit_sum %= 10;

        // Update result
        // multiplier to maintain place value
        res = (bit_sum * multiplier) + res;
        n /= 10;
        m /= 10;

        // Update multiplier
        multiplier *=10;
    } print res
 
Approach : To solve this problem we will need the bit by bit addition of number where we start adding two number from right most bit (LSB) and add integers from both numbers with same position. Also we will neglect carry at each position so that  carry will not affect further higher bit position. Start adding both numbers bit by bit and for each bit take sum of integers then neglect their carry by taking modulo of bit_sum by 10 further add bit_sum to res by multiplying bit_sum with a multiplier specifying place value. (Multiplier got incremented 10 times on each iteration.) Below is the implementation of above approach :  
C++
Java
Python3
C#
PHP
Javascript
// CPP program for special// addition of two number#include <bits/stdc++.h>using namespace std; int xSum(int n, int m){    // variable to store result      int res = 0;     // variable to maintain    // place value    int multiplier = 1;     // variable to maintain    // each digit sum    int bit_sum;     // Add numbers till each    // number become zero    while (n || m) {         // Add each bits        bit_sum = (n % 10) + (m % 10);                 // Neglect carry        bit_sum %= 10;                 // Update result        res = (bit_sum * multiplier) + res;        n /= 10;        m /= 10;                 // Update multiplier        multiplier *= 10;    }    return res;} // Driver programint main(){    int n = 8458;    int m = 8732;    cout << xSum(n, m);    return 0;}
// Java program for special// addition of two numberimport java.util.*;import java.lang.*; public class GfG {     public static int xSum(int n, int m)    {        int res = 0;        int multiplier = 1;        int bit_sum;         // Add numbers till each        // number become zero        while (true) {                             if(n==0 && m==0)            break;             // Add each bits            bit_sum = (n % 10) + (m % 10);             // Neglect carry            bit_sum %= 10;             // Update result            res = (bit_sum * multiplier) + res;            n /= 10;            m /= 10;             // Update multiplier            multiplier *= 10;                   }        return res;    }     // Driver function    public static void main(String args[])    {        int n = 8458;        int m = 8732;        System.out.println(xSum(n, m));    }}/* This code is contributed by Sagar Shukla */
# Python3 program for special# addition of two numberimport math def xSum(n, m) :     # variable to    # store result    res = 0     # variable to maintain    # place value    multiplier = 1     # variable to maintain    # each digit sum    bit_sum = 0     # Add numbers till each    # number become zero    while (n or m) :         # Add each bits        bit_sum = ((n % 10) +                   (m % 10))                 # Neglect carry        bit_sum = bit_sum % 10                 # Update result        res = (bit_sum *               multiplier) + res        n = math.floor(n / 10)        m = math.floor(m / 10)                 # Update multiplier        multiplier = multiplier * 10         return res # Driver coden = 8458m = 8732print (xSum(n, m)) # This code is contributed by# Manish Shaw(manishshaw1)
// C# program for special// addition of two numberusing System; public class GfG {     public static int xSum(int n, int m)    {        int res = 0;        int multiplier = 1;        int bit_sum;         // Add numbers till each        // number become zero        while (true) {             // Add each bits            bit_sum = (n % 10) + (m % 10);             // Neglect carry            bit_sum %= 10;             // Update result            res = (bit_sum * multiplier) + res;            n /= 10;            m /= 10;             // Update multiplier            multiplier *= 10;            if (n == 0)                break;            if (m == 0)                break;        }        return res;    }     // Driver function    public static void Main()    {        int n = 8458;        int m = 8732;        Console.WriteLine(xSum(n, m));    }} /* This code is contributed by Vt_m */
<?php// php program for special// addition of two number function xSum($n, $m){         // variable to store result    $res = 0;     // variable to maintain    // place value    $multiplier = 1;     // variable to maintain    // each digit sum    $bit_sum;     // Add numbers till each    // number become zero    while ($n || $m) {         // Add each bits        $bit_sum = ($n % 10) +                   ($m % 10);                 // Neglect carry        $bit_sum %= 10;                 // Update result        $res = ($bit_sum * $multiplier) + $res;        $n =floor($n / 10);        $m =floor($m / 10);                 // Update multiplier        $multiplier *= 10;    }    return $res;}     // Driver code    $n = 8458;    $m = 8732;    echo xSum($n, $m); //This code is contributed by mits?>
<script> // Javascript program for special// addition of two numberfunction xSum(n, m){    // variable to store result    var res = 0;     // variable to maintain    // place value    var multiplier = 1;     // variable to maintain    // each digit sum    var bit_sum;     // Add numbers till each    // number become zero    while (n || m) {         // Add each bits        bit_sum = (n % 10) + (m % 10);                 // Neglect carry        bit_sum %= 10;                 // Update result        res = (bit_sum * multiplier) + res;        n = parseInt(n / 10);        m = parseInt(m / 10);                 // Update multiplier        multiplier *= 10;    }    return res;} // Driver programvar n = 8458;var m = 8732;document.write(xSum(n, m));         // This code is contributed by noob2000.</script>
Output :  
6180
 
Mithun Kumar
manishshaw1
offbeat
noob2000
simmytarika5
sagartomar9927
Bit Magic
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Little and Big Endian Mystery
Cyclic Redundancy Check and Modulo-2 Division
Binary representation of a given number
Program to find whether a given number is power of 2
Josephus problem | Set 1 (A O(n) Solution)
Bit Fields in C
Set, Clear and Toggle a given bit of a number in C
Find the element that appears once
Bits manipulation (Important tactics)
C++ bitset and its application