0

Help optimizing code

I have written a prime number program, which shows prime numbers , quantity of prime numbers and sum of prime numbers

2nd May 2017, 8:02 AM
Naveed
Naveed - avatar
3 odpowiedzi
+ 1
Hint: What if i = 5?
2nd May 2017, 9:23 AM
Bebida Roja
Bebida Roja - avatar
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";                                                  }                    }
2nd May 2017, 8:02 AM
Naveed
Naveed - avatar
0
5 in place of 3 in AND operator
2nd May 2017, 9:25 AM
Naveed
Naveed - avatar