# Addition of two numbers

• 06-10-2009
saswatdash83
Addition of two numbers
I want to write a program to add two numbers, such that the addition result is not going to overflow at anytime.Can somebody help me out.

Thanks.
• 06-10-2009
Sebastiani
>> Can somebody help me out.

Sure. But since this is probably homework anyway, I would ask what have you got so far?
• 06-10-2009
ಠ_ಠ
Quote:

Originally Posted by Sebastiani
>> Can somebody help me out.

Sure. But since this is probably homework anyway, I would ask what have you got so far?

It would be a really stupid homework assignment, seeing how it requires infinite memory
• 06-10-2009
Sebastiani
Ha! True. :p
• 06-10-2009
Salem
You mean like

if ( some_kind_of_test_on_the_operands )
else
-- warn about overflow

Yes, there are ways to do that in a finite machine.

Write the obvious test on paper, and rearrange it a little.
• 06-10-2009
dwks
Probably the only thing you need to know is the highest and lowest numbers that a variable type can hold. For int, you probably have numbers ranging from -2^31 up to 2^31-1, but it may be different on your machine. The easiest way to get at these numbers is to #include <limits.h> and use macros, e.g. INT_MIN and INT_MAX, which will be the values you need.

Actually, you can do the overflow test without knowing INT_MIN or INT_MAX, but knowing them may make it easier.
• 06-11-2009
mostafa faisal
#include<stdio.h>
main()
{
int x,y;
printf("Enter two no.s to ADD:");
scanf("%d%d",&x,&y);
printf("\n%d",x+y);
}
this is what i understand from u
• 06-11-2009
saswatdash83
Quote:

Originally Posted by saswatdash83
I want to write a program to add two numbers, such that the addition result is not going to overflow at anytime.Can somebody help me out.

Thanks.

I was trying with bit shift operation but did not succeed.
• 06-11-2009
saswatdash83
Quote:

Originally Posted by Sebastiani
>> Can somebody help me out.

Sure. But since this is probably homework anyway, I would ask what have you got so far?

Hi Sebastiani,

Could u pls explain a bit.I tried bit shift operation,but did not get the result.
• 06-11-2009
laserlight
What exactly are you trying to do? Answer with respect to Salem's post #5.