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.

Printable View

- 06-10-2009saswatdash83Addition 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-2009Sebastiani
>> 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ಠ_ಠ
- 06-10-2009Sebastiani
Ha! True. :p

- 06-10-2009Salem
You mean like

if ( some_kind_of_test_on_the_operands )

-- do addition

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-2009dwks
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-2009mostafa 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-2009saswatdash83
- 06-11-2009saswatdash83
- 06-11-2009laserlight
What exactly are you trying to do? Answer with respect to Salem's post #5.