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
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.
>> Can somebody help me out.
Sure. But since this is probably homework anyway, I would ask what have you got so far?
Ha! True. :p
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.
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.
#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
What exactly are you trying to do? Answer with respect to Salem's post #5.