Canada Numbers | C ++ implementation for the above approach ; Function to calculate sum of all trivial divisors of given natural number ; Final result of summation of trivial divisors ; Find all divisors which divides ' num ' ; if ' i ' is divisor of ' num ' ; if both divisors are same then add it only once else add both ; Function to return sum of squares of digits of N ; Function to check if N is a Canada number ; Driver Code ; Given Number ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int divSum ( int num ) { int result = 0 ; for ( int i = 1 ; i <= sqrt ( num ) ; i ++ ) { if ( num % i == 0 ) { if ( i == ( num / i ) ) result += i ; else result += ( i + num / i ) ; } } return ( result - 1 - num ) ; } int getSum ( int n ) { int sum = 0 ; while ( n != 0 ) { int r = n % 10 ; sum = sum + r * r ; n = n / 10 ; } return sum ; } bool isCanada ( int n ) { return divSum ( n ) == getSum ( n ) ; } int main ( ) { int n = 125 ; if ( isCanada ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }