COUNT OF PRIME NUMBERS




















#include<iostream>
using namespace std;
main()
{
int min,max,i,flag,count=0;
cout<<"Enter Min Value: "<<endl;
cin>>min;
cout<<"Enter Max Value: "<<endl;
cin>>max;
if(min<2)
{
cout<<"Lower limit can't be less than 2! "<<endl;
}

while (min<max)
{

if(min>=max)
{
cout<<"Minimum value can't be greater than or equal to maximum value! "<<endl;
break;
continue;
}
flag = 0;

for(i = 2; i <= min/2; i++)
{
if(min % i == 0)
{
flag = 1;
break;
}
}
if (flag == 1)
{
count++;
}
if (flag == 0)
{
cout<<min<<endl;
}
min++;
}
cout<<"Total Prime Numbers Are: "<<count<<endl;

}



Please check errors as I am unable to find total numbers of prime numbers!
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
#include <iostream>
#include <cmath>

int main(int /* argc */, char* /* argv */[])
{
    std::cout << "Enter min value:\n";
    int min = 0; std::cin >> min;
    std::cout << "Enter max value:\n";
    int max = 0; std::cin >> max;

    if (min < 2)
    {
        std::cerr << "Min can't be less than 2.\n";
        return -1;
    }
    else if (min >= max)
    {
        std::cerr << "Min can't be greater than or equal to max.\n";
        return -1;
    }

    int count = 0;
    for (int n = min; n <= max; ++n)
    {
        bool prime = true;
        int root = std::sqrt(n) + 1;
        for (int i = 2; i < root; ++i)
            if (n % i == 0)
            {
                prime = false;
                break;
            }
        if (prime) ++count;
    }

    std::cout << "Total count of prime numbers: " << count << '\n';

    return 0;
}
Last edited on
Topic archived. No new replies allowed.