Write a program to: Prompt the user for two positive integers, divide the larger
ID: 3737883 • Letter: W
Question
Write a program to:
Prompt the user for two positive integers, divide the larger by the smaller, and print the answer as a quotient and remainder.
Use main( ) as the driver function. Allow the user to run the program as many times as desired.
Write 5 functions that main( ) calls to accomplish the task:
getIntegers( ): Prompts the user for two positive integers. Use reference parameters.
findLargeAndSmall( ): Determines which of the integers is larger and which is smaller. Returns the larger and smaller integers via reference parameters.
calcQuotient( ): Calculates and returns the quotient of the larger integer divided by the smaller.
calcRemainder( ): Calculates and returns the remainder of the larger integer divided by the smaller.
displayResults( ): Prints the quotient and the remainder in the form typically used for long division problems in grade school.
e.g. The answer for 7/2 is displayed as 3 R1.
Sample I/O:
**************************************************************
This program will ask you to enter two positive integers.
It will divide the larger by the smaller and display the
result as a quotient and remainder.
**************************************************************
Please enter your first integer and press <Enter>: 5
Please enter your second integer and press <Enter>: 9
The answer for 9/5 is 1 R 4
Would you like to process another set of numbers (Y or N)? y
**************************************************************
This program will ask you to enter two positive integers.
It will divide the larger by the smaller and display the
result as a quotient and remainder.
**************************************************************
Please enter your first integer and press <Enter>: 7
Please enter your second integer and press <Enter>: 2
The answer for 7/2 is 3 R 1
Would you like to process another set of numbers (Y or N)? y
**************************************************************
This program will ask you to enter two positive integers.
It will divide the larger by the smaller and display the
result as a quotient and remainder.
**************************************************************
Please enter your first integer and press <Enter>: 3
Please enter your second integer and press <Enter>: 5
The answer for 5/3 is 1 R 2
Would you like to process another set of numbers (Y or N)? n
Explanation / Answer
#include <iostream>
using namespace std;
void getIntegers(int &a, int &b ) {
cout<<"Please enter your first integer and press <Enter>: "<<endl;
cin >> a;
cout<<"Please enter your second integer and press <Enter>: "<<endl;
cin >> b;
}
void findLargeAndSmall( int &a, int &b , int &max, int &min) {
if(a<=b) {
max =b;
min = a;
} else {
max =a;
min = b;
}
}
int calcQuotient( int a, int b) {
return a/b;
}
int calcRemainder( int a, int b) {
return a%b;
}
void displayResults(int a, int b) {
cout<<"The answer for "<<a<<"/"<<b<<" is displayed as "<<calcQuotient(a,b)<<" R "<<calcRemainder(a,b)<<endl;
}
int main()
{
int a,b,max,min;
char c = 'y';
while(c=='y'||c=='Y') {
cout << "************************************************************** This program will ask you to enter two positive integers. It will divide the larger by the smaller and display the result as a quotient and remainder. **************************************************************" << endl;
getIntegers(a,b);
displayResults(a,b);
findLargeAndSmall(a,b,max,min);
cout<<"Max: "<<max<<" Min: "<<min<<endl;
cout<<"Would you like to process another set of numbers (Y or N)? "<<endl;
cin >> c;
}
return 0;
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.