Addition of two numbers without carry
Last Updated :
28 May, 2022
You are given two positive numbers n and m. You have to find a simple addition of both numbers 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 numbers where we start adding two numbers from rightmost bit (LSB) and add integers from both numbers with the 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 the sum of integers then neglect their carry by taking the 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 the above approach :
C++
#include <bits/stdc++.h>
using namespace std;
int xSum( int n, int m)
{
int res = 0;
int multiplier = 1;
int bit_sum;
while (n || m) {
bit_sum = (n % 10) + (m % 10);
bit_sum %= 10;
res = (bit_sum * multiplier) + res;
n /= 10;
m /= 10;
multiplier *= 10;
}
return res;
}
int main()
{
int n = 8458;
int m = 8732;
cout << xSum(n, m);
return 0;
}
|
Java
import 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;
while ( true ) {
if (n== 0 && m== 0 )
break ;
bit_sum = (n % 10 ) + (m % 10 );
bit_sum %= 10 ;
res = (bit_sum * multiplier) + res;
n /= 10 ;
m /= 10 ;
multiplier *= 10 ;
}
return res;
}
public static void main(String args[])
{
int n = 8458 ;
int m = 8732 ;
System.out.println(xSum(n, m));
}
}
|
Python3
import math
def xSum(n, m) :
res = 0
multiplier = 1
bit_sum = 0
while (n or m) :
bit_sum = ((n % 10 ) +
(m % 10 ))
bit_sum = bit_sum % 10
res = (bit_sum *
multiplier) + res
n = math.floor(n / 10 )
m = math.floor(m / 10 )
multiplier = multiplier * 10
return res
n = 8458
m = 8732
print (xSum(n, m))
|
C#
using System;
public class GfG {
public static int xSum( int n, int m)
{
int res = 0;
int multiplier = 1;
int bit_sum;
while ( true ) {
bit_sum = (n % 10) + (m % 10);
bit_sum %= 10;
res = (bit_sum * multiplier) + res;
n /= 10;
m /= 10;
multiplier *= 10;
if (n == 0)
break ;
if (m == 0)
break ;
}
return res;
}
public static void Main()
{
int n = 8458;
int m = 8732;
Console.WriteLine(xSum(n, m));
}
}
|
PHP
<?php
function xSum( $n , $m )
{
$res = 0;
$multiplier = 1;
$bit_sum ;
while ( $n || $m ) {
$bit_sum = ( $n % 10) +
( $m % 10);
$bit_sum %= 10;
$res = ( $bit_sum * $multiplier ) + $res ;
$n = floor ( $n / 10);
$m = floor ( $m / 10);
$multiplier *= 10;
}
return $res ;
}
$n = 8458;
$m = 8732;
echo xSum( $n , $m );
?>
|
Javascript
<script>
function xSum(n, m)
{
var res = 0;
var multiplier = 1;
var bit_sum;
while (n || m) {
bit_sum = (n % 10) + (m % 10);
bit_sum %= 10;
res = (bit_sum * multiplier) + res;
n = parseInt(n / 10);
m = parseInt(m / 10);
multiplier *= 10;
}
return res;
}
var n = 8458;
var m = 8732;
document.write(xSum(n, m));
</script>
|
Output :
6180
Time Complexity: O(MAX(len(n),len(m))), where len(X) is the number of digits in a number X.
Space Complexity: O(1)
Share your thoughts in the comments
Please Login to comment...