Return to the BobbyGriggs.com C++ Nook

Chapter 5 - Integer division with 0 remainder not displayed.
© 1998 by Bobby Griggs.





// Program name:  Division



// Purpose:  Perform division on two integers and return the quotient



// and remainder.  The program will swap the integers if the second



// integer is larger than the first.  Additionally, the remainder



// will not be displayed if it equals 0.







// Win32 console application







#include < iostream.h >



#include < conio.h >







void Input_numbers(int &n1,int &n2);



void Swap(int &n1,int &n2);



void Calculate(int n1, int n2,int &q,int &r);



void Display(int q,int r);







void main()



{



	int num1,num2,quot,rem;







	Input_numbers(num1,num2);



	Calculate(num1,num2,quot,rem);



	Display(quot,rem);



	return;



}







void Input_numbers(int &n1,int &n2)



{



	cout << "Enter first integer: "; cin>> n1;



	cout << endl; cout << "Enter second integer: "; cin>> n2;



	cout << endl; Swap(n1,n2); return; } void Swap(int &n1,int &n2) { int hold; if (n1 < n2) { hold="n2;" n2="n1;" n1="hold;" } return; } void Calculate(int n1, int n2,int &q,int &r) { q="n1" / n2; r="n1" % n2; return; } void Display(int q,int r) { cout << "Answer: " << q; if (r) cout << " r " << r << endl << endl; else cout << endl << endl; cout << "Press any key to continue ... " << endl; getche(); return; }