Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Program in c++, can u please tell me where is the error and possibly fix it ? in

ID: 3671346 • Letter: P

Question

Program in c++, can u please tell me where is the error and possibly fix it ?
int main()
{
   int n;
   cout << " Please enter an integer n " << endl;
   cin >> n;
   bool isSumtwosquares = false;
   int i;
   int j;

   for ( i=0; i<=n; i++)
   {
       for ( j=0; j<=i; j++)
       {
           if (i*i + j*j == n)
               isSumtwosquares = true;
       }
   }

   if (isSumtwosquares)
       cout << "YES" << n << "=" << i-1 << "^2" << "+" << j-1 << "^2" << endl;
   else
       cout << n << " is not a sum of two squares " << endl;

c Sum of two Squiares. Write a program which prompts the user to enter an integer n, and checks whether or not n is the sum of two squares and y such that n = x2 + y2. If the answer is YES, your program is supposed to print z and y in the format shown below . That is, your program should check whether or not there exist two integer r As in (a), you are NOT allowed to use the power or the square-root functions or any other function in the cmath library For instance, each of the following number is a sum of two squares: 0 (since 0 02+02), 1 (since 1-02 + 12), 2 (since 1 = 12-12), 4 (since 4 02 +22), 5 (since 5-12 +22), 8 (since 8-22 +22), 9 (since 9-02 +32), and 10 (since 10-12+32). On the other hand, none of the following numbers is a sum of two squares: -10, 3, 6, 7, 11 In this part you don't have to worry about fast solutions. Any solution is worth full grade Format: Please enter an integer : 5 YES : 5= 1"2 + 2"2 Please enter an integer: 6 6 is not a sum of two squares

Explanation / Answer

//You forgot to break;

#include <iostream>

using namespace std;

int main()
{
    int n;
    cout << " Please enter an integer n " << endl;
    cin >> n;
    bool isSumtwosquares = false;
    int i;
    int j;

    for ( i=0; i<=n; i++)
    {
        for ( j=0; j<=i; j++)
        {
            if (i*i + j*j == n)
                {
                  isSumtwosquares = true;
                  cout << "YES" << n << "=" << i << "^2" << "+" << j << "^2" << endl;
                  return 1;
                }
        }
    }

    cout << n << " is not a sum of two squares " << endl;

return 0;
}