# Multiply a number without using the '*' operator.

Posted by Dotnetrajanikanth on 4/18/2012 | Category: Others Interview questions | Views: 3312 | Points: 40

Simplest answer that i could give is

for(i=0;i<n;i++)
{
num = num+num
}

Hope this helps...

Posted by: Dotnetrajanikanth on: 4/19/2012 | Points: 10
The same idea can be used to implement division also
Posted by: dhirenkaunar-15094 on: 4/19/2012 | Points: 10
Hi,

The above code will return wrong result
Ex : if will execute with assumtion n=2 and num=3
for(i=0;i<n;i++)
{
num = num+num
}

i=0 num=6
i=1 num=12

it will resolve if will use third variable
res=0
for(i=0;i<n;i++)
{
res+=num ;
}
i=0 res=3
i=1 , res=6
BUt the above condition will be fail if we have -ve number
Hope the below code will help.
int num = -5, i, num2 = -3;
int res=0;
int negCtr = 0;

if (num < 0)
{
num = num * -1;
negCtr++;
}
if (num2 < 0)
{
num2 = num2 * -1;
negCtr++;
}

for (i = 0; i < num2; i++)
{
res += num;
}
if (negCtr == 1)
{
res = res * -1;

}

Thanks,
Dhiren
Posted by: Stefanstaev on: 4/19/2012 | Points: 10
Hope this helps... - this is not helpfull. I am reading a lot of "spam" articles in the last months.
Posted by: Dotnetrajanikanth on: 4/19/2012 | Points: 10
Thank you for pointing out the error...
More Interview Questions by Dotnetrajanikanth

Latest Interview Questions