any one find a solution for this (addition of two numbers without usin

can any one write a java rogram for adding two numbers without using o

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 2888 Views - Last Post: 08 March 2010 - 04:23 AM Rate Topic: ****- 3 Votes

#1 harishn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-December 07

any one find a solution for this (addition of two numbers without usin

Post icon  Posted 16 December 2007 - 09:45 AM

can any one write the program to add two numbers with out using operators in java

This post has been edited by harishn: 16 December 2007 - 10:55 AM

Is This A Good Question/Topic? 0
  • +

Replies To: any one find a solution for this (addition of two numbers without usin

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: any one find a solution for this (addition of two numbers without usin

Posted 16 December 2007 - 10:04 AM

you need to ask your question in the textarea. Your title is trimmed, thus we have no idea what it is.
Was This Post Helpful? 0
  • +
  • -

#3 ericode  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 112
  • Joined: 09-December 06

Re: any one find a solution for this (addition of two numbers without usin

Posted 16 December 2007 - 04:20 PM

View Postharishn, on 16 Dec, 2007 - 09:45 AM, said:

can any one write the program to add two numbers with out using operators in java



I am thinking that can't be done without at least an assignment operator '=' at some point. What are the exact restrictions on which operators you can and cannot use? Can you simply relocate the addition operator to a method and just call that add method instead?
Was This Post Helpful? 0
  • +
  • -

#4 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: any one find a solution for this (addition of two numbers without usin

Posted 17 December 2007 - 03:31 AM

View Postharishn, on 16 Dec, 2007 - 10:45 AM, said:

can any one write the program to add two numbers with out using operators in java

Short answer: no. At some point you have to use the arithmetic operators.
Was This Post Helpful? 0
  • +
  • -

#5 harishn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-December 07

Re: any one find a solution for this (addition of two numbers without usin

Posted 17 December 2007 - 12:33 PM

View Postharishn, on 16 Dec, 2007 - 09:45 AM, said:

can any one write the program to add two numbers with out using operators in java




u can use "=" operator but not arithmetic operators
Was This Post Helpful? 0
  • +
  • -

#6 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: any one find a solution for this (addition of two numbers without usin

Posted 17 December 2007 - 01:04 PM

On second thought, there might be a way to do this if you work on the bit level. Consider how you'd implement two's complement addition/subtraction and then look at the bit manipulation capabilities of Java. You might be able to use the boolean operators to do this. That's all I'm saying. :)

This post has been edited by Programmist: 17 December 2007 - 01:11 PM

Was This Post Helpful? 0
  • +
  • -

#7 ericode  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 112
  • Joined: 09-December 06

Re: any one find a solution for this (addition of two numbers without usin

Posted 17 December 2007 - 11:00 PM

View Postharishn, on 17 Dec, 2007 - 12:33 PM, said:

View Postharishn, on 16 Dec, 2007 - 09:45 AM, said:

can any one write the program to add two numbers with out using operators in java




u can use "=" operator but not arithmetic operators


Try something along these lines, create a method which takes two int params. Then create two Object arrays (need to use assignment operator unless you make a method which will return a new array) which are the sizes of each param. Then do a for each loop (no operators involved there) over both arrays where you add the elements in the array to list. Then return the size of the list. You'll only need assignment doing it that way. If you go so far as to extend a List class and in the for loops add objects to your implicit list, then you can get away without using any operators (not even assignment).
Was This Post Helpful? 0
  • +
  • -

#8 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: any one find a solution for this (addition of two numbers without usin

Posted 18 December 2007 - 04:16 AM

I think with a switch you can do this stuff quite easily.
Was This Post Helpful? 0
  • +
  • -

#9 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: any one find a solution for this (addition of two numbers without usin

Posted 18 December 2007 - 06:40 AM

View Postericode, on 18 Dec, 2007 - 12:00 AM, said:

View Postharishn, on 17 Dec, 2007 - 12:33 PM, said:

View Postharishn, on 16 Dec, 2007 - 09:45 AM, said:

can any one write the program to add two numbers with out using operators in java




u can use "=" operator but not arithmetic operators


Try something along these lines, create a method which takes two int params. Then create two Object arrays (need to use assignment operator unless you make a method which will return a new array) which are the sizes of each param. Then do a for each loop (no operators involved there) over both arrays where you add the elements in the array to list. Then return the size of the list. You'll only need assignment doing it that way. If you go so far as to extend a List class and in the for loops add objects to your implicit list, then you can get away without using any operators (not even assignment).

definitely a clever use of iteration and less complex that implementing a boolean bit adder (although maybe not as interesting ;)), but you left out something. What happens if one of the numbers is negative? You don't have to answer. Leave this as an exercise for the poster to figure out as you've already given him most of the answer.
Was This Post Helpful? 0
  • +
  • -

#10 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5848
  • View blog
  • Posts: 12,707
  • Joined: 16-October 07

Re: any one find a solution for this (addition of two numbers without usin

Posted 18 December 2007 - 07:08 AM

View Postharishn, on 17 Dec, 2007 - 02:33 PM, said:

u can use "=" operator but not arithmetic operators


You must be very, very specific as to what counts as an opertator. Here a list from java: http://java.sun.com/.../opsummary.html

If it's only "arithmetic operators" that are out, the here's an answer.
int a=8;
int b=9;
int c=a;
for(int i=0; i<b; i++, c++);


Was This Post Helpful? 0
  • +
  • -

#11 hyndavi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-December 08

Re: any one find a solution for this (addition of two numbers without usin

Posted 02 January 2009 - 05:26 AM

View Postharishn, on 16 Dec, 2007 - 08:45 AM, said:

can any one write the program to add two numbers with out using operators in java

Quote

we can use a pipe symbol
but it is applicable only to few numbers

Was This Post Helpful? 0
  • +
  • -

#12 ayman_mastermind  Icon User is offline

  • human.setType("geek");
  • member icon

Reputation: 126
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Re: any one find a solution for this (addition of two numbers without usin

Posted 02 January 2009 - 06:20 AM

@harishn i dont understand why you need to do that... although i think it could be done... in some way, but i am sure it wont be efficient...
Was This Post Helpful? 0
  • +
  • -

#13 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: any one find a solution for this (addition of two numbers without usin

Posted 02 January 2009 - 10:32 AM

I'm assuming this is an assignment on understanding how the processor handles addition as an arithmetic operation.
You can add two numbers using bit-operations AND and XOR (both twice) in combination. It's quite simple but I won't show you as this is clearly homework and you haven't shown any effort to solve it.
Was This Post Helpful? 0
  • +
  • -

#14 Guest_dasdsa*


Reputation:

Re: any one find a solution for this (addition of two numbers without usin

Posted 08 March 2010 - 12:21 AM

int add_no_arithm(int a, int B) {
if (b == 0) return a;
int sum = a ^ b; // add without carrying
int carry = (a & B) << 1; // carry, but donít add
return add_no_arithm(sum, carry); // recurse
}
Was This Post Helpful? 0

#15 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: any one find a solution for this (addition of two numbers without usin

Posted 08 March 2010 - 12:42 AM

View Postdasdsa, on 07 March 2010 - 10:21 PM, said:

int add_no_arithm(int a, int B) {
if (b == 0) return a;
int sum = a ^ b; // add without carrying
int carry = (a & B) << 1; // carry, but donít add
return add_no_arithm(sum, carry); // recurse
}

Please restrain from posting in year old threads.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2