Traveling this summer?Check out our on-demand webinar to learn about the importance of Wi-Fi security and 3 easy measures you can start taking immediately to protect your private data while using public Wi-Fi. Follow us today to learn more!

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?

Experts Exchange Solution brought to you by ConnectWise

Enjoy your complimentary solution view.

Get every solution instantly with premium.
Start your 7-day free trial.

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

int p=0;

unsigned u = (unsigned)a;

while( u ){

if( u&1 ){ p+=b; }

b<<=1;

u>>=1;

}

return p;

}

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.

```
#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 }
```

Use a simple "for" loop.

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

```
#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;
}
```

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trialSolution 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?

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

Experts Exchange Solution brought to you by ConnectWise

Enjoy your complimentary solution view.

Get every solution instantly with premium.
Start your 7-day free trial.

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.