A more efficient way to do this is to keep doubling the product while you can....
So, for example, \$5^2\$ is:
5 + 5 -> 10
10 + 10 -> 20
$$\begin{eqnarray*} 5 +& 5 &\longrightarrow 10 \\ 10 +& 10 &\longrightarrow 20 \end{eqnarray*} $$
then add the last 5 to get 25.
This can be done efficiently by using bit shifting:
public static int sqr (int val)
{
int mask = 1;
int factorsum = val;
int sum = 0;
while (val >= mask)
{
if ((val & mask) == mask)
{
sum += factorsum;
}
factorsum += factorsum;
mask += mask;
}
return sum;
}
This is a \$O(log(n))\$\$O(\log(n))\$ solution to the problem. See it in ideone too