Notifications
Clear all

detecting odd and even numbers

9 Posts
4 Users
8 Likes
221 Views
barrie
(@barrie)
Estimable Member
Joined: 5 months ago
Posts: 86
Topic starter  

Apart from using if statements, is there a statement  like "modulo" that will detect odd and even numbers? Couldn't see anything in the Reference department.


   
Quote
ron bentley
(@ronbentley1)
Honorable Member
Joined: 12 months ago
Posts: 386
 
Posted by: @barrie

Apart from using if statements, is there a statement  like "modulo" that will detect odd and even numbers? Couldn't see anything in the Reference department.

Hi Barrie,

There are a couple of obvious methods:

1. You can use the % operator with modulo 2 parameter, eg

Value % 2, = 0 if Value is even, and = 1 if Value is odd

2. Boolean mask, eg

Value & 1, = 1 if Value is odd and = 0 if Value is even

 

Ron B

Ron Bentley
Creativity is an input to innovation and change is the output from innovation. Braden Kelley
A computer is a machine for constructing mappings from input to output. Michael Kirby
Through great input you get great output. RZA
Gauss is great but Euler rocks!!


   
frogandtoad reacted
ReplyQuote
Inq
 Inq
(@inq)
Noble Member
Joined: 10 months ago
Posts: 982
 
// IF STATEMENT
if (a % 2)
{
    // Odd
}
else
{
    // Even
}


// ASSIGNMENT

const char* english = a % 2 ? "Odd" : "Even";

 

3 lines of code = InqPortal = Complete IoT, App, Web Server w/ GUI Admin Client, Access Point Manager, Drag & Drop File Manager, OTA, Performance Metrics, Web Socket Comms, Easy App API, All running on ESP8266...
Even usable on ESP-01S - Quickest Start Guide


   
frogandtoad and Ron reacted
ReplyQuote
barrie
(@barrie)
Estimable Member
Joined: 5 months ago
Posts: 86
Topic starter  

@ronbentley1 

Thank you, exactly what I was looking for.

@inq Is this equivalent to your sketch? 

if (n % 2 = 0) { f = 1; b = -1;} else { f = -1; b = 1;}
Note: n, f and b assigned as int.

I got an error "lvalue required as left operand of assignment"


   
ReplyQuote
barrie
(@barrie)
Estimable Member
Joined: 5 months ago
Posts: 86
Topic starter  

@inq Just found my error. should have made (n % 2 == 0) 

 

This post was modified 4 months ago by barrie

   
ron bentley reacted
ReplyQuote
Inq
 Inq
(@inq)
Noble Member
Joined: 10 months ago
Posts: 982
 

Yes, Your second version is right.  Strictly speaking logical statements do not have to have the (in)equality.  They only have to resolve to zero or non-zero to be false or true respectively.

Thus:

if (n % 2)   is the same as    if (n % 2 == 1)

 

Some people prefer to reverse them...

if (1 == n % 2)

... so they catch the exact thing you did above.  

  1. if (n == 1) will compile
  2. if (n = 1) will also compile, but do something you likely didn't intend.
  3. if (1 == n) will compile and is the same as (1)
  4. if (1 = n) will not compile, so it lets you know you screwed up.

 

If you have explored pointers, they are either NULL or pointing to some place in memory, so just checking a pointer before using it like...

MyClass* pClass = new MyClass();

if (pClass)
{
    // Can work with the class object.
}
else
   Serial.println("Could not allocate space for object.");

3 lines of code = InqPortal = Complete IoT, App, Web Server w/ GUI Admin Client, Access Point Manager, Drag & Drop File Manager, OTA, Performance Metrics, Web Socket Comms, Easy App API, All running on ESP8266...
Even usable on ESP-01S - Quickest Start Guide


   
ReplyQuote
frogandtoad
(@frogandtoad)
Noble Member
Joined: 4 years ago
Posts: 1506
 

@inq

Posted by: @inq

Yes, Your second version is right.  Strictly speaking logical statements do not have to have the (in)equality.  They only have to resolve to zero or non-zero to be false or true respectively.

Thus:

if (n % 2)   is the same as    if (n % 2 == 1)

 

Some people prefer to reverse them...

if (1 == n % 2)

... so they catch the exact thing you did above.  

  1. if (n == 1) will compile
  2. if (n = 1) will also compile, but do something you likely didn't intend.
  3. if (1 == n) will compile and is the same as (1)
  4. if (1 = n) will not compile, so it lets you know you screwed up.

 

If you have explored pointers, they are either NULL or pointing to some place in memory, so just checking a pointer before using it like...

MyClass* pClass = new MyClass();

if (pClass)
{
    // Can work with the class object.
}
else
   Serial.println("Could not allocate space for object.");

Well explained, as I've also mention this in the past.

There's just one more thing I would like to add/offer... and that is that anytime you come across such a situation, you should look to improve it, if it is something that you feel you might use in future, and by that I mean... you should (or may like), to turn it into a function for your own personal library, for future ease of use!

For example:

bool isEven(int number) {
  if(number % 2) {
    return false; 
   }

  return true;
 }

void setup() {
  Serial.begin(9600);
 }

void loop() 
 {
  if(isEven(43)) {
    Serial.println("Number is EVEN");
   } else {
    Serial.println("Number is ODD"); 
   }

  delay(1000);
 }

We could implement a more advanced/efficient function structure, but that would most likely take away from the readability of the learning what's going on here, so just keeping it simple.

Any questions, please ask.

Cheers


   
ReplyQuote
Inq
 Inq
(@inq)
Noble Member
Joined: 10 months ago
Posts: 982
 
Posted by: @frogandtoad

Well explained, as I've also mention this in the past.

You know... that is an interesting problem waiting to be solved by AI.  

Forums are a treasure house of information.  If we could tap that information more transparently, it would make learning quicker.  Yes, I know the search engine is there, but how often are questions re-asked?  Is it timely?  It's not really laziness to want to ask your own question.  Sometimes there is an added nuance.  Sometimes they don't know how to ask the question or what is the keyword that will bring up the information that will click for them.  Sometimes, it's merely to make contact with another human being... a peer.  I have no peers within a hundred miles.  I know... I volunteer and teach people about all kinds of technical subjects... so I turn to forums to mainly learn, but also to reach someone with similar interests.  In the case of robotics, MPUs... its forum.dronebotworkshop.com.

I've seen some forums that attempt to real-time guess what you're asking while writing the body of you post and suggest other posts... however, I've never seen one give me a useful answer.

3 lines of code = InqPortal = Complete IoT, App, Web Server w/ GUI Admin Client, Access Point Manager, Drag & Drop File Manager, OTA, Performance Metrics, Web Socket Comms, Easy App API, All running on ESP8266...
Even usable on ESP-01S - Quickest Start Guide


   
frogandtoad reacted
ReplyQuote
frogandtoad
(@frogandtoad)
Noble Member
Joined: 4 years ago
Posts: 1506
 
Posted by: @inq
Posted by: @frogandtoad

Well explained, as I've also mention this in the past.

You know... that is an interesting problem waiting to be solved by AI.  

Forums are a treasure house of information.  If we could tap that information more transparently, it would make learning quicker.  Yes, I know the search engine is there, but how often are questions re-asked?  Is it timely?  It's not really laziness to want to ask your own question.  Sometimes there is an added nuance.  Sometimes they don't know how to ask the question or what is the keyword that will bring up the information that will click for them.  Sometimes, it's merely to make contact with another human being... a peer.  I have no peers within a hundred miles.  I know... I volunteer and teach people about all kinds of technical subjects... so I turn to forums to mainly learn, but also to reach someone with similar interests.  In the case of robotics, MPUs... its forum.dronebotworkshop.com.

I've seen some forums that attempt to real-time guess what you're asking while writing the body of you post and suggest other posts... however, I've never seen one give me a useful answer.

Good point, perhaps a programming "best practices" tips and tricks FAQ?

Cheers


   
ReplyQuote