#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!
#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;
}
elseif (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;
}