Aliquot sum in C++?



Aliquot sum in C++

Aliquot sum of a positive integer n is the sum of all proper divisors of n. A positive proper divisor is a divisor of a number, excluding the number itself. For example, for n = 6; 1, 2, 3 are positive proper divisors but 6 itself is not.

Scenario 1

Input: n = 20
Output: 22
Explanation: Proper divisors of 20 are: 1, 2, 4, 5, 10
Sum of proper divisors are: 1 + 2 + 4 + 5 + 10 = 22
So, aliquot sum of 20 is : 22

Scenario 2

Input: 15
Output: 9
Explanation: Proper divisors of 15 are: 1, 3, 5
Sum of proper divisors are: 1 + 3 + 5 = 9
So, aliquot sum of 15 is : 9

One interesting fact is that if the Aliquot sum of a number is equal to the number itself, then that number is a perfect number. For example, n = 6; proper divisors of 6 are (1, 2, 3). So, the Aliquot sum is 1 + 2 + 3 = 6, which is equal to the given number n. Therefore, 6 is a perfect number.

Algorithm to Calculate Aliquot Sum

Let us see how we can get the Aliquot sum using the following algorithm:

  • Step 1: Declare and initialize a variable n = 20.

  • Step 2: Declare and initialize another variable, sum = 0, to store the sum of proper divisors.

  • Step 3: Iterate using a for loop from i = 1 to n - 1.

  • Step 4: If n % i == 0 (i.e., i is a proper divisor of the given number n), add i to the sum.

  • Step 5: Once the loop ends, print the sum.

C++ Program to Calculate Aliquot Sum of a Number

The following is the C++ program to calculate the Aliquot sum of a given number:

#include <iostream>
using namespace std;
int main() {
   int n = 20;
   cout << "The number is: " << n << endl;
   int sum = 0;
   //  proper divisors of n
   for (int i = 1; i < n; i++) {
      if (n % i == 0) {
         sum += i;
      }
   }
   cout << "The Aliquot sum of " << n << " is " << sum << endl;
   return 0;
}

Following is the output of the above program:

The number is: 20
The Aliquot sum of 20 is 22
Manisha Chand
Manisha Chand

Words That Decode Code

Updated on: 2025-07-31T14:59:55+05:30

275 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements