even or odd

P

pete

pete said:
That's because (d=1) is always odd.

But that's not it. I read it wrong.

It's because this part of the code always prints
"The number is odd\n"
regardless of what happens in the loop.

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

This is my version of the repaired algorithm:

/* BEGIN new.c */

#include <stdio.h>

int main(void)
{
int n, d;

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

if (n > d) {
do {
d += 2;
} while (n > d);
} else {
while (d > n) {
n += 2;
}
}
if (d == n) {
puts("The number is odd.");
} else {
puts("The number is even.");
}
return 0;
}

/* END new.c */
 
K

Kenneth Brody

pete wrote:
[...]
This is my version of the repaired algorithm:

/* BEGIN new.c */

#include <stdio.h>

int main(void)
{
int n, d;

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

if (n > d) {
do {
d += 2;
} while (n > d);
} else {
while (d > n) {
n += 2;
}
}
if (d == n) {
puts("The number is odd.");
} else {
puts("The number is even.");
}
return 0;
}

/* END new.c */

How long will the program take to run if you enter "2000000000"?
What about "2147483647" on a 32-bit int system?

--
+-------------------------+--------------------+-----------------------+
| 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]>
 
P

pete

Kenneth said:
pete wrote:
[...]
This is my version of the repaired algorithm:

/* BEGIN new.c */

#include <stdio.h>

int main(void)
{
int n, d;

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

if (n > d) {
do {
d += 2;
} while (n > d);
} else {
while (d > n) {
n += 2;
}
}
if (d == n) {
puts("The number is odd.");
} else {
puts("The number is even.");
}
return 0;
}

/* END new.c */

How long will the program take to run if you enter "2000000000"?
What about "2147483647" on a 32-bit int system?

You can't speed up a program that doesn't tell you
anything that you don't already know,
to point where the program is fast enough to become useful.
 
G

Gilbert Hamm

John H. Guillory said:
if ((n & 1) == 1) printf("The number is odd");
}

Anytime Bit 1 is set, the number is odd, if its cleared, its even.
Simply test for bit 1 and you know if its even or odd.... Not to
mention, most C compilers have a EVEN() and ODD() macro


Why don't you try
if(N%2 == 0) printf("even");
else printf("odd");
 
C

CBFalconer

John H. Guillory said:
if ((n & 1) == 1) printf("The number is odd");

Anytime Bit 1 is set, the number is odd, if its cleared, its
even. Simply test for bit 1 and you know if its even or odd.
Not to mention, most C compilers have a EVEN() and ODD() macro

You should make n an unsigned int. It won't work with signed ints
on 1's complement systems.
 

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,780
Messages
2,569,608
Members
45,250
Latest member
Charlesreero

Latest Threads

Top