[tricky method] Find whether a number is odd or even.

Status
Not open for further replies.

mehulved

18 Till I Die............
Err, getting the last digit in C is as easy as indexing it to sizeof()-1 ?
All right.

I don't think that should be difficult. In PHP, there is a function for this. Since PHP syntax is derived from C, I am sure there must be a similar function in C too.
Yes, but I needed info on how to do it. QwertyM has replied to that now
Ok, here is the PHP code to do it. As I said, there is an inbuilt function to get a substring out from a string.
PHP:
<?php
$input="12345";//input number
$last_digit=substr($input,"-1");//get the last digit
if($last_digit=="0" || 
$last_digit=="2" || 
$last_digit=="4" || 
$last_digit=="6" || 
$last_digit=="8")
//compare the last digit to see if its either of 0, 2, 4, 6 or 8, or use in_array() function.
{
print "Input is an even number";
}
else
{
print "Input is an odd number";
}
?>

Wouldn't using case be better instead of if?
 
Last edited:

victor_rambo

हॉर्न ओके प्लीज़
Wouldn't using case be better instead of if?
Ya, you could use switch() too.
PHP:
$input="12345";//input number
$last_digit=substr($input,"-1");//get the last digit
switch($last_digit)
{
case 0; case 2; case 4; case 6; case 8;
print "Number is even";
break;

default;
print "Number is odd";
}

Also you can do it by using array functions:
1. Create an array containing even numbers.
2. check if the last digit is exists in the array.
3. If it exists->Number is even; Else:->Number is odd.
PHP:
$input="12345";//input number
$last_digit=substr($input,"-1");//get the last digit
$array_of_even_numbers=array("0","2","4","6","8");
if(in_array($last_digit,$array_of_even_numbers))
{
print "Number is even";
}
else
{
print "Number is odd";
}
 

aadipa

Padawan
How about this
Code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
    int num;
    printf("Please Enter a Number > ");
    scanf("%d", &num);
    if((num&1)==1)
        printf("%d is odd", num\n);
    else
        printf("%d is even", num\n);
    return 0;
    exit(0);
}
Just a small modification to Mehul's code which is best and most efficient solution to the problem.
In C, any non-zero value is true, only zero is false. So we can skip comparison in if()
Code:
    if(num&1)
        printf("%d is odd", num\n);
    else
        printf("%d is even", num\n);
 

dheeraj_kumar

Legen-wait for it-dary!
How about sprintf function?

char* str;
sprintf(str, "%d", number);
printf("%c", str[strlen(str)-1]);

we can also use itoa instead of sprintf, i guess.
 

QwertyManiac

Commander in Chief
Just a small modification to Mehul's code which is best and most efficient solution to the problem.
In C, any non-zero value is true, only zero is false. So we can skip comparison in if()
Code:
    if(num&1)
        printf("%d is odd", num\n);
    else
        printf("%d is even", num\n);
But doesn't the comparison of jump-if-not-zero happen anyway?
 

aadipa

Padawan
How about sprintf function?
char* str;
sprintf(str, "%d", number);
printf("%c", str[strlen(str)-1]);
we can also use itoa instead of sprintf, i guess.
Wont it just print last digit of number? We need to find whether number is even or odd.

But doesn't the comparison of jump-if-not-zero happen anyway?
Code:
if(num&1)
Code:
if((num&1)==1)
2nd code has 1 more comparision before result is fed to if condition. this will add 1 more instruction, hence will perform slower.
 
Last edited:

dheeraj_kumar

Legen-wait for it-dary!
Oh, I'm sorry, I meant to combine it with rohan's idea. If the last digit is 0,2,4,6,8 then its even. Or else, its odd.
 
Status
Not open for further replies.
Top Bottom