scanf shows number always as pair

-1

I have a problem and I do not know how to solve it because I'm starting to use Dev-C ++ now. I do not know much about it, the problem is the following I was able to solve a bug I was having, which was the use of % . Now I have another problem: any number that I type will appear on the screen that is even. Could someone please let me know in what line I was wrong and the mistake I made?

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main ()
{
    int N;
    printf ("Digite um numero: ");
    scanf("%f", &N);
    if (N<0)
        printf ("Este número não é positivo\n");
    if(N % 2 == 0)
        printf ("Este numero e par\n");
    else
        printf("Este número é impar");
    system ("PAUSE");
}
    
asked by anonymous 03.04.2014 / 19:46

2 answers

6

The variable N is integer type. But in your scanf call you are using "%f" which is float . Change to "%d" (used for decimal integers) that the reading will be done correctly.

Read about scanf and prinf also, it will help.

See running on ideone .

    
03.04.2014 / 19:55
0

I took the liberty to make some modifications, I tried to change the minimum possible of your program, first the "% d" of your scanf, you were using "% f" that serves for float, another thing I also used was the modification of the condition of the second if, which went on to compare if the rest of the division is (! =) nonzero (then going to odd numbers and else for the pairs), I also used the translation library to enable the characters of our language. I think that would solve your problem. I leave a piece of advice, it would be nice to use the keys in if, else if and else, to avoid minor confusions, indentation and organization help a lot for the understanding and execution of the program.

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>

int main (){
    setlocale(LC_ALL, "Portuguese");
    int N;
    printf ("Digite um numero: ");
    scanf("%d", &N);
    if (N<0)
            printf ("Este número não é positivo\n\n");
    if(N%2!=0)
        printf ("Este numero e ímpar\n\n");
    else
        printf("Este número é par\n\n");
    system ("PAUSE");
}
    
08.06.2014 / 05:20