scanf shows number always as pair

Asked

Viewed 167 times

-1

I have a problem and I don’t know how to solve it because I’m starting to use Dev-C++ now. I don’t know much about him, the problem is the following I managed to solve an error I was having, which was the use of %. Now I have another problem: any number I type will appear on the screen that is even. Could someone please let me know on what line I made a mistake 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");
}

2 answers

6


The variable N It’s the whole kind. But on your call from scanf are using "%f" which is for float. Change to "%d" (used for decimal integers) the reading will be done correctly.

Give a read on scanf and prinf also, will help.

See running on ideone.

0

I took the liberty of making some modifications, I tried to change the minimum possible of your program, first the "%d" of your scanf, you were using "%f" that serves to float, another thing I also used was the modification of the condition of the second if, which started comparing whether the rest of the division is (!=) non-zero (then going to odd numbers and Else to pairs), I also used the translation library, to enable the special characters of our language. I think that would solve your problem. I leave a piece of advice, it would be good to use the keys in the if, Else and Else, to avoid small 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");
}

Browser other questions tagged

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