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;
}
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;
}
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).
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++;
}
}