Divisors of n-square that are not divisors of n in C++ Program


In this tutorial, we are going to write a program that finds the divisors count of n-square and not n.

It's a straightforward problem. Let's see the steps to solve the problem.

  • Initialize the number n.

  • Initialize a counter for divisors.

  • Iterate from 2 to n^2n2.

    • If the n^2n2 is divisible by the current number and nn is not divisible by the current number, then increment the count.

  • Print the count.

Example

Let's see the code.

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int getNumberOfDivisors(int n) {
   int n_square = n * n;
   int divisors_count = 0;
   for (int i = 2; i <= n_square; i++) {
      if (n_square % i == 0 && n % i != 0) {
         divisors_count++;
      }
   }
   return divisors_count;
}
int main() {
   int n = 6;
   cout << getNumberOfDivisors(n) << endl;
   return 0;
}

Output

If you execute the above program, then you will get the following result.

5

Conclusion

If you have any queries in the tutorial, mention them in the comment section.

Updated on: 28-Jan-2021

59 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements