954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

c++ program for division using loops

hi! i need help by creating a program that will divide using loops wihout using any division or multiplication sign. the allowed signs are just addition and subtraction. Thank you! i will post my program if someone replies with this.

franmaez_0716
Newbie Poster
9 posts since Jan 2012
Reputation Points: 10
Solved Threads: 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.

jaskij
Junior Poster
105 posts since Oct 2011
Reputation Points: 55
Solved Threads: 18
 

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:)

franmaez_0716
Newbie Poster
9 posts since Jan 2012
Reputation Points: 10
Solved Threads: 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)

jaskij
Junior Poster
105 posts since Oct 2011
Reputation Points: 55
Solved Threads: 18
 
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.

franmaez_0716
Newbie Poster
9 posts since Jan 2012
Reputation Points: 10
Solved Threads: 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
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

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

franmaez_0716
Newbie Poster
9 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You