Pythagoras triplet

#include
using namespace std;
void pythagoras(int n)
{
long long int a,b,c;
if(n==1|| n==2)
cout<<"-1";
else
{
if(n%2!=0)
{
long long int p=((n+1)/2);
long long int q=((n-1)/2);
a=(p+q)(p-q);
b=2
pq;
c=((p
p)+(qq));
if((c
c)==(aa)+(bb))
{
cout<<b<<" "<<c;

        }
        else{
            cout<<-1;
        
        }
    }
    else
    {
        long long int p=n/2;
        long long int q=1;
        a=(p+q)*(p-q);
        b=2*p*q;
        c=((p*p)+(q*q));
        if((c*c)==(a*a)+(b*b))
        {
            cout<<b<<" "<<c;

        }
        else{
            cout<<-1;
        
        }
    }
    }

}

int main() {
long long int n;
cin>>n;
pythagoras(n);

return 0;

}

one testcase is showing wrong answer .pls tell the mistake

In the else section of your code, you need to print a and c rather than b and c again… Thus use, cout<<a<<" "<<c;, and then try to submit the code…

#include
using namespace std;
int main(int argc, char const *argv[])
{
int a,b,c,n,m,N;
cin>>N;

if (N%2==0)
{
  n= 1;
  m = N/2;
  a = m*m-n*n;
  c = m*m+n*n;
  cout<<a<<" "<<c;

}
else
{
	m = (N+1)/2;
	n = (N-1)/2;
	a = m*m-n*n;
	b = 2*m*n;
	c = m*m+n*n;
	if(a>b){
	cout<<a<<" "<<c;
            }
            else
            	cout<<b<<" "<<c;
}


return 0;

}
3 testcase not passing

I have edited your code… Try to submit now…

1 Like