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 Answers
+ 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