443,889 Members | 1,358 Online 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
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 numbersso is there any predefined function i can use or what can structure ishould 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 getthe 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

 P: n/a ia*************@gmail.com wrote: On Mar 22, 6:22 pm, Larry Smith Larry Smith wrote: >>devoreb...@gmail.com wrote:hey i wrote all my code but i need to print out only the even numbersso is there any predefined function i can use or what can structure ishould 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 getthe 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

 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 numbersso is there any predefined function i can use or what can structure ishould 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 getthe 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. 