Write a program that reads pairs of integers between 100 an

*
Write a program that reads pairs of integers between 100 and 10, 000 from the standard
input stream and determines whether the pairs are amicable or not.
Use a while loop to read the pairs of nums.
The while loop must have some explicit stopping condition.
Use the functions that you previously defined
?readnum, properDivisor, properDivisorSum, and amicablePair.
*/

and here is my code

#include <limits>
#include <iostream>
#include <string>
using namespace std;

bool readNumber(int lbound, int ubound, int ntries, int wrong, int& num);
int properDivisorSum(int numer, string);
bool amicablePair(int a, int b);
int main()
{
   int numX, numY, sumX, sumY;
   int lbound, ubound, ntries, wrong;
   lbound = 100;
   ubound = 10000;
   ntries = 5;
   wrong = 0;
  

   cout << \"Please enter an integer num for X: \";
   cin >> numX;
   readNumber(lbound, ubound, ntries, wrong, numX);
   if(readNumber(lbound, ubound, ntries, wrong, numX) == true)
   {
       cout << \"Please enter an integer num for Y: \";
       cin >> numY;

   readNumber(lbound, ubound, ntries, wrong, numY);
   }
   if(readNumber(lbound, ubound, ntries, wrong, numX) == true && readNumber(lbound, ubound, ntries, wrong, numY) == true)
   {
  
   sumX = properDivisorSum(numX, \"X\");
   sumY = properDivisorSum(numY, \"Y\");
   amicablePair(sumX, numY);
   amicablePair(sumY, numX);

  
       if (amicablePair(sumY, numX) == true)
       {
           cout << \"They are amicable numbers \" << endl;
           cout << \"----------------------------------------------------------------------\" << endl;
           cout << \"num of X \" << numX << \" is\" << \" equals to \" << \"sum of Y divisors \" << sumY << endl;
           cout << \"----------------------------------------------------------------------\" << endl;
           cout << \"num of Y \" << numY << \" is\" << \" equals to \" << \"sum of X divisors \" << sumX << endl;
          
          
       }
       if (amicablePair(sumY, numX) == false)
       {
           cout << \"They are not amicable \" << endl;
           cout << \"----------------------------------------------------------------------\" << endl;
           cout << \"num of X \" << numX << \" is\" << \" not equals to \" << \"sum of Y divisors \" << sumY << endl;
           cout << \"----------------------------------------------------------------------\" << endl;
           cout << \"num of Y \" << numY << \" is\" << \" not equals to \" << \"sum of X divisors \" << sumX << endl;
       }
   }
   if(readNumber(lbound, ubound, ntries, wrong, numX) == false || readNumber(lbound, ubound, ntries, wrong, numY) == false)
   {
   cout <<\" game over\" << endl;
   }
system(\"pause\");
   return 0;
}

int properDivisorSum(int num, string result)
{
   {
       double limit = num;
       int sum = 0;
       for (int i = 1; i < limit; i++)
       {
           if (num % i == 0)
           {
               sum += i;
               cout << \"The divisor for \" << result << \" is \" << i << endl;
           }
       }
       cout << \"The sum of the divisors from \" << result << \" is \" << sum << \"\ \" << endl;
       cout << \"-----------------------------------------------------------------\" << endl;
       return sum;
   }
}
bool amicablePair(int sum, int num)
{
   if (sum == num)
       return true;
   else
       return false;
}

              
bool readNumber(int lbound, int ubound, int ntries, int wrong, int& num)
{
  
   if (num >= lbound && num <= ubound)

   {
       return true;

   }
   else{
           do
           {
              
               if (num < lbound || num > ubound || !cin.good())
               {
                   cout << \"try again!: \";
                   cin >> num;
                   wrong++;

                   if(wrong > ntries)
                   {
                       cout << \"Sorry..!!\ You have reached the maximum limit of Tries.\" << endl;
                   return false;
                   }
                   cin.clear();
                   cin.ignore();
               }

           else if (num >= lbound && num <= ubound)
               {
                   return true;
               }
  
           } while (num < lbound || num > ubound);      
  
}
}

program should reject non numeric input

program should only accept numbers between 100 and 10,000

once it reaches maxmimum ntries(number of tries) program should return false

Solution

#include <limits>
#include <iostream>
#include <string>
#include<stdio.h>
using namespace std;
bool readNumber(int lbound, int ubound, int ntries, int wrong, int& num);
int properDivisorSum(int numer, string);
bool amicablePair(int a, int b);
int main()
{
int numX, numY, sumX, sumY;
int lbound, ubound, ntries, wrong;
lbound = 100;
ubound = 10000;
ntries = 5;
wrong = 1;
int flag=1;
while(flag)
{

cout << \"Please enter an integer num for X: \";

  
if (scanf(\"%d\", &numX) != 1)
{
printf(\"This is not a number.\ \");
  
}

readNumber(lbound, ubound, ntries, wrong, numX);
if(readNumber(lbound, ubound, ntries-1, wrong, numX) == true)
{
cout << \"Please enter an integer num for Y: \";
cin >> numY;
readNumber(lbound, ubound, ntries-1, wrong, numY);
}
if(readNumber(lbound, ubound, ntries, wrong, numX) == true && readNumber(lbound, ubound, ntries, wrong, numY) == true)
{
  
sumX = properDivisorSum(numX, \"X\");
sumY = properDivisorSum(numY, \"Y\");
amicablePair(sumX, numY);
amicablePair(sumY, numX);
  
if (amicablePair(sumY, numX) == true)
{
cout << \"They are amicable numbers \" << endl;
cout << \"----------------------------------------------------------------------\" << endl;
cout << \"num of X \" << numX << \" is\" << \" equals to \" << \"sum of Y divisors \" << sumY << endl;
cout << \"----------------------------------------------------------------------\" << endl;
cout << \"num of Y \" << numY << \" is\" << \" equals to \" << \"sum of X divisors \" << sumX << endl;
  
  
}
if (amicablePair(sumY, numX) == false)
{
cout << \"They are not amicable \" << endl;
cout << \"----------------------------------------------------------------------\" << endl;
cout << \"num of X \" << numX << \" is\" << \" not equals to \" << \"sum of Y divisors \" << sumY << endl;
cout << \"----------------------------------------------------------------------\" << endl;
cout << \"num of Y \" << numY << \" is\" << \" not equals to \" << \"sum of X divisors \" << sumX << endl;
}
}
if(readNumber(lbound, ubound, ntries, wrong, numX) == false || readNumber(lbound, ubound, ntries, wrong, numY) == false)
{
cout <<\" game over\" << endl;
}
cout<<\"Do you want to enter another pair(y/n)\ \";
char ch;
cin>>ch;
if(ch==\'y\')
{

}
else
{
flag=0;
}

}
  

return 0;
}
int properDivisorSum(int num, string result)
{
{
double limit = num;
int sum = 0;
for (int i = 1; i < limit; i++)
{
if (num % i == 0)
{
sum += i;
cout << \"The divisor for \" << result << \" is \" << i << endl;
}
}
cout << \"The sum of the divisors from \" << result << \" is \" << sum << \"\ \" << endl;
cout << \"-----------------------------------------------------------------\" << endl;
return sum;
}
}
bool amicablePair(int sum, int num)
{
if (sum == num)
return true;
else
return false;
}
  
bool readNumber(int lbound, int ubound, int ntries, int wrong, int& num)
{
  
if (num >= lbound && num <= ubound)
{
return true;
}
else{
do
{
  
if (num < lbound || num > ubound || !cin.good())
{
cout << \"try again!: \";
cin >> num;
wrong++;
if(wrong > ntries)
{
cout << \"Sorry..!!\ You have reached the maximum limit of Tries.\" << endl;
return false;
}
cin.clear();
cin.ignore();
}
else if (num >= lbound && num <= ubound)
{
return true;
}
  
} while (num < lbound || num > ubound);
  
}
}

=======================================================

Output:

akshay@akshay-Inspiron-3537:~/Chegg$ g++ ntries.cpp
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
Please enter an integer num for X: a
This is not a number.
try again!: try again!: 123
Please enter an integer num for Y: 123
The divisor for X is 1
The divisor for X is 3
The divisor for X is 41
The sum of the divisors from X is 45

-----------------------------------------------------------------
The divisor for Y is 1
The divisor for Y is 3
The divisor for Y is 41
The sum of the divisors from Y is 45

-----------------------------------------------------------------
They are not amicable
----------------------------------------------------------------------
num of X 123 is not equals to sum of Y divisors 45
----------------------------------------------------------------------
num of Y 123 is not equals to sum of X divisors 45
Do you want to enter another pair(y/n)
y
Please enter an integer num for X: 86
try again!: 111
Please enter an integer num for Y: 1236
The divisor for X is 1
The divisor for X is 3
The divisor for X is 37
The sum of the divisors from X is 41

-----------------------------------------------------------------
The divisor for Y is 1
The divisor for Y is 2
The divisor for Y is 3
The divisor for Y is 4
The divisor for Y is 6
The divisor for Y is 12
The divisor for Y is 103
The divisor for Y is 206
The divisor for Y is 309
The divisor for Y is 412
The divisor for Y is 618
The sum of the divisors from Y is 1676

-----------------------------------------------------------------
They are not amicable
----------------------------------------------------------------------
num of X 111 is not equals to sum of Y divisors 1676
----------------------------------------------------------------------
num of Y 1236 is not equals to sum of X divisors 41

* Write a program that reads pairs of integers between 100 and 10, 000 from the standard input stream and determines whether the pairs are amicable or not. Use
* Write a program that reads pairs of integers between 100 and 10, 000 from the standard input stream and determines whether the pairs are amicable or not. Use
* Write a program that reads pairs of integers between 100 and 10, 000 from the standard input stream and determines whether the pairs are amicable or not. Use
* Write a program that reads pairs of integers between 100 and 10, 000 from the standard input stream and determines whether the pairs are amicable or not. Use
* Write a program that reads pairs of integers between 100 and 10, 000 from the standard input stream and determines whether the pairs are amicable or not. Use
* Write a program that reads pairs of integers between 100 and 10, 000 from the standard input stream and determines whether the pairs are amicable or not. Use

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site