This program is running but I am looking for a more efficient way to run this program. You can see I commented out the code because I think it can work out without that extra code. however, I am a little bit confused. in the else statement, I am trying to decrement the x value so it becomes x=8 and runs the loop again and decrement again. But it is not working. can anyone help me what should I do? thanks.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int x, y;
if (x = 9) {
for (y = 0; y < 9; y++)
if (3 * pow (x, 2) + 4* pow(y, 2) == 271)
cout << "x value is : " << x << endl << "y value is: " << y<<endl;
else {
x--;
}
}
/* if (x = 8) {
for (y = 0; y < 9; y++)
if (3 * pow(x, 2) + 4 * pow(y, 2) == 271)
cout << "x value is : " << x << endl << "y value is: " << y<<endl;
}
if (x = 7) {
for (y = 0; y < 9; y++)
if (3 * pow(x, 2) + 4 * pow(y, 2) == 271)
cout << "x value is : " << x << endl << "y value is: " << y<<endl;
}
if (x = 6) {
for (y = 0; y < 9; y++)
if (3 * pow(x, 2) + 4 * pow(y, 2) == 271)
cout << "x value is : " << x << endl << "y value is: " << y<<endl;
}
if (x = 5) {
for (y = 0; y < 9; y++)
if (3 * pow(x, 2) + 4 * pow(y, 2) == 271)
cout << "x value is : " << x << endl << "y value is: " << y<<endl;
}
if (x = 4) {
for (y = 0; y < 9; y++)
if (3 * pow(x, 2) + 4 * pow(y, 2) == 271)
cout << "x value is : " << x << endl << "y value is: " << y<<endl;
}
if (x = 3) {
for (y = 0; y < 9; y++)
if (3 * pow(x, 2) + 4 * pow(y, 2) == 271)
cout << "x value is : " << x << endl << "y value is: " << y<<endl;
}
if (x = 2) {
for (y = 0; y < 9; y++)
if (3 * pow(x, 2) + 4 * pow(y, 2) == 271)
cout << "x value is : " << x << endl << "y value is: " << y<<endl;
}
if (x = 1) {
for (y = 0; y < 9; y++)
if (3 * pow(x, 2) + 4 * pow(y, 2) == 271)
cout <<"x value is : "<< x <<endl<<"y value is: "<< y<<endl;
}
*/
system("pause");
return 0;
}
Just use a for loop to iterate over values of x
:
for (x = 9; x > 0; x--) {
for (y = 0; y < 9; y++) {
if (3 * pow (x, 2) + 4* pow(y, 2) == 271) {
cout << "x: " << x << endl << "y: " << y << endl;
}
}
}