Find even and odd in C language variable float

Question:

There is some error in my program when I try to find the odd and even of a float variable.

#include<stdio.h>
#include<string.h>
void exe7(float vet[]){
    int i;

    for(i=0;i<5;i++){
        scanf("%f",&vet[i]);
    }

    for(i=0;i<5;i++){
        if(vet[i] %2 ==0){
            vet[i+5]=vet[i]+0.02;
        }
        if(vet[i] %2 !=0){
            vet[i+5]=vet[i]+0.05;//vet[i+5] pois será armazenado em outra posição;
        }
    }
    for(i=0;i<10;i++){
        printf("%f",vet[i]);
    }
}
main(){
    float vet[10];

    exe7(vet);
}

Answer:

It does not give.

Parity (the property of being even or odd) is exclusive to integers . Float type variables represent numbers by approximation – they will never represent an integer, as Francisco said.

If you want to delve into this, the Math Stack has explanations on why parity doesn't apply to non-integer numbers . The answer marked as correct is very complex for someone who has not gone to college in mathematics, but there are simpler answers that are equally satisfying.

Ideally, work with integers, not floating point numbers. But if you go that way, better than capturing a number as a floating point and converting it to an integer is to capture it as an integer ( %d instead of %f in your scanf ).

Scroll to Top