i have just started learning c++
i just want a program to print ood & even from 1-100
please help me

jonsca commented: Start your own thread, don't hijack someone else's -1

Recommended Answers

All 7 Replies

vsawant -

Welcome to DaniWeb! Let me give you a few pointers to ensure your stay here is as successful as possible.

1) Please do not "hijack" threads. That is, your post here was not related to the original question. In this case, you should start a new thread rather than reply to this one.

2) The learning process goes more smoothly if you show us what you have tried so far and we help you get it to work. You will not get many responses just by telling us what you are trying to.

In this case, I would recommend that you look into the modulo operator (%). Note that [even numbers] % 2 = 0 !

Good luck,

Dave

I suggest using a bitwise AND (much quicker as it doesn't involve a division) if(number & 1 == 1) { /*Odd number*/ }else{ /* Even number */}

In anticipation of the thread being split, Ed has a comment.

> I suggest using a bitwise AND (much quicker as it doesn't involve a division)
This is a common misconception. Every time Edward tries to confirm such claims, the result is that the two are competitive at all optimization levels. While bitwise AND *is* statistically faster when averaging large numbers of tests, the actual difference in each test is vanishingly small.

Ed's conclusion is that it doesn't matter and you should pick whichever option is clearer. :)

commented: good sumation +28
commented: Good going Ed +4

>>you should start a new thread rather than reply to this one

>>in anticipation of the thread being split

Done.

FWIW: I agree with Ed. Optimizing on this level often leads to unreadable (/unmaintainable) code. When programming for something as powerful as a normal PC/Notebook, readability is preferred to micro-optimization.

Agree with Nick and Ed. Optimatization isn't really important at all to beginners, they should be more concerned with just learning the language. Optimization will come later in a year or so.

Sorry, I'm just used to programming like that =(

I also do number << 1 and number >> 1 to multiply and divide by 2 ^^

(I come from a Games Programming and Embedded Devices background)

Member Avatar for ZootAllures

Sorry, I'm just used to programming like that =(

I also do number << 1 and number >> 1 to multiply and divide by 2 ^^

(I come from a Games Programming and Embedded Devices background)

I have to agree with Nick, Ed et al; I guess your background explains it.

In general the performance difference is so minute that unless you're running some tiny embedded processor or doing a few billion of them, the clarity and readability of your code is way more important.

BTW, have we hijacked again by wandering off-topic?

vsawant: You'll need a way to tell even from odd (use if (number % 2 == 0) as already mentioned), a "for" loop to get from 1 to 100, and a way to print your results, probably "cout".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.