Print all Perfect Numbers from an array whose sum of digits is also a Perfect Number | C ++ program for the above approach ; Function to check if a number is perfect number or not ; Stores sum of proper divisors ; If sum of digits is equal to N , then it 's a perfect number ; Otherwise , not a perfect number ; Function to find the sum of digits of a number ; Stores sum of digits ; Return sum of digits ; Function to count perfect numbers from an array whose sum of digits is also perfect ; Traverse the array ; If number is perfect ; Stores sum of digits of the number ; If that is also perfect number ; Print that number ; Driver Code ; Given array ; Size of the array ; Function call to count perfect numbers having sum of digits also perfect
#include <bits/stdc++.h> NEW_LINE using namespace std ; int isPerfect ( int N ) { int sumOfDivisors = 1 ; for ( int i = 2 ; i <= N / 2 ; ++ i ) { if ( N % i == 0 ) { sumOfDivisors += i ; } } if ( sumOfDivisors == N ) { return 1 ; } else return 0 ; } int sumOfDigits ( int N ) { int sum = 0 ; while ( N != 0 ) { sum += ( N % 10 ) ; N = N / 10 ; } return sum ; } void countPerfectNumbers ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; ++ i ) { if ( isPerfect ( arr [ i ] ) ) { int sum = sumOfDigits ( arr [ i ] ) ; if ( isPerfect ( sum ) ) { cout << arr [ i ] << " ▁ " ; } } } } int main ( ) { int arr [ ] = { 3 , 8 , 12 , 28 , 6 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; countPerfectNumbers ( arr , N ) ; return 0 ; }