What you want to see on DotNetFunda.com ?
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 54035 |  Welcome, Guest!   Register  Login
 Home > Interview Questions > Others Interview Questions > Multiply a number without using the ' ...

Multiply a number without using the '*' operator.

Interview question and answer by: Dotnetrajanikanth | Posted on: 4/18/2012 | Category: Others Interview questions | Views: 961 | | Points: 40


Answer:

Simplest answer that i could give is

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

Hope this helps...

Source: Experience | Asked In: interview | Alert Moderator 
Found interesting? Add this to:


 Responses

Posted by: Dotnetrajanikanth | Posted on: 19 Apr 2012 01:35:51 AM | Points: 10 | Alert Moderator 

The same idea can be used to implement division also

Posted by: Dhiren.Kaunar@Gmail.Com | Posted on: 19 Apr 2012 01:56:44 AM | Points: 10 | Alert Moderator 

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 | Posted on: 19 Apr 2012 04:07:24 AM | Points: 10 | Alert Moderator 

Hope this helps... - this is not helpfull. I am reading a lot of "spam" articles in the last months.

Posted by: Dotnetrajanikanth | Posted on: 19 Apr 2012 04:39:43 AM | Points: 10 | Alert Moderator 

Thank you for pointing out the error...

>> Write Response - Respond to this post and get points

Even more ... | Submit Interview Questions and win prizes!

More Interview Questions from Dotnetrajanikanth

    Even more ... | Submit Interview Questions and win prizes!


    About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
    General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
    Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/24/2013 9:42:21 AM