multiply two integer without * operator

multiply two integer without * operator

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 48359 Views - Last Post: 10 February 2010 - 01:51 AM Rate Topic: -----

#1 vipinkumar.vsk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 10-July 08

multiply two integer without * operator

Posted 10 July 2008 - 04:10 AM

hi friends

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 vipink@ascs.amity.edu or vipinkumar.vsk@sify.com...............


please help me

thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: multiply two integer without * operator

#2 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: multiply two integer without * operator

Posted 10 July 2008 - 04:13 AM

1) Think about how multiplication can be done without actually multiplying:
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]
Was This Post Helpful? 0
  • +
  • -

#3 skaoth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 91
  • View blog
  • Posts: 601
  • Joined: 07-November 07

Re: multiply two integer without * operator

Posted 10 July 2008 - 10:52 AM

Your best bet for #2) W/O using looping constructs is to probably use recursion. You would continue the recursion until you meet some sort of terminal case
Was This Post Helpful? 0
  • +
  • -

#4 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: multiply two integer without * operator

Posted 10 July 2008 - 11:35 AM

Oops, didn't see that it said without :blush:
Was This Post Helpful? 0
  • +
  • -

#5 perfectly.insane  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 644
  • Joined: 22-March 08

Re: multiply two integer without * operator

Posted 12 July 2008 - 09:22 AM

View Postgabehabe, on 10 Jul, 2008 - 04:13 AM, said:

1) Think about how multiplication can be done without actually multiplying:
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

Was This Post Helpful? 0
  • +
  • -

#6 lanec42  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 229
  • Joined: 26-March 08

Re: multiply two integer without * operator

Posted 12 July 2008 - 09:46 AM

:genius: That's a pretty cool idea.
Was This Post Helpful? 0
  • +
  • -

#7 polymath  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • Posts: 670
  • Joined: 04-April 08

Re: multiply two integer without * operator

Posted 12 July 2008 - 11:26 AM

might be over complicating things, but this should work for all integers positive and negative.

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

Was This Post Helpful? 0
  • +
  • -

#8 Ajaybajpayee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 12-July 08

Re: multiply two integer without * operator

Posted 12 July 2008 - 11:40 AM

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

hi friends

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 vipink@ascs.amity.edu or vipinkumar.vsk@sify.com...............


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
Was This Post Helpful? 0
  • +
  • -

#9 polymath  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • Posts: 670
  • Joined: 04-April 08

Re: multiply two integer without * operator

Posted 12 July 2008 - 01:43 PM

That's hard coding the stars. You want it to be dynamic. Using a loop:
#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;
}


Was This Post Helpful? 0
  • +
  • -

#10 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5833
  • View blog
  • Posts: 12,687
  • Joined: 16-October 07

Re: multiply two integer without * operator

Posted 12 July 2008 - 04:28 PM

Since answers are on the table, I thought multiply would be fun.

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.
Was This Post Helpful? 0
  • +
  • -

#11 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: multiply two integer without * operator

Posted 13 July 2008 - 06:01 AM

Here's another cool one, uses recursion twice:
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);


Was This Post Helpful? 0
  • +
  • -

#12 perfectly.insane  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 644
  • Joined: 22-March 08

Re: multiply two integer without * operator

Posted 13 July 2008 - 07:57 AM

Well, I suppose if anyone doesn't want to use the + operator either...

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

Was This Post Helpful? 0
  • +
  • -

#13 polymath  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • Posts: 670
  • Joined: 04-April 08

Re: multiply two integer without * operator

Posted 13 July 2008 - 03:10 PM

Wow your snippets are so much simpler than mine! Though my multiply works, it looks like i'm adding too many spokes to the wheel! :)
Was This Post Helpful? 0
  • +
  • -

#14 code_red  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 23-August 08

Re: multiply two integer without * operator

Posted 23 August 2008 - 01:42 PM

PROGRAM TO FIND SQUARE WITHOUT MULTIPLICATION (*)
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 :blink:

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

Was This Post Helpful? 0
  • +
  • -

#15 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: multiply two integer without * operator

Posted 23 August 2008 - 02:05 PM

h is uninitialized. Basically there is no calculation going on in the recursive function.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2