Check if given number contains a digit which is the average of all other digits | C ++ Program to check whether a given number contains a digit which is the average of all other digits ; Function which checks if a digits exists in n which is the average of all other digits ; Calculate sum of digits in n ; Store the digits ; Increase the count of digits in n ; If average of all digits is an integer ; Otherwise ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void check ( int n ) { set < int > digits ; int temp = n ; int sum = 0 ; int count = 0 ; while ( temp > 0 ) { sum += temp % 10 ; digits . insert ( temp % 10 ) ; count ++ ; temp = temp / 10 ; } if ( sum % count == 0 && digits . find ( sum / count ) != digits . end ( ) ) cout << " Yes " << endl ; else cout << " No " << endl ; } int main ( ) { int n = 42644 ; check ( n ) ; }