# Aliquot sum in C++?

Here we will see what is the Aliquot sum? The Aliquot sum of n is the sum of all perfect factors of a n except the n. For example, if the number is 20, then the perfect factors are (1, 2, 4, 5, 10). So the Aliquot sum is 22.

One interesting fact is that, if Aliquot sum of a number is the number itself, then that number is a perfect number. For example, 6. The factors are (1, 2, 3). The Aliquot sum is 1+2+3=6.

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

## Algorithm

### getAliquotSum(n)

begin
sum := 0
for i in range 1 to n, do
if n is divisible by i, then
sum := sum + i
end if
done
return sum.
end

## Example

Live Demo

#include <iostream>
using namespace std;
int getAliquotSum(int n) {
int sum = 0;
for(int i = 1; i<n; i++) {
if(n %i ==0) {
sum += i;
}
}
return sum;
}
int main() {
int n;
cout << "Enter a number to get Aliquot sum: ";
cin >> n;
cout << "The Aliquot sum of " << n << " is " << getAliquotSum(n);
}

## Output

Enter a number to get Aliquot sum: 20
The Aliquot sum of 20 is 22