How to know if a decimal number is even or odd?

Asked

Viewed 12,706 times

1

There are several questions related to this question, but always with integers.

For example:

<?php

function evenOdd($number){
    $conta = $number / 2;
    $resto = $number % 2;

    if($resto == 0.0)
        echo $conta.' - Par';
    else
        echo $conta.' - Ímpar';
}

evenOdd(11.8); 
echo ' | ';
evenOdd(10.8);
echo ' | ';
evenOdd(5.075);
echo ' | ';
evenOdd(4.39);

What is the essence of whether it is even or odd ?

  • I do not know if I understood your doubt well, but to identify if it is even or odd we always divide the value by two and check the rest of this division. Example: 5/2 = 2 and 1 remains, so it is considered odd. 8/2 = 4 and 0 remains so it is even

  • @Wagnersoares I think you really don’t understand. He meant for decimal numbers (fractionated). From what I’m reading on Wikipedia the concept of "even" or "odd" only applies to integer numerals. Now I don’t know if it’s true.

  • True, decimals are not classified as even or odd. An even number is an integer in the form n = 2 k, where k is an integer. Even numbers are therefore ..., -4, -2, 0, 2, 4, 6, 8, 10, ... An odd number is an integer in the form n = 2 k + 1, where k is an integer. Even numbers are therefore ..., -3, -1, 1, 3, 5, 7, ... 0,5 is not integer. Therefore, it is neither even nor odd.

  • I closed it because at the end to check it’s the same as what has already been answered. I pointed out one that doesn’t say anything about integers, so any response and consideration about integers, decimals, float would fit there.

  • Why was this question denied ? It is even closed.

2 answers

3

This is more a question of mathematics than of PHP itself. Only numbers whole can be classified in odd or even.

To answer your question.

What is the essence of whether it is even or odd?

Just check the module (rest) of the division by 2, if the rest is 0 the number is even, if it is 1 the number is odd. Therefore, all numbers ending in 1, 3, 5, 7 and 9 are odd, those ending in 0, 2, 4, 6 and 8 are even.

1


As already explained by @jbueno, only integers can be classified as odd or even.

For a number to be Even, it is necessary:

  • Be a multiple integer number of two, that is, it can be written in 2x form;
  • Be divisible by 2;
  • Be surrounded by odd numbers;
  • can be divided into two groups with an equal integer number of elements;
  • Be compatible with all sums/subtractions rules and even and odd number products.

If he doesn’t obey the above rules, then he is Odd.

In practice, to know if a number is PAR, just check that the rest of the division by 2 is equal to 0 (zero)

function ePar($numero){
    if($numero % 2 == 0){ // se o resto da divisão do número por 2 = 0
       return true; // é par
    }else{
       return false; // não é par
    }
}

Use:

var_dump(ePar(43));
var_dump(ePar(44));
var_dump(ePar(46.7));

Exit:

bool(false)

bool(true)

number is not integer

Browser other questions tagged

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