5 Replies - 652 Views - Last Post: 17 July 2012 - 02:06 AM Rate Topic: -----

#1 lukeme99  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,311
  • 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.
Was This Post Helpful? 1
  • +
  • -

#3 lukeme99  Icon User is offline

  • D.I.C Head

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

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

Posted 17 July 2012 - 12:27 AM

View Postsepp2k, 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...
Was This Post Helpful? 0
  • +
  • -

#4 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 615
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,311
  • Joined: 21-June 11

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

Posted 17 July 2012 - 02:00 AM

View PostAphex19, 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?
Was This Post Helpful? 0
  • +
  • -

#6 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 615
  • View blog
  • 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

View Postsepp2k, on 17 July 2012 - 10:00 AM, said:

View PostAphex19, 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1