# How would you check if a number is an integer?

Page 1 of 1

## 5 Replies - 767 Views - Last Post: 17 July 2012 - 02:06 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=286029&amp;s=63bc83fae2adcd76cdfc6743ff927114&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 lukeme99

Reputation: 0
• Posts: 85
• Joined: 15-March 11

# How would you check if a number is an integer?

Posted 16 July 2012 - 11:54 PM

Hi,

I'm trying to make a snippet to check whether or not the number is even, it does this by getting a number dividing by 2 and checking whether it's an interger or not, but for some reason I can't find anything that does this AND actually compiles, because I only have limited acces to compilers from my iPad.

So far:

#include <iostream>
#include <stdlib.h>
#include <ctype.h>
int main ()
{
int integer;
cout << "Please enter number: \n";
cin >> integer;

integer = integer / 2;

cout << "Is your number even? "

if(integer *codecodecode*)
{
cout << "YES";
}
else
{
cout << "NOPE";
}
return 0;
}

Help would be appreciated!
L99

Is This A Good Question/Topic? 0

## Replies To: How would you check if a number is an integer?

### #2 sepp2k

• D.I.C Lover

Reputation: 2270
• Posts: 3,483
• Joined: 21-June 11

## Re: How would you check if a number is an integer?

Posted 17 July 2012 - 12:09 AM

The result of dividing two ints will always be another int - if the ints do not evenly divide the result will simply be rounded. And even if you change it to something that won't result in an integer (like integer / 2.0) assigning the result to integer will just convert it back to int because integer has type int.

What you should rather do is to check whether there's a remainder when you divide integer by 2. You can use the % operator for that. x % y will give you the remainder of dividing x by y.

### #3 lukeme99

Reputation: 0
• Posts: 85
• Joined: 15-March 11

## Re: How would you check if a number is an integer?

Posted 17 July 2012 - 12:27 AM

sepp2k, on 17 July 2012 - 08:09 AM, said:

The result of dividing two ints will always be another int - if the ints do not evenly divide the result will simply be rounded. And even if you change it to something that won't result in an integer (like integer / 2.0) assigning the result to integer will just convert it back to int because integer has type int.

What you should rather do is to check whether there's a remainder when you divide integer by 2. You can use the % operator for that. x % y will give you the remainder of dividing x by y.

Ah! Didn't think of using mods, so check if remainder is zero, if it's anything else then it's non even! Thank you so much...

### #4 Aphex19

• Born again Pastafarian.

Reputation: 616
• Posts: 1,873
• Joined: 02-August 09

## Re: How would you check if a number is an integer?

Posted 17 July 2012 - 01:38 AM

There is a much easier way of checking whether an integer is even. Just check the status of least significant bit. If it's 1, it's odd, else it's even.

### #5 sepp2k

• D.I.C Lover

Reputation: 2270
• Posts: 3,483
• Joined: 21-June 11

## Re: How would you check if a number is an integer?

Posted 17 July 2012 - 02:00 AM

Aphex19, on 17 July 2012 - 10:38 AM, said:

There is a much easier way of checking whether an integer is even. Just check the status of least significant bit. If it's 1, it's odd, else it's even.

How is & 1 "much easier" than % 2?

### #6 Aphex19

• Born again Pastafarian.

Reputation: 616
• Posts: 1,873
• Joined: 02-August 09

## Re: How would you check if a number is an integer?

Posted 17 July 2012 - 02:06 AM

sepp2k, on 17 July 2012 - 10:00 AM, said:

Aphex19, on 17 July 2012 - 10:38 AM, said:

There is a much easier way of checking whether an integer is even. Just check the status of least significant bit. If it's 1, it's odd, else it's even.

How is & 1 "much easier" than % 2?

Sorry, that was thoughtless of me. I was really referring to lukeme99's original code. Still, I think it's atleast another simple and efficient option, although using the modulo operator or bitwise operator, in this case, probably outputs the same code in an optimised compiler.

This post has been edited by Aphex19: 17 July 2012 - 02:14 AM