473,395 Members | 1,919 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.

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

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 3741
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
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
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
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
<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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: Xah Lee | last post by:
in computer languages, often a function definition looks like this: subroutine f (x1, x2, ...) { variables ... do this or that } in advanced languages such as LISP family, it is not uncommon...
56
by: Xah Lee | last post by:
What are OOP's Jargons and Complexities Xah Lee, 20050128 The Rise of Classes, Methods, Objects In computer languages, often a function definition looks like this: subroutine f (x1, x2, ...)...
137
by: Philippe C. Martin | last post by:
I apologize in advance for launching this post but I might get enlightment somehow (PS: I am _very_ agnostic ;-). - 1) I do not consider my intelligence/education above average - 2) I am very...
2
by: Fred | last post by:
Hi. Sorry, but I don't know where else to ask. I wrote a access shipping database and we currently use the web interface for DHL to print shipping labels. That means we have to manualy transpose...
6
by: Niklaus | last post by:
Hi, Can someone point out what is wrong with this code ? How can i make it better optimize it. When run it gives me seg fault in linux. But windows it works fine(runs for a long time). Do we...
9
by: Karlo Lozovina | last post by:
Here is it: --- class Human: def __init__(self, eye_one, eye_two): self.eye_one = eye_one self.eye_two = eye_two class Population: def __init__(self):
4
by: Ju Hui | last post by:
I want to print 3 numbers without blank. >>> for x in range(3): .... print x .... 0 1 2 >>> for x in range(3): .... print x, ....
18
by: Xah Lee | last post by:
What are OOP's Jargons and Complexities Xah Lee, 20050128 Classes, Methods, Objects In computer languages, often a function definition looks like this: subroutine f (x1, x2, ...) {...
3
by: sparks | last post by:
I was generating some numbers for a new database. Its based on some entries the people do and it worked fine except for deleted records. Private Sub generate() Dim rst As Recordset Dim maxof As...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.