Solved

multiply and divide by adding and subtracting in C programing language

Posted on 2011-03-10
11
803 Views
Last Modified: 2012-05-11
how can i build a program in C programing language, that  multiplies two user given numbers , by only adding numbers , and divides, by only subtracting the same numbers?
0
Comment
Question by:costaf
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 35093476
>> how can i build a program in C programing language, that  multiplies two user given numbers , by only adding numbers

For x times y, can you use a loop, and repeatedly add the same number y to itself x amount fo times ?


>> and divides, by only subtracting the same numbers?

Similarly for division, except that in this case, you need to use division just to find the value you need to subtract, so what you're asking is a bit weird.
0
 
LVL 2

Author Comment

by:costaf
ID: 35093529
that much, i had imagined. i would like some help with an example of C code. but thanks anyway.
0
 
LVL 84

Expert Comment

by:ozo
ID: 35093535
int mult(int a, int b){
  int p=0;
  unsigned u = (unsigned)a;
  while( u ){
    if( u&1 ){ p+=b; }
    b<<=1;
    u>>=1;
  }
  return p;
}
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35093560
Actually, it is more interesting than that.  Assuming no multiply or divide at all...

Starting with the user-supplied number (presumably in decimal) - how to convert this to binary without using div or mult?

Better to leave it in decimal and simulate long division and long multiiplication using repeated subtraction and addition.   This is much more efficient than brute-force repeated subtraction and addition (in binary).  You do have to be smart about carrying and borrowing digits.

I've written extended precision decimal arithmetic functions using this technique.  Nice thing is that you can do any number of digits, just like doing arithmetic by hand.
0
 
LVL 9

Expert Comment

by:mayank_joshi
ID: 35093640
multiplication using addition:-
#include<iostream>
02	using namespace std;
03	int main()
04	{
05	    int a, b, result, i;
06	 
07	 
08	    cout<<"Enter first number:"<<endl;
09	    cin>>a;
10	    cout<<"Enter second number:"<<endl;
11	    cin>>b;
12	 
13	    result = 0;
14	    i = 0;
15	 
16	    do { result+=a; i++; } while(i<b);
17	 
18	 
19	    cout << "Your result is: " << result <<endl; 
20	    system("PAUSE");
21	}

Open in new window

0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 16

Expert Comment

by:sjklein42
ID: 35093654
Solution above does not work properly for b == 0.

Use a simple "for" loop.

99 percent of the time, do/while loops are the wrong thing to use.
0
 
LVL 9

Accepted Solution

by:
mayank_joshi earned 250 total points
ID: 35093661
division using subtraction:-

#include <iostream.h>
int main()
{
	int x;
	int y;
        int rem;
	int quotient = 0;
	
	cout<<"input first number:";
	cin>>x;
	cout<<"input second number:";
	cin>>y;
	
	for(int i=0; i<y; i--)
	{
		quotient -=x;
	}
	
	cout<<"Quotient is:"<<quotient<<"Remainder is:"<<rem;
	return 0;
}

Open in new window

0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35093762
Still just scratching the surface.

Solution above for division does not handle negative x and y.

All these solutions assume integer numbers (no decimal point).  Boring!

And the division could be expected to generate some digits to the right of the decimal point rather than a simple remainder.

Is this someone's homework assignment we're doing?
0
 
LVL 2

Author Comment

by:costaf
ID: 35093788
its not a homework of some one , its a exercise i was trying to resolve, and cant seem to put it to work while learning C programing language.
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35093829
Hi. If you would like to show us your code, we'll help you figure out why it isn't working.  There are so many ways to solve this problem.
0
 
LVL 2

Author Comment

by:costaf
ID: 35093860
thank you for all your answers, it gave me plenty information which allowed me to figure out what was wrong and fix it.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
posix semaphore deadlock 13 115
An API detour question 7 79
How do I set up two external hard drives with Acronis True Image? 12 81
Computer slow / BSOD 10 44
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now