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.

1
2
3
4
5
6
7
8
9
10
11
12
  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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void PrintSeries_2()
{
	int count=1;
	while (count<=100)
	{
		while( count%3==0)
		{
			cout<<count;
			break;
		}
		count++;
	}

	return;
}
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<<counter;
	
	}
	return;
}

//Function for printing numbers divisible by two
void PrintSeries_2()
{
	int count=1;
	while (count<=100)
	{
		while( count%3==0)
		{
			cout<<count;
			break;
		}
		count++;
	}

	return;
}
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?
1
2
3
4
5
6
//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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void print_divisible(int divisiblenumber)
{
	int remainder;
	int counter=1;
	while (counter<=100)
	{
		remainder=counter%divisiblenumber;
			if (remainder==0)
			{
				cout<<divisiblenumber;
			}

		counter++;
	
	}
	
}
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
1
2
3
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
Or just look at your other thread :P
http://www.cplusplus.com/forum/beginner/115812/
Ok so without if statments you must have a nested loop, so here is the code :-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

void DivisibleBy_2()
{
	int count = 1;
	while ( count<=1000 )
	{
		while ( count%2==0 )
		{
			cout<<count<<'\t';
			break;
		}
		count++;
	}
}

int main()
{
	 DivisibleBy_2();
	 return 0;
}


Good Luck, Zaki
Or simply increment by 2.
Topic archived. No new replies allowed.