check for overflow

find the sum of two numbers and check for overflow.
both the numbers are within integer limit but their sum may exceed.

Comments

  1. int first,second;
    cin>>first>>second;

    //since int has 2^32-1 values
    int x = 2^32-1;
    int diff = x-first;
    if(diff <second){
    cout<<"sum will give overflow";
    }else{
    cout<<first+second;
    }

    ReplyDelete

Post a Comment

Popular posts from this blog