Can anyone suggest me a way to multiply decimal numbers without using multiplication(*) sign. I know it looks like homework thing, but I just want to know how to achieve this. I have already done this for positive and negative integers as below:

```
int first = 2;
int second =2;
int result = 0;
bool isNegative = false;
if (first < 0)
{
first = Math.Abs(first);
isNegative = true;
}
if (second < 0)
{
second = Math.Abs(second);
isNegative = true;
}
for (int i = 1; i <= second; i++)
{
result += first;
}
if (isNegative)
result = -Math.Abs(result);
```

Want to multiply this for decimals:

```
decimal third = 1.1;
decimal fourth = 1.2;
```

Thanks

Bit of a cheat, but if the task strictly relates to all forms of multiplication (rather than just the `*`

operator), then divide by the reciprocal:

```
var result = first / (1 / (decimal)second);
```

Just another way ;)

```
if (second < 0)
{
second = Math.Abs(second);
first = (-1) * first;
}
result = Enumerable.Repeat(first, second).Sum();
```

The simplest way to multiple two decimals without using the * operator is to use the Decimal.Multiply method.

Example:

```
decimal first = -2.234M;
decimal second = 3.14M;
decimal product = Decimal.Multiply(first, second);
```

Strictly with out '*' ? I'd take note of the XOR operator implemented. Although I know this code is not much different to the OP's.

```
int first = 2;
int second =2;
int result = 0;
bool isNegative;
isNegative = (first<0)^(second<0);
first = (first<0)?Math.Abs(first):first;
second = (second<0)?Math.Abs(second):second;
for (int i = 1; i <= second; i++)
result += first;
result = (isNegative)?-Math.Abs(result):result;
```

Here's another approach, using rules of logs, for the sake of completeness:

```
decimal result = (decimal)Math.Exp(
Math.Log((double)third) + Math.Log((double)fourth));
```

It's not going to work for all decimals (in particular not for negative numbers, although it could be extended to detect these and work around them), but it's interesting nevertheless.

Similar Questions

How to find the remainder of dividing two numbers without using the modulo operator!! My teacher gave me this exact exercise and It's only my 5th lecture in a course called programming fundamentals. I

how to restrict user to enter only numbers with 6 digits and two decimal values in textbox using javascript?? //Function to allow only numbers to textbox function validate(key) { //getting key code of

I stumbled upon an error during a simple multiplication that rather surprised me. What is happening here, I always assumed * was only for matrix multiplication. x = 2; y = zeros(1,4); y(1) = 1 *x; y(2

I want to convert values in my Pivot Table to 0.0% format without moving the decimal, e.g. 1 --> 1%, NOT 100%. The code I'm using produces a Run-time error '1004': Cannot change this part of a P

since i knew how to manually convert hexadecimal to decimal using this method. Read from right to left,the last digit times with the constant value 16 and plus the first digit. For example: 12h = 2 +

How can i print the numbers of a float/double variable after the decimal point? For example: 435.5644 the output will be 5644 Thanks,

I have this question about decimal numbers from oracle to php. If i have 0,7 on oracle, for example, when i use it on php, it will be ,7, without the zero. Anyone one knows why? And a possible sol

How would you multiply a number by 3.5 without using *, / or %, operators? The number may be signed or unsigned.

i want to multiply two 512 bit integers in java and store the result.suggest some method to do this.

How can I divide two numbers in Assembly without using DIV instruction but by using shift and add method? I did that with multiplication and here is my code: mov bl, 56H ;For example mov dl, 79H ;star

I have the following object which I want to extract the numbers without attributes to a data frame as a column. I tried this, but didn't work: data.frame(do.call(rbind, alpha.bar)) Named num [1:66] 0

Why when you use the multiplication/shift method of division (for instance multiply by 2^32/10, then shift 32 to the right) with negative numbers you get the expected result minus one? For instance, i

How do I make a textbox in gridview accept numbers only without using validators? I am using C#, ASP.net. I'm hoping to use KeyPress event or something.

Say that you are in a constrained computing environment where the multiplication operation is expensive or unavailable. You have access to a stream of pixels for an image of known dimensions. How woul

I would like to parse a required, positional argument containing a comma-separated list of integers. If the first integer contains a leading minus ('-') sign, argparse complains: import argparse parse

This question already has an answer here: In C bits, multiply by 3 and divide by 16 5 answers I need to multiply a number by 3/16, rounding to zero using only bitwise operations such as ! ~ &am

Ok, here is the problem. I had to write a MIPS program that got 2 input numbers from the user. Then, I had to write a code that would output the product, quotient and remainder for the 2 numbers enter

Is it possible to compare decimal values for a value that also contains alphanumeric characters without using preg_replace or stripping out the non-numeric values? For instance comparing these 2 decim

I am trying to multiply 2 matrices together by using 1 thread for each cell of output. I am using c++/g++ on unix. How would I go about doing this? Can I do this in a loop?

I want to achieve multiplication operation in django template. For example I have the values, price=10.50 quantity=3 With the help of this link http://slacy.com/blog/2010/07/using-djangos-widthratio-

Is it possible to use VBA to detect which decimal sign is being used on the computer? I have a macro script that adds a conditional formatting to an excel sheet. The problem is that the target compute

I'm using read_csv to read CSV files into pandas data frames. My CSV files contain large numbers of decimals/floats. The numbers are encoded using the european decimal notation: 1.234.456,78 This mea

I am using the awesome JScience library to convert units. What is bothering me: The output of for example WATT.times(HOUR) is W·h. What I want to output is just Wh. Is it possible to change the output

I am having a hard time with the Multiplication method for the Complex numbers code. This line seems to be giving me the most trouble : public IntegerComplex multiply(IntegerComplex a){ return new Int

I am looking for an algorithm to multiply and divide fixed point 15.16 numbers. I already have addition and subtraction. Those were easy - simple 32-bit add and subtract. With multiply and divide, I

What is correct way to use decimal number with two decimal places in if checkup in jquery? I have writted this way $(.entered).change(function(){ if(parseInt(this.value) > 39617756.85){ alert('gr

I have a dll (project.dll) I want to sign the dll using cmd as I don't have source code of it. Is it possible to sign a dll without sourcecode. If yes, please let me know how to do it.

How can i do subtraction of integers in C without using either the unary or binary '-' operator? Or can we do the same for other data types like float/double?

I've used this in other languages, but lua seems to be lacking this rather useful function. Could one of you nice chappies provide me a lua function to get the sign of the number passed to it?

I have a list of numbers as below. How can I select the numbers and multiply them by 2 using LINQ? var Mylist = new List<int> {1,2,3,4}; ---> MyNewList = {2,4,6,8}

I have a number of decimal numbers stored in a database and need to calculate the percentage increase(or decrease) between two of the numbers using PHP. An example of two of the numbers are: 111.0516

I have a string that can be a hex number prefixed with 0x or a decimal number without a special prefix except for possibly a minus sign. 0x123 is in base 16 and -298 is in base 10. How do I conv

This question already has an answer here: How can I multiply and divide using only bit shifting and adding? 10 answers How can I perform multiplication by 36 using bit-shifting? Isn't it only

Like if i told the program n=10, how would I make it return 10*9*8*7*6*5....1? I thought a while loop but I feel I messed up somewhere because it doesn't sum up all of the numbers in the sequence. My

Given 2 numbers, x and n, what's the method to multiply x by 2^n ? For instance x=3.7 and n=5. so 3.7*2^5 = 118.4. I need this done without using the FPU commands (math coprocessor). So i figured that

Hi I'm working on an assignment and got stuck on this part, how do I validate decimal numbers/numbers in shell? It can accept numbers but not decimal numbers. I want it to be able to accept both. This

I would like to generate uniformly distributed random numbers between 0 and 0.5, but truncated to 2 decimal places. without the truncation, I know this is done by import numpy as np rs = np.random.Ran

I'm trying to create multiplication table using php as follows: <?php $cols = 10; $rows = 10; ?> ... lot of html code ... <?php echo <table border=\1\>; for ($r =0; $r < $rows;

I am struggling with code that allows me to generate and sign certificates from java code without using keytool. Additionaly due to dependency problems and incompatibilities i am unable to use bouncyc

I am trying to compare two decimal values but I am getting errors. I used if [ $(echo $result1 '>' $result2 | bc -l) -eq 1 ];then as suggested by the other Stack Overflow thread. I am getting e

I am writing an app that , among other things will be able to multiply or divide very large numbers by number 19 - Those large numbers could possibly span hundreds of digits . Do you know of a library

I know how to multiply the elements of a single tuple: tup = (2,3) tup[0]*tup[1] 6 My question is how do you multiply the elements of several tuples using a for loop? example: How do I get x = (4,5,6

How would I go about... multiplying two 64-bit numbers multiplying two 16-digit hexadecimal numbers ...using Assembly Language. I'm only allowed to use registers %eax, %ebx, %ecx, %edx, and the s

I need a program to convert Binary numbers into Decimal number in Java or in C++. is there some one who can help me.

I'm doing a simple lottery system. I've come to the part where I need to find out how much money in total have been won at the draw. My idea is to make a variable called won = 0 and multiply the prize

I have a number stored as a double and want to print it without the decimal places. So if I had the double value 919545634521.000000, it is always printed with the decimal places added to it. How can

I am attempting to take the weighted average of a list of Decimal numbers using numpy.average; however, I am receiving a TypeError. For example, consider the following. >>> from decimal impor

In my application ,I am handling large numbers(currencies) and I am formatting them using nsnumberformatter.however currently ,the number of decimal places are set according to the locale.Since large

I am having trouble with writing a python program that reads an integer number and displays its binary representation, WITHOUT MULTIPLICATION OR DIVISION? So it's supposed to convert like the integer

Possible Duplicate: How can I multiply and divide using only bit shifting and adding? I have to write functions to perform binary subtraction, multiplication, and division without using any arithme