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++
// 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 program
int main()
{
int n = 8458;
int m = 8732;
cout << xSum(n, m);
return 0;
}
Java
// Java program for special
// addition of two number
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;
// 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
# Python3 program for special
# addition of two number
import 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 code
n = 8458
m = 8732
print (xSum(n, m))
# This code is contributed by
# Manish Shaw(manishshaw1)
C#
// C# program for special
// addition of two number
using 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
// 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
?>
JavaScript
<script>
// Javascript program for special
// addition of two number
function 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 program
var n = 8458;
var m = 8732;
document.write(xSum(n, m));
// This code is contributed by noob2000.
</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)
Similar Reads
Addition of two numbers without propagating Carry Given 2 numbers a and b of same length. The task is to calculate their sum in such a way that when adding two corresponding positions the carry has to be kept with them only instead of propagating to the left.See the below image for reference: Examples: Input: a = 7752 , b = 8834 Output: 151586 Inpu
15 min read
Add two numbers without using arithmetic operators Given two integers a and b, the task is to find the sum of a and b without using + or - operators. Examples: Input: a = 10, b = 30Output: 40Input: a = -1, b = 2Output: 1Approach:The approach is to add two numbers using bitwise operations. Let's first go through some observations: a & b will have
5 min read
Add two numbers using ++ and/or -- Given two numbers, return a sum of them without using operators + and/or -, and using ++ and/or --.Examples: Input: x = 10, y = 5 Output: 15 Input: x = 10, y = -5 Output: 10 We strongly recommend you to minimize your browser and try this yourself first The idea is to do y times x++, if y is positive
4 min read
Count of carry operations on adding two Binary numbers Given two decimal numbers num1 and num2, the task is to count the number of times carry operation is required while adding the two given numbers in binary form. Examples: Input: num1 = 15, num2 = 10Output: 3Explanation:Give numbers are added as:15 -> 1 1 1 110 -> 1 0 1 0carry -> 1 1 1 - ---
6 min read
Add two numbers represented by two arrays Given two array A[0....n-1] and B[0....m-1] of size n and m respectively, representing two numbers such that every element of arrays represent a digit. For example, A[] = { 1, 2, 3} and B[] = { 2, 1, 4 } represent 123 and 214 respectively. The task is to find the sum of both the number. In above cas
15+ min read