How to make my lists of functions smaller

Not asking for the answer! Rather then having to set up 12 void functions, is it possible to reduce my functions or use the minimum amound of functions tot attain these desired options. Once again, not looking for the answer! If there is how can I do it?

Option Description Output
1 Print all the numbers between 1 and 1000 that are divisible by 1 1 2 3 4 5 … 1000
2 Print all the numbers between 1 and 1000 that are divisible by 2 2 4 6 … 1000
3 Print all the numbers between 1 and 1000 that are divisible by 3 3 6 9 … 999
4 Print all the numbers between 1 and 1000 that are divisible by 4 4 8 12 … 1000
5 Print all the numbers between 1 and 1000 that are divisible by 5 5 10 15 … 1000
6 Print all the numbers between 1 and 1000 that are divisible by 6 6 12 ... 996
7 Print all the numbers between 1 and 1000 that are divisible by 7 7 14 ... 994
8 Print all the numbers between 1 and 1000 that are divisible by 8 8 16 ... 1000
9 Print all the numbers between 1 and 1000 that are divisible by 9 9 18 ... 999
10 Print all the numbers between 1 and 1000 1 2 3 4 5 … 1000
11 Print all the even numbers between 1 and 1000 2 4 6 … 1000
12 Print all the odd numbers between 1 and 1000 1 3 5 … 999
So 1 - 10 are just to print out multiples of 1 - 10, you can do that with a nested loop in one function.

Task 10 is the same as no 1???

Task 11 is the same as task 2???

Task 12 is doing task 11, but just subtracting one from each number.
You can write just two functions to solve all the above.
One function can be print_divisible() and the other print_odd().

print_divisible(int n) where n is the number that numbers from 1 to 1000 must divide by exactly.

Then for problems 1 to 9 you can simply give n as 1, 2, 3, ... 9.
Problems 10 and 11 are are problems 1 and 2 repeated.
Problem 12 would use print_odd() instead, where you print numbers that do not divide by 2.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12

void print_divisible(int n)
{
	if (n==1)
	{
		//Print out all numbers from 1-1000
	}
	else if( n==2)
		//Do the remainder formula
}

]


I was thinking of using if statements; however, im not allowed to use that.
Last edited on
use a for loop like this:

1
2
3
4
5
for( int i = 1; i <= 1000; ++i )
{
    if( i % n == 0 ) //no remainder e.g. it divides into it
        std::cout << i << ' ';
}
Apologize. I'm not allowed to use any if statements, or condionitanal statements. Not even switch.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void print_divisible(int divisiblenumber)
{
	int counter=1;
	while (counter<=100)
	{
		divisiblenumber=counter%divisiblenumber;


		counter++;
	
	}
}



Did something like this, but still thinking
Last edited on
well.

For 1 and 10 you can do

1
2
3
4
for( int i = 1; i <= 1000; ++i )
{
    std::cout << i << ' ';
}


for 2 - 9 and 11 you can do:

1
2
for( int i = n; i <= 1000; i += n; )
    std::cout << i << ' ';


then 12 you can do
1
2
for( int i = 1; i <= 1000; i += 2 )
    std::cout << i << ' ';
Giblit, I believe I have my code working, but I was working on my fellow classmates code and can't seem to figure out why this is not incrementing by two. In my output, I get the results "2345678.... and not "2468

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void print_divisible()
{
	int counter=0;
	int k=0;
	while(counter<=100)
	{
		k=counter+2;
		cout<<k;
		counter++;
	
	}

}

And yes Im aware I have two posts :P I was looking for more then one solution. :D
Do you need both k and counter?
Thinking of now, I don't think I do. Should it stil not work though.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

void print_divisible()
{
	int counter=0;
	
	while(counter<=100)
	{
		counter=counter+2;
		cout<<counter;
		
	
	}

}


Well, the previous code didn't have counter=counter+2;, it had counter++;
I know I changed it :D to see if it was correct. Thank you though :)
Topic archived. No new replies allowed.