TOPIC

5% wrong answer

Ahmed asked 11 months ago

include

using namespace std ; int main () { int a , b , temp , sum = 0 ; while(a>0 || b>0) { cin >> a >> b ; if(a<=0 || b<=0){break ;} if(a>b){ temp=a ; a=b ; b=temp; } for(int j = a ; a<=b ; a++) { cout << a << " " ; sum+=a; } cout << "Sum=" << sum ; sum = 0 ; } }

Remember not post solutions. Your post may be reviewed by our moderators.

  • feodorv replied 11 months ago

    int a , b , temp , sum = 0 ; while(a>0 || b>0) {

    You do not initialize the variables a and b, first while has undefined behaviour. And no need in the condition in while because you check the values in the cycle body:

    int a , b , temp , sum = 0 ; while(1){

    Also no endl:

    cout << "Sum=" << sum << endl;