CrazyEngineers
  • Addition of 2 positive intgers

    rohitvishnu

    Member

    Updated: Oct 26, 2024
    Views: 1.0K
    hey can anyone add 2 positive integers without using any arithmetic operators. no use of even shift operators.


    😕😕😕
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • shalini_goel14

    MemberApr 5, 2009

    rohitvishnu
    hey can anyone add 2 positive integers without using any arithmetic operators. no use of even shift operators.


    😕😕😕
    Hey is increment operator allowed?

    If so then apply a for loop for smallest integer no. times and increment the other integer's value in it.
    It will finally give you the value.

    package src;
    
    public class AddOperation {
    
        public static void main(String[] args) {
            
    int a=10;
    int b=20;
    for(int i=0; i<10;i++){
        b++;
        }
            System.out.println("Value of sum="+b) ;
    
        
        }
    
    }
    
    Ouput :Value of sum=30
    Are you sure? This action cannot be undone.
    Cancel
  • shalini_goel14

    MemberApr 5, 2009

    Hey rohitvishnu,

    One more way in Java, just got late in recalling this class BigDecimal (Amazing class -Play with it and have fun, its really Good) 😉

    Check following program in Java
    /*
     * AddingIntegersWithoutOperators.java
     *
     * Created on April 6, 2009, 9:59 AM
     *
     */
    package myjava;
    import java.math.BigDecimal;
    /**
     *
     * @author shalinig
     */
    public class AddingIntegersWithoutOperators {
     
        public static void main(String[] args){
     
            BigDecimal firstNo=new BigDecimal(20);
            BigDecimal secondNo=new BigDecimal(30);
            BigDecimal sum;
     
            System.out.println("First Number ="+firstNo);
            System.out.println("Second Number ="+secondNo);
     
            sum=firstNo.add(secondNo);
            System.out.println("Sum of First Number and Second Number ="+sum);
        }
     
    }
     
    
    Output:
    First Number =20
    Second Number =30
    Sum of First Number and Second Number =50
    PS: No ideas in any C, C++ 😔

    Thanks
    Are you sure? This action cannot be undone.
    Cancel
  • pradeep_agrawal

    MemberApr 5, 2009

    I feel the below code will do addition of two positive integers without using arithmetic or shift operators.

    int add(int a, int b) {
      int sum = a;
      int i = 0;
    
      for(i=0; i<b;i++) {
        sum++;
      }
    
      return sum;
     }
    The function add takes two integer a and b as input and return the sum of those integers.

    Note: The above code does not take care of conditions like overflow.

    -Pradeep
    Are you sure? This action cannot be undone.
    Cancel
  • rohitvishnu

    MemberApr 17, 2009

    shalini_goel14
    Hey rohitvishnu,

    One more way in Java, just got late in recalling this class BigDecimal (Amazing class -Play with it and have fun, its really Good) 😉

    Check following program in Java
    /*
     * AddingIntegersWithoutOperators.java
     *
     * Created on April 6, 2009, 9:59 AM
     *
     */
    package myjava;
    import java.math.BigDecimal;
    /**
     *
     * @author shalinig
     */
    public class AddingIntegersWithoutOperators {
     
        public static void main(String[] args){
     
            BigDecimal firstNo=new BigDecimal(20);
            BigDecimal secondNo=new BigDecimal(30);
            BigDecimal sum;
     
            System.out.println("First Number ="+firstNo);
            System.out.println("Second Number ="+secondNo);
     
            sum=firstNo.add(secondNo);
            System.out.println("Sum of First Number and Second Number ="+sum);
        }
     
    }
     
    
    Output:


    PS: No ideas in any C, C++ 😔

    Thanks


    nice try but even the function will ultimately use + operator .

    actually i havent found out the answer myself ..
    i guess we have to use some string concepts

    and no increment operators allowed
    Are you sure? This action cannot be undone.
    Cancel
  • slashfear

    MemberApr 21, 2009

    Hi Rohit,
    Alright so adding two numbers with out using arithmetic operators and even the increment operators. Ok so this is how we do it:

    /* written by : Arvind
    Language : C */
    
    #include <stdio.h>
          int add(int a, int b)
          {
            if (!a)
            return b;
            else
            return add((a & b) << 1, a ^ b);
          }
    
          int main()
          {
            unsigned int a,b;
            printf("Enter the two numbers: \n");
    
            scanf("%d",&a);
            scanf("%d",&b);
            printf("Sum is: %d",add(a,b));
          }
    
    

    And you can do the subtraction in the similar way just by adding the following function to the above code:

     int sub(int a, int b)
          {
              return add(a, add(~b, 1));
          }
    
    and so the final program for performing addition as well as subtraction of two numbers is as shown below:

    /* written by : Arvind
    Language : C */
    
    #include <stdio.h>
          int add(int a, int b)
          {
            if (!a)
            return b;
            else
            return add((a & b) << 1, a ^ b);
          }
    
          int sub(int a, int b)
          {
              return add(a, add(~b, 1));
          }
    
          int main()
          {
            unsigned int a,b;
            printf("Enter the two numbers: \n");
    
            scanf("%d",&a);
            scanf("%d",&b);
            printf("Sum is: %d",add(a,b));
            printf("\nSub is: %d",sub(a,b));
          }
    
    
    
    Alright lets explore the Logic behind the code:

    In digital electronics, an XOR gate is also known as a quarter adder. Basically an addition is performed on each individual bit, and the carry is discarded. All I'm doing here is applying the same concept.

    sum = a ^ b;

    If any bits match in position, then perform the addition on the current sum and the results of the carry.

    In binary arithmetic, subtraction is simply adding the two's complement. The two's complement is simply one added to the one's complement.
    Are you sure? This action cannot be undone.
    Cancel
  • silverscorpion

    MemberApr 21, 2009

    That was fantastic. Good logic.. keep it up.
    Are you sure? This action cannot be undone.
    Cancel
  • pradeep_agrawal

    MemberApr 21, 2009

    That's a piece of good code Slashfear.

    But i thought the initial requirement was to not use even the shift operator while doing this.

    rohitvishnu
    hey can anyone add 2 positive integers without using any arithmetic operators. no use of even shift operators.
    -Pradeep
    Are you sure? This action cannot be undone.
    Cancel
  • shalini_goel14

    MemberMay 7, 2009

    @rohitvishu I guess you cannot do it using Strings, it will require "+" operator and append() will not add them. If you are able to do so, don't forget to share program here. 😀
    Are you sure? This action cannot be undone.
    Cancel
  • slashfear

    MemberMay 8, 2009

    Hey guys,

    This question made me scratch my head for a long time (as well as other CEans like shalini, pradeep, scorpion..... i think😒 ) any way's finally i have found the solution guys Eureka..........😁

    Here is the program to add two numbers using a string class:

    [B]
    /* Written by: Arvind(slashfear)
     Language: C++ */
    
    #include<iostream>
    #include<string>
    using namespace std;
    int add( int i, int j )
    {
    std::string r( i, ' ' ) ;
    r.append( j, ' ' ) ;
    return r.size() ;
    }
    
    int main()
    {
        int i, j;
        cout<<"Enter two number: ";
        cin>>i;
        cin>>j;
        cout<<"The Sum of two numbers is : "<<add(i,j);
    return 0;
    }[/B]
    
    Thanks rohit for the question that filled my head for long time!!!!😉

    And let me know if this is what you where expecting.....;-)

    -Arvind(Slashfear)
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register