
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Divisors of n^2 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.
#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.
Advertisements