Check whether given number N is a Moran Number or not | C ++ implementation to check if the number is Moran number ; Function to calculate digit sum ; Function to check if number is prime ; Function to check if number is moran number ; Calculate digit sum ; Check if n is completely divisible by digit sum ; Calculate the quotient ; Check if the number is prime ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int digSum ( int a ) { int sum = 0 ; while ( a ) { sum += a % 10 ; a = a / 10 ; } return sum ; } bool isPrime ( int r ) { bool s = true ; for ( int i = 2 ; i * i <= r ; i ++ ) { if ( r % i == 0 ) { s = false ; break ; } } return s ; } void moranNo ( int n ) { int dup = n ; int sum = digSum ( dup ) ; if ( n % sum == 0 ) { int c = n / sum ; if ( isPrime ( c ) ) { cout << " Yes " ; return ; } } cout << " No " << endl ; } int main ( ) { int n = 21 ; moranNo ( n ) ; return 0 ; }