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");