Problem You are given a positive integer X. Your task is to tell whether there exist two positive integers a and b (a > 0, b > 0) such that 2⋅a 2⋅b a⋅b=X If there exist positive integers a and b satisfying the above condition print YES, otherwise print NO.
Input Format The first line of input will contain a single integer T, denoting the number of test cases. Each test case consists of a single line containing a positive integer X.
Output Format For each test case, output on a new line YES or NO.
You may print each character of the string in either uppercase or lowercase (for example, the strings yes, YES, Yes, and yeS will all be treated as identical).
#include <iostream>
using namespace std;
int main() {
int t,x,i,j;
cin>>t;
while(t--)
{
cin>>x;
for(i=1; i<x; i )
{
for(j=1; j<x; j )
{
if(2*i 2*j i*j == x)
{
cout<<"Yes"<<endl;
}
else
{
cout<<"No"<<endl;
}
}
}
}
return 0;
}
According to my above code, I will get No for every iteration of if_loop and Yes for that one specific case for which it satisfies that equation, but I want it to print yes or no only once. Please tell me how it can be done.
CodePudding user response:
Take a flag variable and set to true if consition is satisfied.Print it after the loop not inside the loop
#include <iostream>
using namespace std;
int main() {
int t,x,i,j;
int flag;
cin>>t;
while(t--)
{
cin>>x;
flag = checkSatisfiedNumber(x)
if (flag == 1)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
return 0;
}
int checkSatisfiedNumber(int x){
int i,j;
for(i=1; i<x; i )
{
for(j=1; j<x; j )
{
if(2*i 2*j i*j == x)
{
return 1;
}
}
}
return 0;
}