The Wayback Machine - https://web.archive.org/web/20240818123338/https://www.geeksforgeeks.org/special-addition-two-number/
Open In App

Addition of two numbers without carry

Last Updated : 28 May, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 Input: a = 123 , b = 456 Output: 579 Approach: First o
15 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 -&gt; 1 1 1 110 -&gt; 1 0 1 0carry -&gt; 1 1 1 - -------------------------------25 -&gt; 1 1 0 0 1 Inp
6 min read
Position of rightmost bit with first carry in sum of two binary
Given two non-negative integers a and b. The problem is to find the position of the rightmost bit where a carry is generated in the binary addition of a and b. Examples: Input : a = 10, b = 2 Output : 2 (10)10 = (1010)2 (2)10 = (10)2. 1010 + 10 As highlighted, 1st carry bit from the right will be generated at position '2'. Input : a = 10, b = 5 Out
4 min read
Minimum boxes required to carry all gifts
Given an array containing weights of gifts, and an integer K representing maximum weight a box can contain (All boxes are uniform). Each box carries at most 2 gifts at the same time, provided the sum of the weight of those gifts is at most limit of box. The task is to find the minimum number of boxes required to carry all gifts. Note: It is guarant
5 min read
BCD addition of given Decimal numbers
Given two numbers A and B, the task is to perform BCD Addition of the given numbers. Examples: Input: A = 12, B = 20 Output: 110010 Explanation: The summation of A and B is 12 + 20 = 32. The binary representation of 3 = 0011 The binary representation of 2 = 0010 Therefore, the BCD Addition is "0011" + "0010" = "110010" Input: A = 10, B = 10 Output:
5 min read
Bitwise recursive addition of two integers
When adding two binary numbers by hand we keep the carry bits in mind and add it at the same time. But to do same thing in program we need a lot of checks. Recursive solution can be imagined as addition of carry and a^b (two inputs) until carry becomes 0. Examples : Input : int x = 45, y = 45 Output : 90 Input : int x = 4, y = 78 Output : 82 Sum of
4 min read
Minimize subset addition or multiplication to make two given triplets equal
Given three integers A, B, C denoting a triplet and three integers P, Q, R denoting another triplet. Repeatedly select any integer and add or multiply it to all elements in a subset (A, B, C) with that integer until the two given triplets become equal. The task is to find the minimum number of such operations required to make the two triplets equal
14 min read
Split N into two integers whose addition to A and B makes them equal
Given three positive integers A, B and N, the task is to split N into two parts such that they become equal i.e. find two positive integers X and Y, such that X + Y = N and A + X = B + Y. If no such pair exists, print -1. Examples: Input: A = 1, B = 3, N = 4 Output: 3 1 Explanation: If X = 3 and Y = 1, then A + X = B + Y and X + Y =4 Input: A = 1,
8 min read
Program for addition of two matrices
Given two N x M matrices. Find a N x M matrix as the sum of given matrices each value at the sum of values of corresponding elements of the given two matrices. Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Approach: Below is the idea to solve the problem. Iterate over every cell of matrix (i, j), add the corres
7 min read
Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
Given an integer N, the task is to print N integers ? 109 such that no two consecutive of these integers are co-prime and every 3 consecutive are co-prime. Examples: Input: N = 3 Output: 6 15 10Input: N = 4 Output: 6 15 35 14 Approach: We can just multiply consecutive primes and for the last number just multiply the gcd(last, last-1) * 2. We do thi
15+ min read
Find HCF of two numbers without using recursion or Euclidean algorithm
Given two integer x and y, the task is to find the HCF of the numbers without using recursion or Euclidean method. Examples: Input: x = 16, y = 32 Output: 16 Input: x = 12, y = 15 Output: 3 Approach: HCF of two numbers is the greatest number which can divide both the numbers. If the smaller of the two numbers can divide the larger number then the H
6 min read
Smallest string without any multiplication sign that represents the product of two given numbers
Given two numbers A and B, the task is to print the string of the smallest possible length which evaluates to the product of the two given numbers i.e., A*B, without using the multiplication sign. Any perfect power of 2 can be expressed in the form of a left-shift operator. For Example: N = 2 = 1&lt;&lt;1 = "&lt;&lt;1" N = 4 = 1&lt;&lt;2 = "&lt;
9 min read
Add two numbers represented by Linked List without any extra space
Given two numbers represented by two linked lists, write a function that returns sum list. The sum list is linked list representation of addition of two input numbers. Expected Space Complexity O(1). Examples: Input: L1 = 5 -&gt; 6 -&gt; 3 -&gt; NULL L2 = 8 -&gt; 4 -&gt; 2 -&gt; NULL Output: 1 -&gt; 4 -&gt; 0 -&gt; 5 -&gt; NULL Input: L1 = 1 -&gt;
11 min read
Add two numbers without using arithmetic operators
Write a function Add() that returns sum of two integers. The function should not use any of the arithmetic operators (+, ++, --, -, .. etc). Method 1: C/C++ Code #include &lt;iostream&gt; using namespace std; int add(int a, int b) { // for loop will start from 1 and move till the value of // second number , first number(a) is incremented in for //
9 min read
Subtract two numbers without using arithmetic operators
Write a function subtract(x, y) that returns x-y where x and y are integers. The function should not use any of the arithmetic operators (+, ++, –, -, .. etc). The idea is to use bitwise operators. Addition of two numbers has been discussed using Bitwise operators. Like addition, the idea is to use subtractor logic. The truth table for the half sub
8 min read
Check if two numbers are equal without using arithmetic and comparison operators
Given two numbers, the task is to check if two numbers are equal without using Arithmetic and Comparison Operators or String functions. Method 1 : The idea is to use XOR operator. XOR of two numbers is 0 if the numbers are the same, otherwise non-zero. C/C++ Code // C++ program to check if two numbers // are equal without using arithmetic // and co
8 min read
Finding LCM of more than two (or array) numbers without using GCD
Given an array of positive integers, find LCM of the elements present in array.Examples: Input : arr[] = {1, 2, 3, 4, 28} Output : 84 Input : arr[] = {4, 6, 12, 24, 30} Output : 120 We have discussed LCM of array using GCD.In this post a different approach is discussed that doesn't require computation of GCD. Below are steps. Initialize result = 1F
9 min read
Check if two numbers are equal without using comparison operators
The following are not allowed to use Comparison Operators String function Examples: Input : num1 = 1233, num2 =1233Output : Same Input : num1 = 223, num2 = 233Output : Not Same Method 1: The idea is to use the XOR operator. XOR of two numbers is 0 if the numbers are the same, otherwise non-zero. C/C++ Code #include &lt;iostream&gt; using namespace
5 min read
Compute average of two numbers without overflow
Given two numbers A and B, such that 0 &lt;= A &lt;= B &lt;= (231 - 1). Compute the average ((A + B) / 2) of the two numbers. Examples: Input: A = 1000000000, B = 2000000000Output: 1500000000.000000Explanation: (A + B)/2 = (1000000000+ 2000000000)/2 = 1500000000.000000 Input: A = 2000000000, B = 2000000001Output: 2000000000.500000Explanation: (A +
5 min read
Javascript program to swap two numbers without using temporary variable
To swap two numbers without using a temporary variable, we have multiple approaches. In this article, we are going to learn how to swap two numbers without using a temporary variable. Below are the approaches used to swap two numbers without using a temporary variable: Table of Content Using Arithmetic OperatorsUsing Bitwise XORA mixture of bitwise
6 min read
To find sum of two numbers without using any operator
Write a program to find sum of positive integers without using any operator. Only use of printf() is allowed. No other library function can be used. Solution It's a trick question. We can use printf() to find sum of two numbers as printf() returns the number of characters printed. The width field in printf() can be used to find the sum of two numbe
9 min read
How to swap two numbers without using a temporary variable?
Given two variables, x, and y, swap two variables without using a third variable.  Method 1 (Using Addition and subtraction)  The idea is to get a sum in one of the two given numbers. The numbers can then be swapped using the sum and subtraction from the sum.  [GFGTABS] C++ // C++ Program to swap two numbers without // using temporary variable #inc
15+ min read
Fast average of two numbers without division
Given two numbers, find floor of their average without using division. Input : x = 10, y = 12Output : 11Input : x = 10, y = 7Output : 8We take floor of sum.The idea is to use right shift operator, instead of doing (x + y)/2, we do (x + y) &gt;&gt; 1 [GFGTABS] C++ // C++ program to find average without using // division. #include &lt;bits/stdc++.h
3 min read
Inorder Tree Traversal without recursion and without stack!
Using Morris Traversal, we can traverse the tree without using stack and recursion. The idea of Morris Traversal is based on Threaded Binary Tree. In this traversal, we first create links to Inorder successor and print the data using these links, and finally revert the changes to restore original tree. 1. Initialize current as root 2. While current
9 min read
Postorder traversal of Binary Tree without recursion and without stack
Given a binary tree, perform postorder traversal. Prerequisite - Inorder/preorder/postorder traversal of tree We have discussed the below methods for postorder traversal. 1) Recursive Postorder Traversal. 2) Postorder traversal using Stack. 2) Postorder traversal using two Stacks. Approach 1 The approach used is based on using an unordered set to k
15 min read
Minimum steps in which N can be obtained using addition or subtraction at every step
Given N, print the sequence of a minimum number of steps in which N can be obtained starting from 0 using addition or subtraction of the step number. Note: At each step, we can add or subtract a number equal to the step number from the current position. For example, at step 1 we can add 1 or -1. Similarly, at step 2 we add 2 or -2 and so on. The be
11 min read
Add N digits to A such that it is divisible by B after each addition
Given three integers A, B and N, repeat the following process N times: Add a digit to A such that after adding it, A is divisible by B.Print the smallest value of A possible after N iterations of above operation.Print -1 if the operation fails. Note : We need to check divisibility after every digit addition.Examples: Input: A = 10, B = 11, N = 1 Ou
5 min read
Find the maximum component size after addition of each edge to the graph
Given an array arr[][] which contains the edges of a graph to be used to construct an undirected graph G with N nodes, the task is to find the maximum component size in the graph after each edge is added while constructing the graph. Examples: Input: N = 4, arr[][] = {{1, 2}, {3, 4}, {2, 3}} Output: 2 2 4 Explanation: Initially, the graph has 4 ind
15+ min read
Modify array by merging elements with addition such that it consists of only Primes.
Given an array arr[] consisting of positive integers, the task is to check whether we can modify the array by adding any of the elements of the array such that it consists of only Primes. Examples: Input: arr[] = {3, 5, 7, 21} Output: YES Explanation: Add the following elements, 3+5+21 So the array becomes {7, 29} which consists of only primes. Inp
10 min read
Number formed after K times repeated addition of smallest divisor of N
Given two integers N and K, the task is to generate the final outcome of performing K operations which involves adding the smallest divisor, other than 1, of the current value of N to it at every step.Example: Input: N = 9, K = 4 Output: 18 Explanation: Divisors of 9 are {1, 3, 9} 1st Operation: N = 9 + 3 = 12 2nd Operation: N = 12 + 2 = 14 3rd Ope
5 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg