Checking Odd/Even Numbers and Changing Outputs on Number Size

Check if a number is odd or even in Python

if num % 2 == 0:
pass # Even
else:
pass # Odd

The % sign is like division only it checks for the remainder, so if the number divided by 2 has a remainder of 0 it's even otherwise odd.

Or reverse them for a little speed improvement, since any number above 0 is also considered "True" you can skip needing to do any equality check:

if num % 2:
pass # Odd
else:
pass # Even

How to change even numbers to odd in a String for java?

public class MyClass {
public static void main(String args[]) {
String social = "472-19-1267";
String result ="";
System.out.println("[INPUT] : " + social) ;
for(int i = 0; i< social.length(); i++){ //this is to loop social string
char c = social.charAt(i);
if (c!='-') //check if the character is not -
{
if ( c % 2 == 0 ) //check if the character is even
{
int cresult=Character.getNumericValue(c); //check cast the character to integer for doing add or subtract
cresult = cresult+1; //add the result with +1
result += Integer.toString(cresult).charAt(0); //cast the int back to character

}
else
{
//if character is odd
int cresult=Character.getNumericValue(c);
cresult = cresult+1;
if (cresult >= 10) //check if character is 9 +1 = 10 so i will change it to subtract to to make it -1 (10-2=8)
{
cresult = cresult-2;
}
result += Integer.toString(cresult).charAt(0); //this line will append the result with cresult
}
}
else
{
result += c; //this line will append the result with -
}
}
System.out.println("[RESULT] : " + result) ;
}

}

the result as below

[INPUT] : 472-19-1267
[RESULT] : 583-28-2378

How do I check if an integer is even or odd?

Use the modulo (%) operator to check if there's a remainder when dividing by 2:

if (x % 2) { /* x is odd */ }

A few people have criticized my answer above stating that using x & 1 is "faster" or "more efficient". I do not believe this to be the case.

Out of curiosity, I created two trivial test case programs:

/* modulo.c */
#include <stdio.h>

int main(void)
{
int x;
for (x = 0; x < 10; x++)
if (x % 2)
printf("%d is odd\n", x);
return 0;
}

/* and.c */
#include <stdio.h>

int main(void)
{
int x;
for (x = 0; x < 10; x++)
if (x & 1)
printf("%d is odd\n", x);
return 0;
}

I then compiled these with gcc 4.1.3 on one of my machines 5 different times:

  • With no optimization flags.
  • With -O
  • With -Os
  • With -O2
  • With -O3

I examined the assembly output of each compile (using gcc -S) and found that in each case, the output for and.c and modulo.c were identical (they both used the andl $1, %eax instruction). I doubt this is a "new" feature, and I suspect it dates back to ancient versions. I also doubt any modern (made in the past 20 years) non-arcane compiler, commercial or open source, lacks such optimization. I would test on other compilers, but I don't have any available at the moment.

If anyone else would care to test other compilers and/or platform targets, and gets a different result, I'd be very interested to know.

Finally, the modulo version is guaranteed by the standard to work whether the integer is positive, negative or zero, regardless of the implementation's representation of signed integers. The bitwise-and version is not. Yes, I realise two's complement is somewhat ubiquitous, so this is not really an issue.

How would I check if three numbers are even or odd?

I think your professor meant something like this:

if (a % 2, b % 2, c % 2) == (0, 0, 0):

Python Beginning: Can't figure out how to tell user their number is odd or even (for 5 numbers)

You are trying to use num for two different purposes:

  • the cumulative sum
  • the number that has just been entered

and as a result you end up testing the odd/evenness of the cumulative sum rather than the number that has just been entered.

Separate them out into two different variables num and total and then it will become easier.

I suggest also using a for instead of while loop for x:

total = 0   
for x in range(5):
num = int(input("Enter a number: "))
mod = num % 2
if mod > 0:
print(num, "is an odd number.")
else:
print(num, "is an even number.")
total += num

print("Your total is", total)


Related Topics



Leave a reply



Submit