Adding Positives in Range

Asked

Viewed 63 times

-1

I am making a program in C Language, but the site informs that the output of the program is not accepted.

Adding Positives in Range

Follows the code:

#include <stdio.h>
int main(){
    int num1, num2, i, soma=0, temp;
    scanf("%i", &num1);
    scanf("%i", &num2);

    if (num1 > num2) {
        temp = num1;
        num1 = num2;
        num2 = temp;
    }

    for ( i = num1 ; i < num2+1; i++ ) {
        if (i>=0) {
            soma = soma + 1;
        }
    }
    printf("%i", soma);

    return 0;
}

Error print: Two screenshots of the error being displayed

  • What exactly makes you wrong? I tested it in a program and it works.

  • One by one, if you want to add these two numbers together, it’s easier to just do soma= num1 + num2; and so does not need any cycle for

  • Hi, you could see the screenshots, it was shown that the program did not pass in 2 cases and do not know how to solve.

1 answer

0


The question asks that each positive number be summed within the interval, so within the cycle you must do:

for (i = num1 ; i < num2+1; i++) {
    if (i > 0) {
        soma = soma + i;
    }
}

If for example the range is 5 10, your code is doing 1 + 1 + 1 + 1 + 1 + 1, when it should be 5 + 6 + 7 + 8 + 9 + 10.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.