# Problem in finding the maximum of product of two numbers whose sum is 100

### Question

• I was trying to find out the max of product of two nos. whose sum is 100.
Like 1 and 99, 1 * 99 = 99 and 1 + 99 = 100.

But after I compile the below, it didn't show expected results.

It shows 45 * 55 = 2475 . . .
However, it should show 50 * 50 = 2500 . . .

Plz tell me if I am missing something.
Thank you.

class Program
{
static void Main()
{
int x = 0;
int y = 100;

int max = x * y;

int product;

for (int i = 0; x <= y; i++)
{
x = x + i;
y = y - i;

product = x * y;

max = Math.Max(max, product);
}

Console.WriteLine(x + " * " + y + " = " + max + " is the max of product of two nos. whose sum is 100");

}
}

Friday, February 22, 2008 12:51 AM

• Code Snippet

using System;
class Program
{
static void Main()
{
int x = 0;
int y = 100;

int max = 0;
int maxX = -1;
int maxY = -1;

int product;

while (x <= y) // i was superfluous
{
x = x + 1; // add 1
y = y - 1; // subtract 1

product = x * y;

if (product > max)
{
max = product;
maxX = x; // save the x value where we found the maximum
maxY = y; // save the y value where we found the maximum
}
}

Console.WriteLine(String.Format("{0} * {1} = {2} is the max product of two numbers whose sum is 100", maxX, maxY, max));
}
}

Friday, February 22, 2008 1:04 AM

### All replies

• Code Snippet

using System;
class Program
{
static void Main()
{
int x = 0;
int y = 100;

int max = 0;
int maxX = -1;
int maxY = -1;

int product;

while (x <= y) // i was superfluous
{
x = x + 1; // add 1
y = y - 1; // subtract 1

product = x * y;

if (product > max)
{
max = product;
maxX = x; // save the x value where we found the maximum
maxY = y; // save the y value where we found the maximum
}
}

Console.WriteLine(String.Format("{0} * {1} = {2} is the max product of two numbers whose sum is 100", maxX, maxY, max));
}
}

Friday, February 22, 2008 1:04 AM
• Thanks Tergiver,
now I found out instead of incrementing with 1 I was incrementing with i which is also incrementing itself.
Thanks a lot.
Friday, February 22, 2008 1:15 AM
• For any sum k that is greater than zero, I think the maximum product is always k * k / 4.

100 * 100 / 4 = 2500

Friday, February 22, 2008 11:45 PM