Responses |
|
The same idea can be used to implement division also
|
|
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
|
|
Hope this helps... - this is not helpfull. I am reading a lot of "spam" articles in the last months.
|
|
Thank you for pointing out the error...
|