Multiply two number with out using multiplicative operator

dhirenkaunar-15094
Posted by dhirenkaunar-15094 under C# category on | Points: 40 | Views : 1231
public class CheckProgram
{
public static int SumNumbers(int i, int j)
{
int result=0;
int flgCtr = 0;

if (i < 0)
{
i = 0 - i;
flgCtr++;
}
if (j < 0)
{
j = 0 - j;
flgCtr++;
}

result= MultiplyTwoNumber(i,j);
if (flgCtr % 2 != 0)
result = 0 - result;
return result;
}

private static int MultiplyTwoNumber(int i, int j)
{
int res = 0;
int ctr = 0;
int startValue = 0;

if (i > j)
{
ctr = j;
startValue = i;
}
else
{
ctr = i;
startValue = j;
}

for (int k = 0; k < ctr; k++)
{
res += startValue;
}
return res;

}
}

Calling function : CheckProgram.SumNumbers(-2, 4);
result will be : -8

Please share any other solution you have .. constraint is no multiplicative operators

Comments or Responses

Login to post response