Complete the program below by writing the function largeDiv that finds the largest divisor of a number other than itself. (Hint: try half of the number first. Then as long as you haven't found a divisor, keep subtracting 1 from your trial value. You can be sure that 1 will divide the number if you don't find a larger divisor.)


#include
using namespace std;

// Write your function prototype for largeDiv here.





int main()
{
int number;
int divisor;

cout << "Enter an integer greater than 1 => ";
cin >> number;

divisor = largeDiv( number );

if ( divisor == 1 )
cout << number << " is a prime number." << endl;
else
cout << divisor << " is the largest divisor of " << number << endl;

return 0;
}

// Write your function definition for largeDiv here.



// Prototype
int largeDiv( int ); or int largeDiv( int num );

// Definition
//
// Find the largest divisor of num that is less than num
// Precondition: num > 1
//
int largeDiv( int num )
{
int tryDiv;
for (tryDiv = num / 2; num % tryDiv != 0; tryDiv--) {}
return tryDiv;
}

Computer Science & Information Technology

You might also like to view...

Two kinds of Java programs are applications and applets. Define and discuss each.

What will be an ideal response?

Computer Science & Information Technology

Which virus detection method creates a virtual environment that simulates the central processing unit (CPU) and memory of the computer?

A. static analysis B. dynamic scanning C. code emulation D. string scanning

Computer Science & Information Technology

The pointer looks like a(n) ________ recorder when you are recording a macro

Fill in the blank(s) with correct word

Computer Science & Information Technology

A picture positioned directly in the text at the insertion point is a(n) ________ object

Fill in the blank(s) with correct word

Computer Science & Information Technology