# Addition of two numbers

This is a discussion on Addition of two numbers within the C Programming forums, part of the General Programming Boards category; I want to write a program to add two numbers, such that the addition result is not going to overflow ...

1. ## 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.

2. >> Can somebody help me out.

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

3. 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

4. Ha! True.

5. 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.

6. 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.

7. #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

8. 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.

9. 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.

10. What exactly are you trying to do? Answer with respect to Salem's post #5.

Popular pages Recent additions