## 17 Replies - 62079 Views - Last Post: 10 February 2010 - 01:51 AM

### #1

# multiply two integer without * operator

Posted 10 July 2008 - 04:10 AM

can any one tell me that how can a write a c program to multiply of any two number without the using of the * operator.

2) the second one is that how can i print this format without use of any loop (for,while,do-while)

*

* *

* * *

* * * *

* * * * *

please reply me on my email [email protected] or [email protected]..............

please help me

thank you.

##
**Replies To:** multiply two integer without * operator

### #2

## Re: multiply two integer without * operator

Posted 10 July 2008 - 04:13 AM

5 * 6

5 + 5 + 5 + 5 + 5 + 5

Write a for loop that would be run (in this example) 6 times, and add 5 to a total each time. (make it more generic, think of the 6 as being n)

2) Nested for loops are the way to go with this one.

[rules][/rules]

### #3

## Re: multiply two integer without * operator

Posted 10 July 2008 - 10:52 AM

### #4

## Re: multiply two integer without * operator

Posted 10 July 2008 - 11:35 AM

*without*

### #5

## Re: multiply two integer without * operator

Posted 12 July 2008 - 09:22 AM

gabehabe, on 10 Jul, 2008 - 04:13 AM, said:

5 * 6

5 + 5 + 5 + 5 + 5 + 5

Write a for loop that would be run (in this example) 6 times, and add 5 to a total each time. (make it more generic, think of the 6 as being n)

Another very similar method is to the shift operators. For example:

5 * 6

becomes:

(5 << 2) + (5 << 1) = 30

20 + 10 = 30

This works because x << y is mostly equivalent to x * (2^y). So, this becomes:

(5 * 2^2) + (5 * 2^1) =

(5 * 4) + (5 * 2) =

5 + 5 + 5 + 5 + 5 + 5

The primary advantage is that the number of iterations is limited to the number of bits in the multiplier. However, one must pay special attention to negative numbers. So 65535 * 65535 would require 16 adds and 16 shifts for computing the number, and another 16 of each for internal state, as opposed to 65535 adds.

In pseudo code, this could be done like:

let i = 0 let total = 0 while multiplicand > zero: if first bit of multiplicand is one: let total = total + (multiplier << i) increment i let multiplicand = multiplicand >> 1

The result would be left in total.

One would have to add additional code for this to work with negative numbers. Also, this only works for integral types.

This post has been edited by **perfectly.insane**: 12 July 2008 - 09:27 AM

### #6

## Re: multiply two integer without * operator

Posted 12 July 2008 - 09:46 AM

### #7

## Re: multiply two integer without * operator

Posted 12 July 2008 - 11:26 AM

All Fixed. I've tested it and it works.

#include <iostream> using namespace std; class math { private: static int multiply_int(int x, int y, int z) { //private cause it is a helper overload if (y>1) { x+=z; y--; return multiply_int(x,y,z); } else return (x); } public: static int multiply_int(int x, int y) { int z=x; if (x!=0 && x!=1 && x!=(-1)) { if (y==1) return x; if (y>1) { y--; x+=z; return (multiply_int(x,y,z)); } else if (y<(-1)) { y=(0-y); //same as y=y*-1 int a=multiply_int(x,y); a=(0-a); //same as y=a*-1 return (a); } else if (y==-1) { x=0-x; return (x); } else return 0; } else if (x==1) return y; else if (x==(-1)) { y=0-y; return (y); } else return 0; } }; int main() { int x, y; cout << "Input x: "; cin >> x; cout << "Input y: "; cin >> y; cout << "\tx * y without using a multiplication operator: " << math::multiply_int(x,y) << "\n\tx * y using the multiplication operator: " << (x*y) << "\nPress any key then enter to exit. "; cin >> x; return 0; }

This post has been edited by **polymath**: 12 July 2008 - 01:23 PM

### #8

## Re: multiply two integer without * operator

Posted 12 July 2008 - 11:40 AM

vipinkumar.vsk, on 10 Jul, 2008 - 04:10 AM, said:

can any one tell me that how can a write a c program to multiply of any two number without the using of the * operator.

2) the second one is that how can i print this format without use of any loop (for,while,do-while)

*

* *

* * *

* * * *

* * * * *

please reply me on my email [email protected] or [email protected]..............

please help me

thank you.

hey u can multiply two nos. without using * operator u need to use any one loop

u can use while loop

u can put 1st no. in while loop and run it till it becomes zero

and add another no. simulaneously. The no. u put in while condition

u must decrement it by using --operator

hey u can print that format without using any loop it's very simple

u need to use printf function only

printf("*\n");

printf("**\n");

printf("***\n");

and so on it's very easy

### #9

## Re: multiply two integer without * operator

Posted 12 July 2008 - 01:43 PM

#include <iostream> using namespace std; int main(){ int a; cout << "Imput the number of *s to output at the largest row: "; cin >> a; int control2; int controller=1; for(;controller<=a;controller++) { for (control2=0; control2 < controller; control2++) cout << "*"; cout << endl; } cout << "\nPress any key then enter to exit." << endl; cin >> a; return 0; }

Using recursitivity:

#include <iostream> using namespace std; void output_stars(int a) { if (a>0) { cout << "*"; a--; output_stars(a); } } void output_rows(int numsmallest, int numlargest) { if (numsmallest<=numlargest) { output_stars(numsmallest); cout << '\n'; numsmallest++; output_rows(numsmallest, numlargest); } } int main () { output_rows(1,5); int exit; cout << "\nPress any key then enter to exit. "; cin >> exit; return 0; }

### #10

## Re: multiply two integer without * operator

Posted 12 July 2008 - 04:28 PM

int multiply(int x, int y) { int n = 0; if ((x<0 && y<0) || (y<0) ) { x=0-x; y=0-y; } while (y>0) { if (y & 1 > 0) { n += x; } x <<= 1; y >>= 1; } return n; }

I know this looks like one of those little C code koans you see sometimes. To understand this, you need to understand that computers multiply both by addition and by bit shifts. Rather than rehash the approach, I thought this page did a good job.

This line if ((x<0 && y<0) || (y<0) ) { x=0-x; y=0-y; } does double duty. If both are negative, we want them positive. It one is negative, we want it to be x rather than y. If x is negative, the sign will be processed consistently.

### #11

## Re: multiply two integer without * operator

Posted 13 July 2008 - 06:01 AM

int sum(int x,int y) { if(!y) return x; return sum(x+1,y-1); } int multiply(int x,int y) { if(y==1) return x; return sum(x,multiply(x,y-1)); } // Usage multiply(num1,num2);

### #12

## Re: multiply two integer without * operator

Posted 13 July 2008 - 07:57 AM

int add(int n1, int n2) { unsigned int n1u = static_cast<unsigned int>(n1); unsigned int n2u = static_cast<unsigned int>(n2); unsigned int c = n1u & n2u; unsigned int s = n1u ^ n2u; while( c ) { unsigned int t = (c <<= 1); c &= s; s ^= t; } return static_cast<int>(s); }

This post has been edited by **perfectly.insane**: 13 July 2008 - 08:02 AM

### #13

## Re: multiply two integer without * operator

Posted 13 July 2008 - 03:10 PM

### #14

## Re: multiply two integer without * operator

Posted 23 August 2008 - 01:42 PM

WHY DOESN'T THIS CODE WORK

#include<stdio.h> #include<conio.h> int square(int,int); void main() { int n,m; clrscr(); printf("enter the number"); scanf("%d",&n); m=square(n,0); printf("square is %d",m); getch(); } int square(int s,int cont) { int h; if(cont==s) return(h); else h=s+square(s,cont++); }

can any one please tell me wats wrong wid this code....It does nt show any error but also does not give result

This post has been edited by **code_red**: 23 August 2008 - 01:45 PM

### #15

## Re: multiply two integer without * operator

Posted 23 August 2008 - 02:05 PM