Problems in displaying the result of a C function

Asked

Viewed 29 times

-1

Basically I’m making a function in C that takes two positive integers M and N and returns the sum of even numbers between these numbers.

When I’m about to run, you show up to type in both numbers. Then when I type nothing else appears, it’s like a blank and nothing happens by pressing enter or something like that. Could you tell me what’s wrong with my code?

#include <stdio.h>

void calculo(int m, int n){
    int aux = 0, soma = 0, i;
    if(n>m){
        aux = m;
        m = n;
        n = aux;
    }
    for(i = n;n < m;i++){
        if(i % 2 == 0){
            soma = soma+i;
        }
    }
    printf("O resulado da soma eh igual a: %d", soma);
}

main(){
    int x, y;
    printf("Digite dois numeros:");
    scanf("%d%d", &x, &y);
    calculo(x,y);
    return 0;
}
  • I believe that this for(i = n;n < m;i++){ is under the wrong termination condition. How do you reversen and m case (n>m) this for only case shall be closed n == m, in other cases will loop .

1 answer

0

Look at the condition in the for... you start with

  i = n;

And compares:

  n < m;

But the variable you’re increasing is i...

Or do you compare i with m... or increases the n.

Browser other questions tagged

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