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 */