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

D

devoreband

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)
 
L

Larry Smith

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

Larry Smith

Larry said:
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?
 
I

ianenis.tiryaki

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!!!!
 
L

Larry Smith

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

Jim Langston

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

rossum

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,436
Messages
2,571,696
Members
48,796
Latest member
Greg L.
Top