Need help on project (prime numbers)

Ok,

Ive been staring at this code for 5 days now and I cant get it to work no matter what I do or try. My professor refuses to show me how to do it and instead gives me little hints. First off this is my first time doing any programming and second I cant learn this way because I don't know what right looks like or in what direction to go.

The assignment:
Write a program that counts the total number of prime numbers less than the positive integer that the user entered. A prime number is a number such that 1 and itself are the only numbers that evenly divide it, e.g., 2, 3, 5, 7, 11, 13, 17, etc. For example, if the user entered 20, your program shall output a message like “The total number of prime numbers less than 20 is 8”

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
  int main()
{
	int count = 0, x, y, z;
	bool prime = true;
	cout << "What is the total number of prime numbers?!.\n";
	cout << "Please enter a number.\n";
	cin >> x;
	for (y=3; y< x - 1; y++)
	{
		for (z = 2; z < y; z++)
		{
			if (y%z == 0)
			{
				bool prime = false;
				break;
			}
			else bool prime = true;

			if (bool prime = true)
			{
				count++;
			}
		}
	}
	cout << count;
	return 0;
}


A couple issues to start with:
Line 14 declares a new variable called prime that exists only for lines 13-16, different from the one of line 4.
Line 15 will break you out of the entire for loop on line 10, which would skip the (intended, see below) check on line 19.
Line 17: same deal as 14.
Line 19: This is not only trying to declare a new variable (which is illegal in an if statement's condition), but is also assign to it rather than (as I expect you meant) testing for equality. An equality test uses ==, while assignment uses =.

You may want to modify this to, instead of simply counting the number of primes, actually enumerate them so you can debug problems with your algorithm. Once you have it right you can adjust it back to what the assignment requires.
Ok, so your saying to first focus on just counting if a number is prime or not and then build it into the assignment?
Yeah. That way you can make sure you are actually properly recognizing primes. Then you can change the printing of each prime to just increment a counter.
Alright, I got it but how do I stop it from displaying the cout so many times?


#include<iostream>

using namespace std;

int main()
{
	int x, y;
	bool prime = true;
	cout << "What is the total number of prime numbers?!.\n";
	cout << "Please enter a number.\n";
	cin >> x;
	for (y = 3; y <= x-1;y++)
	{
		if (x%y == 0)
		{
			prime = false;
		}
		if (prime == false)
		cout << "number is not prime\n";
		else cout << "number is prime\n";
		
	}
	return 0;
}



number 13 entered

output is:

number is prime
number is prime
number is prime
number is prime
number is prime
I was suggesting actually printing the number you have recognized as prime; e.g., you'll see "5 is prime" and such. In that case you would want it to print every time.
closed account (1CfG1hU5)
here is a program that lists primes. list more with screen pauses if you increase the value of n.
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
#include <stdio.h>

int prime(int n);

int main()
{
	int i, n;

	n = 1000;
	for (i=2; i<=n; i++)
		if (prime(i))
            printf("%d ", i);

return 0;

}

int prime(int n)
{
	int i;

	if (n % 2 == 0)
		return (n==2);
	if (n % 3 == 0)
		return (n==3);
	if (n % 5 == 0)
		return (n==5);
	for (i=7; i*i <= n; i+=2)
		if (n % i == 0)
			return 0;
	return 1;
}


removed line #include <conio.h> and changed cprintf() in main to printf().
Last edited on
Alright,

Im stuck and cant get out of the hole lol. My program can determine what number is prime or not but I cant build into the class assignment.
Thanks for all the help. I was able to get it hours before the part 1 was do. This is my final code for part 1.


#include<iostream>

using namespace std;

int main()
{
	int x, y,z, count = -1;
	bool prime = true;
	cout << "What is the total number of prime numbers?!.\n";
	cout << "Please enter a number.\n";
	cin >> x;
	for (z = 2; z <= x - 1;z++)
	{	
		bool prime = true;
		for (y = 3; y <= z-1; y++)
		{
			if (z%y == 0)
			{
				prime = false;
				break;
			}
		}
		if (prime) count++;
	}
	cout << "The total number of prime numbers less than\n" << x << " "<< "is" <<" "<< count<< endl;
	return 0;
}
Topic archived. No new replies allowed.