Calculate square of a number without multiplication
A more efficient way to do this is to keep doubling the product while you can....

So, for example, \$5^2\$ is:

$$\begin{eqnarray*}
 5 +&amp;  5 &amp;\longrightarrow 10 \\
 10 +&amp; 10 &amp;\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 &gt;= mask)
    {
        if ((val &amp; mask) == mask)
        {
            sum += factorsum;
        }
        factorsum += factorsum;
        mask += mask;
    }
    return sum;
}


This is a \$O(\log(n))\$ solution to the problem. See it in ideone too