Advertisement
Mira2706

hw1_5

Nov 18th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     int start = 1;
  8.     int finish = 2;
  9.     int sum1 = 0;
  10.     int counter = 0;
  11.  
  12.     cout<<"Enter 2 whole numbers to define the range: ";
  13.     cin>>start>>finish;
  14.     bool IsPrime = true;
  15.  
  16.     while (start>finish)
  17.     {
  18.         cout<<"Invalid range. Try again."<<endl;
  19.         cin>>start>>finish;
  20.  
  21.     }
  22.  
  23.     for(int i = start; i < finish; i++)
  24.     {
  25.         for (int j = 1; j < i/2 ; j++)
  26.         {
  27.             if (i%j == 0)
  28.             {
  29.                 sum1+=j;
  30.  
  31.             }
  32.  
  33.         }
  34.         for(int k = 2; k<= sum1 / 2; k++)
  35.         {
  36.             if(sum1%k == 0)
  37.             {
  38.                 IsPrime = false;
  39.                 break;
  40.             }
  41.  
  42.         }
  43.             if (IsPrime)
  44.             {
  45.                 counter++;
  46.             }
  47.  
  48.         }
  49.         cout<<"There are "<<counter<<" numbers in the range whose sum of the divisors is a prime number"<<endl;
  50.  
  51.  
  52.  
  53.  
  54.  
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement