How to know if a number is integer

Asked

Viewed 9,945 times

4

I need to develop an algorithm where, it divides a number by 2, until I get to the rest of the division, and I need to know if the rest of this division is an integer, what method can I apply to develop this problem?

where:

funcao menu_8():inteiro
var
inicio
para contador de 0 ate 12 faca
     se (i[contador] % 2 = 1) entao
        escreva (" Potencia de 2: ", i[contador])
     fimse
fimpara
escreval(" ")
fimfuncao

This algorithm will be applied, in the following, if the user type in the vector, a number that is power of 2, should display this value on screen.

Where will I use, Base Log 10, to divide the number typed by 2 log of 10, and if the result of the division of an integer number, ai means it is base of two, the number typed by the user, otherwise it is not.

1 answer

4


The rest of the division is an integer, by definition. If it is not integer, it is not remainder. Alias the whole description of the problem does not seem to make the least sense. I will answer the question title.

Basically this is it:

x == int(x)

The second expression converts the number to an integer. If it remains the same, it means that it is integer, if it is different it is because the number had a decimal part.

  • This works on Visualg in portugol??

  • @Mauricio probably needs some adaptation.

Browser other questions tagged

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