even or odd

M

melsayid

The program always shows that the input is odd.

int main ()
{
int n, d;

printf ("Enter a Number: ");
scanf ("%d", &n);
d=1;
if (d==n)
{
printf ("The number is odd\n");
}
else if (d < n)
{
for (; d<n; d+=2);
printf ("The number is odd\n");
}
else
{
printf ("The number is even\n");
}
 
R

Richard Bos

melsayid said:
The program always shows that the input is odd.

No, it doesn't. Try entering 0.

You might want to look up the modulo operator, BTW.

Richard
 
M

Morris Dovey

melsayid said:
The program always shows that the input is odd.

int main ()
{
int n, d;

printf ("Enter a Number: ");
scanf ("%d", &n);
d=1;
if (d==n)
{
printf ("The number is odd\n");
}
else if (d < n)
{
for (; d<n; d+=2);
printf ("The number is odd\n");
}
else
{
printf ("The number is even\n");
}

This should work for twos complement machines:

#include <stdio.h>
int main(void)
{ int n;
printf ("Enter a Number: ");
scanf ("%d", &n);
printf("n is %s\n",(n & 1)?"odd":"even");
return 0;
}
 
R

Richard Tobin

melsayid said:
else if (d < n)
{
for (; d<n; d+=2);
printf ("The number is odd\n");
}

What is the point of the loop here? You don't test anything after
it; you just print "The number is odd".

-- Richard
 
K

Keith Thompson

melsayid said:
The program always shows that the input is odd.

int main ()
{
int n, d;

printf ("Enter a Number: ");
scanf ("%d", &n);
d=1;
if (d==n)
{
printf ("The number is odd\n");
}
else if (d < n)
{
for (; d<n; d+=2);
printf ("The number is odd\n");
}
else
{
printf ("The number is even\n");
}

You need a "#include <stdio.h>" at the top of your program. (No,
that's not the problem, but it's *a* problem.) Oh, and you're missing
the closing "}" (and, preferably, a "return 0;" before that.)

I assume you know that there are easier ways to determine whether a
number is odd or even, and you're more interested in knowing why this
program behaves as it does.

Try stepping through it manually. Assume that n is 4, and pretend to
be the computer. The first if condition is false, so you fall through
to the else clause. The second condition is true, so you execute the
for loop. And so on. What *exactly* happens as the program executes,
and what needs to happen for the program to work the way you want it
to?

A source level debugger, if you have one, might also be a good way to
step through the program, but I think in this case you'll get a better
understanding if you do it yourself.
 
M

melsayid

What is the point of the loop here? You don't test anything after
it; you just print "The number is odd".

-- Richard

I've removed the semicolon and it still doesn't work probably.
Here is how I thought about it,
*The input is n
1-Equals the d with 1
2-Check if the n is equal to d, if yes it prints that the number is
odd
3-If not it checks if the d is less with n, if not it keeps on adding
+2 to the d till it's equal to the value of the n then output that
it's odd
4-Anything else should output that the input is even

@Morris
It works for me, but I want to figure out what's wrong with my code.
Also I don't understand your code, I just started learning C.
 
K

Kenneth Brody

melsayid said:
Try entering 2 or any higher even number.

He was merely giving a counterexample for your assertion that the
program "always" shows odd.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
R

Richard Tobin

This should work for twos complement machines:

#include <stdio.h>
int main(void)
{ int n;
printf ("Enter a Number: ");
scanf ("%d", &n);
printf("n is %s\n",(n & 1)?"odd":"even");
return 0;
}

It will work for non-negative ints regardless of whether the machine
uses twos complement.

-- Richard
 
K

Kenneth Brody

melsayid said:
The program always shows that the input is odd.

Not "always", just "for any positive number". (See also Richard Bos'
reply.)
int main ()
{
int n, d;

printf ("Enter a Number: ");
scanf ("%d", &n);

You should probably check for failure here, but you can get away
without it for simple test cases where you are entering the data.
d=1;
if (d==n)
{
printf ("The number is odd\n");
}
else if (d < n)

This statement will be true for any "n" greater than 1.
{
for (; d<n; d+=2);

Here, you simply increment d by 2 until it is at least equal to n.
Consider, however, how long your program will run if you enter a
number such as 2000000000. Also consider that this may never
terminate if you enter 2147483647.

<pedant>
Note my use of the word "may" in the preceding sentence. :)
printf ("The number is odd\n");

And then unconditionally print "The number is odd".

Only zero or negative numbers will ever get here.
{
printf ("The number is even\n");

And then unconditionally print "The number is even".

Look up the modulo operator -- % -- as it will suit your needs.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
M

melsayid

Suppose n is 8. You add 2 to d until it's greater than or equal to n.
In this case, that will happen when d is 9. But you still print out
that n is odd! You don't do anything different depending on whether d
stopped at n or went one past it.

-- Richard

Ok, it's working now.
Thanks.
 
R

Richard Tobin

[/QUOTE]
3-If not it checks if the d is less with n, if not it keeps on adding
+2 to the d till it's equal to the value of the n then output that
it's odd

Suppose n is 8. You add 2 to d until it's greater than or equal to n.
In this case, that will happen when d is 9. But you still print out
that n is odd! You don't do anything different depending on whether d
stopped at n or went one past it.

-- Richard
 
M

Martin Ambuhl

melsayid said:
The program always shows that the input is odd.

int main ()
{
int n, d;

printf ("Enter a Number: ");
scanf ("%d", &n);
d=1;
if (d==n)
{
printf ("The number is odd\n");
}
else if (d < n)
{
for (; d<n; d+=2);
printf ("The number is odd\n");
}
else
{
printf ("The number is even\n");
}

Your loop
else if (d < n)
{
for (; d<n; d+=2);
printf ("The number is odd\n");
}
increments d but never tests it. So if (d < n) it is incremented (by 2)
until it is either n or n+1 and then prints that it is odd,

If you must approach this problem this way instead of using the modulo
operator (%), consider reordering you code to be something like this:

#include <stdio.h> /* needed for printf */

int main(void)
{
int n, d;

printf ("Enter an integer: ");
/* note that a mention of range (INT_MIN to INT_MAX, or
0 to INT_MAX) might be nice */
fflush(stdout); /* need to synchronized input and
output */
scanf ("%d", &n); /* this is not best, but we'll let
it pass for now. */

/* I suggest a test for sign of n, something like */
if (n < 0) n = -n;

/* This line will assign either n or n+1 to d */
for (d=1; d < n; d += 2) { /* nothing */ }

/* now you can do the test */
if (d==n)
printf ("The number is odd\n");
else
printf ("The number is even\n");
return 0;
}

Not that the final if/then can be written
printf("The number is %s\n", (d == n) ? "odd" : "even");
 
R

Robbie Hatley

"melsayid" sed:
The program always shows that the input is odd.

No it doesn't.

It prints
"even" for all numbers < 1
"odd" for all numbers >= 1

Which, of course, makes no sense whatsoever.
int main ()
{
int n, d;

printf ("Enter a Number: ");
scanf ("%d", &n);
d=1;
if (d==n)
{
printf ("The number is odd\n");
}
else if (d < n)
{
for (; d<n; d+=2);
printf ("The number is odd\n");
}
else
{
printf ("The number is even\n");
}

Write it in English first. Here's what you just wrote, translated into
plain English:

if d is 1 or greater, print "odd"
else print "even"

Does it make any sense in English? No. So it won't make any sense in C,
either!

Look-up "modulo" in a math book. Then look-up "modulo" in a C book.
(Hint: its the "%" operator.)

Then ask yourself, "what do the words 'even' and 'odd' mean?".
(Hint: it has to do with whether a number is divisible by 2.)

Then ask yourself, "what is the relationship betwen 'divisibility' and
'modulo'?"

I could easily re-write your program for you to correctly print
"odd" or "even", but I won't be a prick and spoil the fun you
could have by forcing yourself to learn these things for yourself.
 
K

Kenneth Brody

melsayid said:
Ok, it's working now.
Thanks.

Could you post your updated "working" code?

Have you considered the ramifications of your logic should the
person enter 2000000000 (or 2147483647) as the number?

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
M

Mahesh

melsayid said:
Could you post your updated "working" code?

Have you considered the ramifications of your logic should the
person enter 2000000000 (or 2147483647) as the number?

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody        |www.hvcomputer.com| #include              |
| kenbrody/at\spamcop.net |www.fptech.com    |    <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>- Hide quoted text -

- Show quoted text -

Lets keep it simple

check for the least significant bit of the number, if its 0 then its
even else its odd.
something like

if(num & 0x1)
printf(" ODD \n");
else
printf(" EVEN \n");

happy coding
 
S

santosh

Mahesh wrote:

Lets keep it simple

check for the least significant bit of the number, if its 0 then its
even else its odd.
something like

if(num & 0x1)
printf(" ODD \n");
else
printf(" EVEN \n");

happy coding

But this won't detect mixed-endian machines.
 
E

Eric Sosman

santosh said:
Mahesh wrote:



But this won't detect mixed-endian machines.

Endianness doesn't affect the outcome.

Where it fails is with ones'-complement negative values:
it would diagnose -1 as even and -2 as odd. It might be
possible to generate two numbers X and Y such that X is
reported as even and Y as odd, but X==Y is true.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top