Error in return type

I want to count the number of numbers that are both divisible by 3 and 11 and wrote the following functions but it is showing an error.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>

using namespace std;

int count1()
{
	int count = 0;
	for (int i = 0; i < 1001; i++)
	{
		if (i % 3 == 0)
		{
			count++;
		}
	}
	return count;
}

int count2()
{
	int count = 0;
	for (int i = 0; i < 1001; i++)
	{
		if (i % 11 == 0)
		{
			count++;
		}
	}
	return count;
}

int main()
{
	int total;

	total = count1 + count2;
	cout << total << endl;

	system("pause");
	return 0;
}
> total = count1 + count2;
Revise on how you call functions.

That's not how you call functions.
total = count1() + count2();

However, your logic is incorrect. Can you spot the error?
1
2
3
4
5
6
7
8
9
10
11
12
int variant1(int from, int to) // [from, to)
{
	int cnt = 0;
	for (int i = from; i < to; i++)
		cnt += !(i % 3) && !(i % 11);
	return cnt;
}

int variant2(unsigned from, unsigned to) // [from, to)
{
	return ((to + 32) / 33) - ((from + 32) / 33);
}
Last edited on
Topic archived. No new replies allowed.