// File: maxDivisors1-N.cpp // find the number between 1-100 with the maximum number of divisors #include int noOfDivisors(int n); /* main() ---------------------------------------------------------- */ void main(){ int n = 1, //loop variable count, //no of divisors of a number which, //holds value of n with max no of divisors max = 0; //max no. of divisors //generate and test each value of n, 1-99 while(n < 100){ count = noOfDivisors(n); //temporary output line: print n and no. of its divisors cout << endl << "n = " << n << " divs = " << count; if(count > max){ max = count; which = n; } n = n + 1; } cout << endl << which << " has the max no. of divisors (" << max << ")" << endl; } /* noOfDivisors() -------------------------------------------------------- * return no. of divisors of n */ int noOfDivisors(int n){ int pd = 2, //potential divisor count = 0; //no. of divisors //count divisors of n while(pd < (n)){ if(0 == (n % pd)) count++; pd++; } //return results return count; }