### Printing out numbers divisible by two

Function for printing numbers divisible by two between 1 and 1000. I got to this part, but I can't use any conditional, if else statements. What else can I use? any hint? and no switch structures eiter.

 ``123456789101112`` `````` void PrintSeries_2() { int divisiblebytwo; int count=2; while (count<=100) { divisiblebytwo=count%2; } return; }``````
You can use the conditional operator.
 `` `` ``cout << (count % 2 == 0 ? count : "") << endl;``

See http://www.cplusplus.com/forum/articles/14631/
Thank you :) I didn't clarify enough, but I can't use that either; however I do have it figured out.
 ``123456789101112131415`` ``````void PrintSeries_2() { int count=1; while (count<=100) { while( count%3==0) { cout<
Last edited on
Just for a better knowledge. How can I combine divisibility by one and two in one function? These are the functions I have but suppose I want to make them in one function. :(

 ```void PrintSeries_1() { //I have another function which will print out total numbers from 1-1000 int counter; for (counter=1; counter<=1000; counter++) { cout<
Last edited on
Why would you want to combine those functions?
If you are to print all numbers divisible by both 1 and 2, you'll just print all the numbers 1-1000 (like PrintSeries_1).
I mean like can I set parameters in a way that it will calculate these both in one function?
closed account (D80DSL3A)
Are you allowed to use a for loop?
 ``123456`` ``````//Function for printing numbers divisible by n void PrintSeries_n(int n) { for(int i=n; i<=100; i+=n) cout << i << " "; }``````
Yes, I'm allowed to use a loop :). As for your code, that actually did help. Thanks! Will keep this in my reference. I did work on my code right now and came up wit hthis solution. You mind helping me improve this without using the if statement.
 ``123456789101112131415161718`` ``````void print_divisible(int divisiblenumber) { int remainder; int counter=1; while (counter<=100) { remainder=counter%divisiblenumber; if (remainder==0) { cout<
Last edited on
closed account (D80DSL3A)
You seem to be stuck in thinking that you can only increment count by 1. Increment it by divisiblenumber instead.
ie. instead of `counter++;`
do
 ``123`` ``````int counter = divisiblenumber; // in while loop counter += divisiblenumber;``````

or
`counter = counter + divisiblenumber;`
Then you can skip the division test and output every value of counter.
Last edited on
Ok so without `if statments` you must have a nested loop, so here is the code :-
 ``12345678910111213141516171819202122`` ``````#include using namespace std; void DivisibleBy_2() { int count = 1; while ( count<=1000 ) { while ( count%2==0 ) { cout<
 `Good Luck, Zaki`