473,395 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

even or odd

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");
}
--
Mustafa El Sayid
Mar 19 '08 #1
25 2850
melsayid <me******@gmail.comwrote:
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
Mar 19 '08 #2
melsayid wrote:
>
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;
}

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto/
Mar 19 '08 #3
On Mar 19, 4:57 pm, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
melsayid <melsa...@gmail.comwrote:
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
Try entering 2 or any higher even number.

--
Mustafa Zaza
Mar 19 '08 #4
In article <69**********************************@e23g2000prf. googlegroups.com>,
melsayid <me******@gmail.comwrote:
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
--
:wq
Mar 19 '08 #5
In article <47***************@iedu.com>,
Morris Dovey <mr*****@iedu.comwrote:
>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
--
:wq
Mar 19 '08 #6
On Mar 19, 5:19 pm, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
In article <6927a02c-a8e1-44c8-979d-4c00d949b...@e23g2000prf.googlegroups.com>,

melsayid <melsa...@gmail.comwrote:
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
--
:wq
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.

--
Mustafa El Sayid
Mar 19 '08 #7
In article <cc**********************************@e23g2000prf. googlegroups.com>,
melsayid <me******@gmail.comwrote:
for (; d<n; d+=2);
printf ("The number is odd\n");
>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
--
:wq
Mar 19 '08 #8
Richard Tobin wrote:
>
In article <47***************@iedu.com>,
Morris Dovey <mr*****@iedu.comwrote:
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.
You noticed that, eh?

:-D

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto/
Mar 19 '08 #9
On Mar 19, 5:35 pm, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
In article <cc4b6752-b7cb-40de-9025-48b41d390...@e23g2000prf.googlegroups.com>,

melsayid <melsa...@gmail.comwrote:
for (; d<n; d+=2);
printf ("The number is odd\n");
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
--
:wq
Ok, it's working now.
Thanks.

--
Mustafa El Sayid
Mar 19 '08 #10
melsayid wrote:
>
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. :-)
</pedant>
printf ("The number is odd\n");
And then unconditionally print "The number is odd".
}
else
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:Th*************@gmail.com>

Mar 19 '08 #11

"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.

--
Cheers,
Robbie Hatley
lonewolf aatt well dott com
www dott well dott com slant user slant lonewolf slant
Mar 19 '08 #12
melsayid wrote:
>
On Mar 19, 5:35 pm, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
In article <cc4b6752-b7cb-40de-9025-48b41d390...@e23g2000prf.googlegroups.com>,

melsayid <melsa...@gmail.comwrote:
for (; d<n; d+=2);
printf ("The number is odd\n");
>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
--
:wq

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:Th*************@gmail.com>
Mar 20 '08 #13
On Mar 20, 6:19*pm, Kenneth Brody <kenbr...@spamcop.netwrote:
melsayid wrote:
On Mar 19, 5:35 pm, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
In article <cc4b6752-b7cb-40de-9025-48b41d390...@e23g2000prf.googlegroups.com>,
melsayid *<melsa...@gmail.comwrote:
* * * * *for (; d<n; d+=2);
* * * * *printf ("The number is odd\n");
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 whetherd
stopped at n or went one past it.
-- Richard
--
:wq
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:ThisIsASpamT...@gmail.com>- 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
Mar 21 '08 #14
Mahesh wrote:

<code to check host endianness>
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.

Mar 21 '08 #15
santosh wrote:
Mahesh wrote:

<code to check host endianness>
>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.
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.

--
Er*********@sun.com

Mar 21 '08 #16
melsayid wrote:
>
The program always shows that the input is odd.
scanf ("%d", &n);
d=1;
That's because (d=1) is always odd.

--
pete
Mar 28 '08 #17
pete wrote:
>
melsayid wrote:

The program always shows that the input is odd.
scanf ("%d", &n);
d=1;

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 */
--
pete
Mar 28 '08 #18
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:Th*************@gmail.com>
Mar 28 '08 #19
Kenneth Brody wrote:
>
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.

--
pete
Mar 29 '08 #20
John H. Guillory said:

<snip>
Not to
mention, most C compilers have a EVEN() and ODD() macro
They do?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 19 '08 #21

"John H. Guillory" <jo***@communicomm.comwrote in message
news:tn********************************@4ax.com...
On Wed, 19 Mar 2008 07:52:22 -0700 (PDT), melsayid
<me******@gmail.comwrote:
>>The program always shows that the input is odd.

int main ()
{
int n, d;

printf ("Enter a Number: ");
scanf ("%d", &n);
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");
Sep 19 '08 #22
"John H. Guillory" wrote:
melsayid <me******@gmail.comwrote:
>The program always shows that the input is odd.

int main () {
int n, d;

printf ("Enter a Number: ");
scanf ("%d", &n);
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.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Sep 19 '08 #23
CBFalconer said:
"John H. Guillory" wrote:
>melsayid <me******@gmail.comwrote:
>>The program always shows that the input is odd.

int main () {
int n, d;

printf ("Enter a Number: ");
scanf ("%d", &n);
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.
Even on ones' complement systems, it will work with 50% of signed ints.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 19 '08 #24

"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:u_******************************@bt.com...
CBFalconer said:
>"John H. Guillory" wrote:
>>melsayid <me******@gmail.comwrote:

The program always shows that the input is odd.

int main () {
int n, d;

printf ("Enter a Number: ");
scanf ("%d", &n);
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.

Even on ones' complement systems, it will work with 50% of signed ints.
Well,

if (1) printf("The number is odd");

also works with about 50% of signed ints :-)

--
poncho
Sep 19 '08 #25
Scott Fluhrer said:
>
"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:u_******************************@bt.com...
>CBFalconer said:
>>"John H. Guillory" wrote:
melsayid <me******@gmail.comwrote:

The program always shows that the input is odd.
>
int main () {
int n, d;
>
printf ("Enter a Number: ");
scanf ("%d", &n);
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.

Even on ones' complement systems, it will work with 50% of signed ints.

Well,

if (1) printf("The number is odd");

also works with about 50% of signed ints :-)
I think we have a winner!

Of course, my point was that it would have been better to say not that "it
won't work with signed ints on 1's complement systems" but that "it won't
work with *negative* ints on ones' complement systems".

And that's assuming you can /find/ a ones' complement system.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 19 '08 #26

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: bill drescher | last post by:
I have a long table and I want to have the alternating rows slightly different colors for clarity. Is there any way to avoid using a class designation in each tr ? (yes, it is for tabular data...
0
by: Nathan | last post by:
Hi, I seem to having a peculiar problem with the display of odd and even pages in XSL-FO. Here is a small background of the problem. My xsl stylesheet mentions my fo:layout-master-set as ...
34
by: Frederick Gotham | last post by:
I'm trying to write extremely efficient, fully portable algorithms to determine if an integer is odd or even. The most basic algorithms would be: #define IS_ODD(x) ((x) % 2) #define IS_EVEN(x)...
10
by: Doug Robertson | last post by:
First off, I'm a hardware/OS guy. I just write code on the side and I'm completely self taught - so bear in mind my total lack of expertise. I have a program originally written in VB2003 using...
17
by: Ron | last post by:
I want to write a program that will accept a number in a textbox for example 23578 and then in a label will display the sum of the odd and even number like this... the textbox containsthe number...
6
by: Klaas Vantournhout | last post by:
Hi, I have a question, which is just out of interest. What is the fastest way to do an odd/even check with c++ and if needed assembler. Assume n is an unsigned integer like type (unsigned...
1
by: chiefychf | last post by:
I'm working on a school project and I am having a few issues... The program calls for three arrays a,b,c that have to be sorted, then compared to even or odd and stored in arrays d & e, then merge...
30
by: bdsatish | last post by:
The built-in function round( ) will always "round up", that is 1.5 is rounded to 2.0 and 2.5 is rounded to 3.0. If I want to round to the nearest even, that is my_round(1.5) = 2 # As...
18
by: =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?= | last post by:
Hello. It seems a year is all it takes for one's proficiency in C++ to become too rusty. Professionally, I've been away from the language (and from programming in general), but I still preserve...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.