Calculation of the first 50 Chiliads for prime numbers problem.

I know this program has been worked on before but whenever i use this it tells me this message : error C4716: 'primeCount' : must return a value. It is the program to output the number of prime numbers in the first 50,000 numbers, in increments of 1000. So I am unsure of where i went wrong with this problem.


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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73


#include <iostream>
#include <iomanip>
#include <cmath>

const int CHILIADS = 50;

bool isPrime (int n);
int primeCount (int x, int y);

using namespace std;
int main() 
{	
	cout << setw(10) << left << "Start" << setw(10) << "End" << setw(24) << "Number of Primes" << endl;
	
	primeCount (0, 50000);
	
	return 0;
}

// Determines whether the number is prime
bool isPrime (int n)
{
	int a;
	
	if (n == 1) 
	{
		return false;
	}
	
	for (a = 2; a <= (n / 2); a++) 
	{
		if ((n % a) == 0) 
		{
			return false;
		}
	}
		return true;
}

// Counts and organizes the prime numbers using the isPrime function
int primeCount (int x, int y) 
{
	bool prime;
	int b;
	int c = 1000;
	int counter = 0;
	int totalSum = 0;
	

	for (b = x; b <= y; b++) 
	{
		prime = isPrime (b);
		
		if (prime == true) 
		{
			counter++;
		}
		if (b == c) 
		{
			cout << setw(10) << left << (b - 999) << setw(10) << left << b << setw(12) << counter << endl;
			
			totalSum = totalSum + counter;
			counter = 0;
			c = c + 1000;
		}
	}
	
	cout << endl << "Total primes in the first 50 chiliads: " << totalSum << endl;
	cout << "Average number per chiliad: " << totalSum / CHILIADS << endl;
	
}


error C4716: 'primeCount'

If anyone could help me out with this problem that i am having it would be greatly appreciated. I hope it is a very simple fix.

Thanks!
Last edited on
@MD1212

Yep, I think it's a very simple fix. primeCount wants to do a return. So, I just made it void instead of int, and it works beautifully. Nice program.
Topic archived. No new replies allowed.