Finding Prime numbers

Pages: 12
@shinigami:

remainder is 0 when number/a is a integer. For example my code gets numbers from 1 to number and checks how many times the remainder is 0. So number/number= 0(meaning remander) and number/1=0(meaning remainder)

so that means count only equals 2 then it is prime else it is not prime.



Now I want to make a new code to find prime numbers in a range:
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
int num1;
int num2;
int counter=0;
int numofvalues=0;
 

cout << "Enter starting number:";
 cin >> num1;
 cout<< "Enter ending number";
 cin >>num2;
 
 for(int i = 1; num1<=num2; i++)
 
{
         if(num1%i==0)
         {
                      counter++;
                      numofvalues++;
                      }
        }
         if (counter==2)
         cout << numofvalues << "is prime" << endl;
         
 
 
 system("PAUSE");
 return(0);
}


doesnt give me any ouput??? i think its because of the for loop or do I need 2 of them?
Last edited on
Topic archived. No new replies allowed.
Pages: 12