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;
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;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.