Click here to Skip to main content
15,883,821 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to divide larger number by smaller number and express answers using quotient and remainder rather than a fraction or decimal quotient. for example if i divide 7 by 2, answer would have been given 3 as a quotient and 1 as a remainder. Divide larger number by smaller.

example:
7 / 2 = 3 r 1
2 / 7 = 3 r 1

i have done this so far but i don't know to i can always divide larger number by smaller using if and else statement.

C++
#include <iostream>
using namespace std;

int main() {

int large,small,quo,rem;
         cout << "\nEnter 2 numbers to divide: ";
	 cin >> large >> small;
	 quo = large / small;
	 rem = large % small;
	 cout << ""<< quo;
	 cout << " r "<< rem;

     return 0;
}
Posted

What you are doing is basically correct, using the '/' and '%' operators. As an alternative you can use the div and ldiv functions of the runtime library, which return the quotient and remainder in a structure. On some systems that might be slightly faster, because the remainder is usually a by-product of the division.
 
Share this answer
 
If the large is smaller than small, swap them:
C++
if(bigger < smaller)
{
   int tmp = bigger;
   bigger = smaller;
   smaller = tmp;
}

Here is your corrected program:
C++
#include <iostream>

using namespace std;

int main()
{ 
	int bigger, smaller;

	cout << "Enter a number:" << endl;
	cin >> bigger;

	cout << "Enter another number:" << endl;
	cin >> smaller;

	if(bigger < smaller)
	{
		int tmp = bigger;
		bigger = smaller;
		smaller = tmp;
	}

	int q = bigger / smaller;
	int r = bigger % smaller;

	cout << "Result: " << q << endl;

        if(r > 0)
	   cout << "Remainder: " << r << endl;

	return 0;
}
</iostream>
 
Share this answer
 
v2
Comments
danial khan 22-Oct-13 20:57pm    
thank you very much for helping :) but the another problem is that i have make this program professional. Example if i divide 99 by 9 it shows the answer like this 11 r 0 but i don't want 0 it suppose to type just 11
Captain Price 22-Oct-13 21:09pm    
then print the remainder only if it's greater than 0 :) I have edited the code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900