so this program suppose to find number of primes (number that divides only by itself) in the given range. I know the problem is with my "for" statement.. i just cant find it :( Thank you

#include <iostream>
using namespace std;

int main()
{
    int intStart, intEnd, intPrime;
    intPrime=0;
    cout << "Enter starting number: ";
    cin >> intStart;
    cout << "Enter ending number: ";
    cin >> intEnd;
    int DivideBy = 2;
    for (int Number=intStart; Number<=intEnd; Number++){
    while (Number%DivideBy!=0){
      DivideBy++;
    if (DivideBy==Number){
      intPrime++; }
      }
     
 }
   cout <<"Number of primes in range: "<<intPrime<<endl;
    system("pause");
    return (0);
}

so this program suppose to find number of primes (number that divides only by itself) in the given range. I know the problem is with my "for" statement.. i just cant find it :( Thank you

You're Welcome.... :D


No wait... OK...

First, here's the code (formatted properly) that contains the problem:

int DivideBy = 2;
    for (int Number=intStart; Number<=intEnd; Number++)
    {
        while (Number%DivideBy!=0)
        {
            DivideBy++;
            if (DivideBy==Number)
            {
                intPrime++; 
            }
        }
    }

1st: set DivideBy before the while loop. You need that initialized foe each number in the for loop
2nd: testing if (DivideBy==Number) inside the loop does no good. You have to test it outside the while loop to see if you went all the way from 2 to Number. If do, it's prime.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.