0
Help optimizing code
I have written a prime number program, which shows prime numbers , quantity of prime numbers and sum of prime numbers
3 Respuestas
+ 1
Hint: What if i = 5?
0
#include <iostream>
using namespace std;
int main()
{
int upperlimit = 0;
int lowerlimit = 0;
int count = 0;
int sum = 0;
cout << "Please enter upper limit: ";
cin>>upperlimit;
cout << "Please enter lower limit: ";
cin>>lowerlimit;
if (lowerlimit>1)
{
for (int i=lowerlimit; i<=upperlimit; i++)
{
if((i%2) !=0 && (i%3) !=0)
{
count++;
sum = sum + i;
cout<<i<<" ";
}
}
cout <<"\n total prime number : "<<count<<endl;
cout <<"\n sum of prime number: "<<sum<<endl;
}
if (lowerlimit>upperlimit)
{
cout<<"Lower limit cannot be greater than upper limit";
}
if (upperlimit <2)
{
cout<<" upper limit cannot be less than 2";
}
}
0
5 in place of 3 in AND operator