## Interview Question

**Country:**United States

**Interview Type:**Written Test

With less complexity

#include <stdio.h>

#include <stdlib.h>

int main()

{

int multiplicant,multiplier,i,result=0;

scanf("%d",&multiplicant);

scanf("%d",&multiplier);

for(i=0;i<multiplicant/2;i++)

{

result+=multiplier;

}

result+=result;

if(multiplicant%2 !=0){

result+=multiplier;

}

printf("result =%d",result);

return 0;

}

For integers

```
#include <stdio.h>
#include <stdlib.h>
int multiply(int multiplier, int multiplicant);
int main()
{
int multiplicant,multiplier,i,result=0;
scanf("%d",&multiplicant);
scanf("%d",&multiplier);
printf(" result =%d", multiply(multiplier,multiplicant));
return 0;
}
int multiply(int multiplier, int multiplicant){
if(multiplicant == 0 || multiplicant ==1){
return multiplier;
}
int result = multiply(multiplier, multiplicant/2);
result +=result;
if( multiplicant %2 ==0){
return result;
} else{
return result+multiplier;
}
}
```

```
def mult(a, b):
sum = 0
while b != 0:
if b & 1 != 0:
sum = sum + a
b >>= 1
a <<= 1
return sum
print(mult(44, 88))
```

It is same as saying ::

sum=sum+(b&1)a+(b>>1&1)a*2+(b>>1&1)a*4+(b>>1&1)a*8 ......

For example ::

344*244

is put it in form (344/2)*(244*2)

(172/2)*(244*4)

(86/2)*(244*8)=43*244*8

sum=sum+244*8;

sum=sum+244*16;

sum=sum+244*64;

sum=sum+244*256;

This can be simply put as

while(b!=0)

{

if(b is odd)

sum=sum+a;

b=b/2;

a=a*2;

}

I think the tricky part is that you need to iterate for the small number, also take care of the sign. C# code

```
public int Multiply(int a, int b)
{
int sign = a > 0 ^ b > 0 ? -1 : 1;
a = Math.Abs(a);
b = Math.Abs(b);
int n = Math.Min(a, b);
int number = Math.Max(a, b);
int total = 0;
for (int i = 0; i < n; i++)
total += number;
return sign * total;
}
```

#include<stdio.h>

- Amogh Singhal December 07, 2015int main(void){

int a ,b,s=0,m,i=1;

printf(" \n Enter number 1 ");

scanf("%d",&a);

printf("Enter number 2 ");

scanf("%d",&b);

while(b>10)

{

m=b%10;

m=m*a*i; // 12x11 => 12 + 120

s=s+m;

b=b/10;

i=i*10;

scanf("%d",&s);

}

scanf("%d",&s);

return 0;

}