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...
Linking to data can reduce ________ and provide an efficient way of interacting with the data
Fill in the blank(s) with correct word
Computer Science & Information Technology
In a(n) ________ network , the most common network found in many businesses, one centralized computer is the server that stores all the shared data on the network, including e-mail, Web pages, files, and applications
A) server B) node C) architecture D) client-server
Computer Science & Information Technology