Thomas Hruska wrote: > Mohan S N wrote: >> --- Ananth <[EMAIL PROTECTED]> wrote: >> >>> Thomas Hruska <[EMAIL PROTECTED]> wrote: >>>> parrot_rabbit4u wrote: >>>> > --- In [email protected], senthil kumar >>> <[EMAIL PROTECTED]> >>>> > wrote: >>>> >> #Iinclude<iostream.h> >>>> >> #include<conio.h> >>>> >> main() >>>> >> { >>>> >> int a,b,c; >>>> >> clrscr(); >>>> >> cout<<"enter the number:"; >>>> >> cin>>a>>b; >>>> >> c=a-(-b); >>>> >> cout<<"The add value:"<<c; >>>> >> getch(); >>>> >> return 0; >>>> >> } >>>> >> >>>> >> ur program will definetly work with out using '+' operator. >>>> > thanks for ur this idea. >>>> >>>> This is fundamental math. Of the lightweight elementary school >>> 'Algebra >>>> I' variety. I still don't know why the OP thought they needed to >>> do >>>> this... >>> To answer a trick question? >>> If learning C++, to impress friends and classmates? >>> Should be one of the two. >> Le me throw in my 2p too... >> >> Putting a+b as a - ( -b ) is not impressive. See this: >> >> for(i=0;i<b;i++){ >> a++; >> } >> >> OR >> >> while(b){ >> a++; >> b--; >> } > > Won't work properly if b is negative. At least the first one won't. > The second one might but I'd have to think about it... Even if it does > work, it would take a little bit to exit the loop (depending on > sizeof(int) among other things).
The second example will probably work on Intel architecture. Let's take a simple example: Assume these are the valid values for 'int': -2, -1, 0, and 1. (Basically a 2-bit signed int). Test for a = 0, b = -1. a = 0, b = -1 a = 1, b = -2 a = -2, b = 1 a = -1, b = 0 In theory it should work for normal 'int's. But this is depending on a lot of different factors. Don't rely on such behavior. Ever. -- Thomas Hruska CubicleSoft President Ph: 517-803-4197 *NEW* MyTaskFocus 1.1 Get on task. Stay on task. http://www.CubicleSoft.com/MyTaskFocus/
