Please solve the mathematics problem using nested for loops in C++. Given 2 inte
ID: 3934821 • Letter: P
Question
Please solve the mathematics problem using nested for loops in C++.
Given 2 integers x and y, x [0, 100] and y [25, 75]. Find the values of x and y that maximize the expression xy - y 2 (y squared) .
Your program should display the solutions in this format, where the # symbols should be replaced by your solutions.
x=#, y=# will maximize the expression.
The max value of the expression is #.
Hint: nested for loops work in this manner:
for ( ; ; ) // if this loop will circle 7 times,
{ for ( ; ; ) // and if this loop will circle 3 times,
{
statements; // then the statements will run 7x3=21 times.
}
}
Explanation / Answer
The given expression can be changed to:
x*y+y2=(x+y)*y
CODE:
void main(){
int max=0,x,y,i,j,temp;
for(i=0;i<=100;i++)
{
for(j=25;j<=75;j++){
temp=(i+j)*j;
if(temp>max){
max=temp;
x=i;
y=j;
}
}
}
cout<<"Maximized Value is:"<<((x+y)*y);
cout<<"x="<<x<<" y="<<y;
}