Dev c ++ does not give the correct result

0

Afternoons or Nights I have a problem in doing this problem in Dev c ++ that when I run the program it throws an illogical result and I do not know why this error is due I would appreciate a response from you. Thanks

    
asked by Jose A. 13.10.2018 в 21:28
source

1 answer

2

The error is on line number 19, you are not passing what variables you want to display with %i .

That is, if you have to show how much n is worth, what you do is: printf("%i", n) , always remember to pass the variable, because otherwise it will show anything.

This would be your fixed code:

#include <stdio.h>

int main(){
    int i,n,in;
    printf(" Ingrese n: ");
    scanf("%i", &n);
    i=0;
    in=1;
    while(i<n){ // no es menor igual a n, porque sino mostrara n+1 numeros pares
        if(in%2==0){
            printf(" %i.\n",in); // paso in porque in es par
            i++;
        }
        in++;
    }
    system("pause");
    return 0;
}

Execution with n = 2, will give you:

  

Enter n: 2

     

2.

     

4.

Clarification:

I did not fix the in=1; because I assumed you were working with natural even numbers, not positive integers or equal to 0.

    
answered by 13.10.2018 / 21:50
source