Need some help with a prime number loop

#include <iostream>
using namespace std;

int main()
{
int k;
int prime = 2;
int count = 0;
int i = 1;

while(prime<100)
{
while(i<=prime)
{
k = prime%i;
if (k == 0)
count++;
i++;
}
}

if(count==2)
cout << prime;

else
cout << "not working";

return 0;
}


This is my code so far, the goal is to get all prime numbers between 2 and 100. The code compiles, but doesn't return back any information and continues on an endless loop until i kill it. Im not sure what is wrong, any help is greatly appreciated!
Last edited on
1
2
3
4
5
6
7
8
9
10
while(prime<100) //¿when will this condition be false?
	{  //`prime' does not ever change its value
		while(i<=prime)
		{
			k = prime%i;
			if (k == 0)
				count++;
			i++; 
		}
	}

@kevo

The reason you don't get back any info, and you stay in an endless loop, is given inside your code, as comments

[Section of your code, below]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
int k;
int prime = 2; // You initialize to 2
int count = 0;
int i = 1;

while(prime<100)// Here you wait till prime is 100 or more
{
while(i<=prime)// Here you wait while i is less than or equal to prime, which 
// right now, is a 2
{
k = prime%i;
if (k == 0)
count++;
i++;// You increase i, let's say, twice. Now, i is greater than prime, which is still 2
} //So, you leave the while loop
// You never leave this while loop, since the prime variable, is never increased
// and always stays less than 100
}


So, it seems, your program logic, is flawed..
I know how its suppose to work, I just haven't been programming long enough to know how to write it. The idea was to find the prime numbers between 2 and 100, I started prime at 2 and take it until its up to 99, picking out the numbers that can only be divisible by 2 numbers within the list between i and prime, being 1 and the number itself.

Im trying to get k to show the condition in which these two numbers occure, the TA for my class lab told me to write it as equating to 0 to show that there is no remainder, which means the number is then divisible by i.

I know i need to add another loop to show that there are only two numbers in which there is no remainder, im just not sure how to do it. Should i add it before the if loop? or create another while loop after the if loop? Can i add the while loop after the if loop without putting in an else statement in the second while loop? Do i need an else statement while the if statement is nested in the while loop?

#include <iostream>
using namespace std;

int main()
{
double k;
int prime = 2;
int count = 0;
int i = 1;

while(prime<100)
{
while(i<=prime)
{
k = prime%i;
if (k == 0)
count ++;
i++;
prime++;
else
i++;
prime++;

}


}

if(count==2)
cout << prime;

else
cout << "not working";

return 0;
}


Just put in the else statement, but its telling me there is an else without a previous if? Why is it giving me this?
Last edited on
your code, indented
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
#include <iostream> 
using namespace std; 

int main()
{
	double k; //¿why double?
	int prime = 2;
	int count = 0;
	int i = 1;

	while(prime<100) //when this ends, prime>=100
	{	
		while(i<=prime)
		{
			k = prime%i;
			if (k == 0)
				count ++;
			i++; //this is outside the `if' block
			prime++; 
			else //so this else is out of place
				i++;
			prime++; //¿?

		}


	}

	if(count==2)
		cout << prime; //printing one number greater or equal than 100

	else
		cout << "not working"; //¿? ¿what did not work?

	return 0;
}

¿have you learned about functions yet?
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
//check if `n' is a prime number
bool is_prime(int n){
   //¿can you code this part?
}
int main(){
   for(int n=2; n<=100; ++n){
      if( is_prime(n) )
         std::cout << n << ' ';
   }
   std::cout << std::endl;
}
Last edited on
Topic archived. No new replies allowed.