
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Count all perfect divisors of a number in C++
In this tutorial, we will be discussing a program to find the number of all perfect divisors of a number.
For this we will be provided with a number. Our task is to count all the perfect divisors of that given number.
Example
#include<bits/stdc++.h> using namespace std; //checking perfect square bool if_psquare(int n){ int sq = (int) sqrt(n); return (n == sq * sq); } //returning count of perfect divisors int count_pdivisors(int n){ int count = 0; for (int i=1; i*i <= n; ++i){ if (n%i == 0){ if (if_psquare(i)) ++count; if (n/i != i && if_psquare(n/i)) ++count; } } return count; } int main(){ int n = 16; cout << "Total perfect divisors of " << n << " = " << count_pdivisors(n) << "\n"; n = 12; cout << "Total perfect divisors of " << n << " = " << count_pdivisors(n); return 0; }
Output
Total perfect divisors of 16 = 3 Total perfect divisors of 12 = 2
- Related Articles
- Find sum of divisors of all the divisors of a natural number in C++
- Find all divisors of a natural number in java
- Sum of all proper divisors of a natural number in java
- Find all divisors of a natural number - Set 1 in C++
- Find all divisors of a natural number - Set 2 in C++
- Count the number of common divisors of the given strings in C++
- Program to count number of common divisors of two numbers in Python
- Check if a number is divisible by all prime divisors of another number in C++
- Count divisors of array multiplication in C++
- Integers have sum of squared divisors as perfect square in JavaScript
- Divisors of factorials of a number in java
- Program to count number of perfect squares are added up to form a number in C++
- Count all triplets whose sum is equal to a perfect cube in C++
- Count the numbers < N which have equal number of divisors as K in C++
- Program to find count of numbers having odd number of divisors in given range in C++

Advertisements