By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,889 Members | 1,358 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,889 IT Pros & Developers. It's quick & easy.

what can you do to print out only even numbers...??

P: n/a
hey i wrote all my code but i need to print out only the even numbers
so is there any predefined function i can use or what can structure i
should be following?

........
16 for(int r = 0; r < 10; r++) {
17 for(int c = 0; c < 10; c++) {
18 cout << setw(5) << setfill(' ') << r*c;
19 } // end for c
20 cout << endl;
21 }// end for r
22
.........

( i thought i could make some change on line 18 so that i could get
the program worked as i would like it to; i am not sure if i am right)

Mar 23 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
de********@gmail.com wrote:
hey i wrote all my code but i need to print out only the even numbers
so is there any predefined function i can use or what can structure i
should be following?

.......
16 for(int r = 0; r < 10; r++) {
17 for(int c = 0; c < 10; c++) {
18 cout << setw(5) << setfill(' ') << r*c;
19 } // end for c
20 cout << endl;
21 }// end for r
22
........

( i thought i could make some change on line 18 so that i could get
the program worked as i would like it to; i am not sure if i am right)
Try this
if ( 0 == (1 & c) && 0 == (1 & r) )
cout << setw(5) << setfill(' ') << r*c;

or this
if ( 0 == (1 & (r * c)) )
cout ...

or this
if ( !(1 & (r * c)) )
cout ...

or this
if ( 0 == ((r * c) % 1) )
cout ...
Mar 23 '07 #2

P: n/a
Larry Smith wrote:
de********@gmail.com wrote:
>hey i wrote all my code but i need to print out only the even numbers
so is there any predefined function i can use or what can structure i
should be following?

.......
16 for(int r = 0; r < 10; r++) {
17 for(int c = 0; c < 10; c++) {
18 cout << setw(5) << setfill(' ') << r*c;
19 } // end for c
20 cout << endl;
21 }// end for r
22
........

( i thought i could make some change on line 18 so that i could get
the program worked as i would like it to; i am not sure if i am right)

Try this
if ( 0 == (1 & c) && 0 == (1 & r) )
cout << setw(5) << setfill(' ') << r*c;

or this
if ( 0 == (1 & (r * c)) )
cout ...

or this
if ( !(1 & (r * c)) )
cout ...

or this
if ( 0 == ((r * c) % 1) )
cout ...
Oops, I forgot to add this:
Which of the above work?
Which don't?
Why?
Mar 23 '07 #3

P: n/a
On Mar 22, 6:22 pm, Larry Smith <lsm...@nospam.comwrote:
Larry Smith wrote:
devoreb...@gmail.com wrote:
hey i wrote all my code but i need to print out only the even numbers
so is there any predefined function i can use or what can structure i
should be following?
.......
16 for(int r = 0; r < 10; r++) {
17 for(int c = 0; c < 10; c++) {
18 cout << setw(5) << setfill(' ') << r*c;
19 } // end for c
20 cout << endl;
21 }// end for r
22
........
( i thought i could make some change on line 18 so that i could get
the program worked as i would like it to; i am not sure if i am right)
Try this
if ( 0 == (1 & c) && 0 == (1 & r) )
cout << setw(5) << setfill(' ') << r*c;
or this
if ( 0 == (1 & (r * c)) )
cout ...
or this
if ( !(1 & (r * c)) )
cout ...
or this
if ( 0 == ((r * c) % 1) )
cout ...

Oops, I forgot to add this:
Which of the above work?
Which don't?
Why?- Hide quoted text -

- Show quoted text -
i believe first one would definetely work!!!!
Mar 23 '07 #4

P: n/a
ia*************@gmail.com wrote:
On Mar 22, 6:22 pm, Larry Smith <lsm...@nospam.comwrote:
>Larry Smith wrote:
>>devoreb...@gmail.com wrote:
hey i wrote all my code but i need to print out only the even numbers
so is there any predefined function i can use or what can structure i
should be following?
.......
16 for(int r = 0; r < 10; r++) {
17 for(int c = 0; c < 10; c++) {
18 cout << setw(5) << setfill(' ') << r*c;
19 } // end for c
20 cout << endl;
21 }// end for r
22
........
( i thought i could make some change on line 18 so that i could get
the program worked as i would like it to; i am not sure if i am right)
Try this
if ( 0 == (1 & c) && 0 == (1 & r) )
cout << setw(5) << setfill(' ') << r*c;
or this
if ( 0 == (1 & (r * c)) )
cout ...
or this
if ( !(1 & (r * c)) )
cout ...
or this
if ( 0 == ((r * c) % 1) )
cout ...
Oops, I forgot to add this:
Which of the above work?
Which don't?
Why?- Hide quoted text -

- Show quoted text -

i believe first one would definetely work!!!!

Hmm, what if 'r' and 'c' are each 3?

Try (edit, compile, & execute) with each of the various
'if' statements to see which ones work.
Mar 23 '07 #5

P: n/a
<de********@gmail.comwrote in message
news:11*********************@y66g2000hsf.googlegro ups.com...
hey i wrote all my code but i need to print out only the even numbers
so is there any predefined function i can use or what can structure i
should be following?

.......
16 for(int r = 0; r < 10; r++) {
17 for(int c = 0; c < 10; c++) {
18 cout << setw(5) << setfill(' ') << r*c;
19 } // end for c
20 cout << endl;
21 }// end for r
22
........

( i thought i could make some change on line 18 so that i could get
the program worked as i would like it to; i am not sure if i am right)
When is a number even? When it is evenly divisible by 2. How can you
determine if a number is evenly divisble by 2? There are a number of ways.

One is the modulus operator. %

dividend % divisor (meh? is it dividend? can't remember)
will return a remainder.

5 % 3
will return 2. Becaue 5 / 3 = 1 with a remainer of 2. What remainder would
mean that the number is evenly devisible by 3?

Another way is to look at the bits.

1 & number
will return 1 if the 1 bit is set. The 1 bit is set if a number is odd.
What number would be returned meaning the number is even?

You will also want an if statement.
if ( somecondition )
cout << something;

It is up to you to figure out what the condition is.
Mar 23 '07 #6

P: n/a
On 22 Mar 2007 17:03:04 -0700, de********@gmail.com wrote:
>hey i wrote all my code but i need to print out only the even numbers
so is there any predefined function i can use or what can structure i
should be following?

.......
16 for(int r = 0; r < 10; r++) {
17 for(int c = 0; c < 10; c++) {
18 cout << setw(5) << setfill(' ') << r*c;
19 } // end for c
20 cout << endl;
21 }// end for r
22
........

( i thought i could make some change on line 18 so that i could get
the program worked as i would like it to; i am not sure if i am right)
Every even number is 2 * m, where m is some other number. It is very
easy to make an even number.

rossum

Mar 23 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.