Answered # c++ program for division using loops

jaskij 45 Discussion Starter franmaez_0716 jaskij 45 Discussion Starter franmaez_0716 Lerner 582 Discussion Starter franmaez_0716

0

You are expected to post the code in the first post, but well.

The algo is pretty simple here - remember those multiplication lessons back in elementary school? For a*b you just make a+a addition b times. Similar with division.

0

yeah. actually, we also need the multiplication process using addition and i've already got that one. but, i still cannot get the algo for division because a/b is not equal to just a-a, b times..- it's zero. please give me the logic here directly..anyway thanks for helping me.. i hope to receive another reply..tnx:)

0

For integer division, a/b, you subtract b from a, as along as a>b, counting how many times you did that. The rest is a remainder (a mod b)

0

```
float divider( int nDivider ){
int d = 0;
int fQuo = 20, fFinal = nDivider - 1;
while ( d < nDivider + 1 ){
fFinal = fQuo - fFinal;
d++;
}
return fFinal;
}
```

This is my sample code.. please help me.. i really don't know what to do.. thank you.

0

To divide one number by another you could probably pass both numbers, say a and b, to the function with intention of doing a/b. Check if b is zero, because dividing by zero is undefined. Within the function you can declare two numbers, say c and d, with c being how many times you can substract b from a and still have a number zero or more and d being the remainder (eg, 10/3 = 3 with 1 remainder translates to a = 10, b = 3, c = 3, d = 1). This type of division is called integer math and is how C++ implements the / operator for type int. What you display or return is up to you. In C++ the return is just c and d is ignored, but you can display both within the function, return c only, whatever. Using the above variable names the main part of the function could look like:

```
c = 0
d = a
while b <= d
d = d - b
++c
display c and d
```

0

wow! I've run my program already..thank you for helping me.. :)

you guys are so great!

This question has already been answered. Start a new discussion instead.

Recommended Articles

I don’t want at this stage work on a big separate project as I've already got plenty ...

I am writing a java program that needs to execute shell commands, so I wrote a function that would take the command to execute as a string (ie: "mkdir ~/Folder1") and execute that command with the shell. Here is the function:

```
try
{
Runtime run = Runtime.getRuntime();
Process pr = ...
```

Hi. I have a form with list box : lst_product, datagridview : grd_order and button: btn_addline. lst_product has a list of product ids selected from database (MS Acess 2013) , grd_order is by default empty except for 2 headers and btn_addline adds rows to grd_order.

btn_addline :

`Private Sub btn_addline_Click(ByVal ...`