Check whether given number N is a Moran Number or not

Given an integer N, check whether the given number is a Moran Number or not. Moran numbers are a subset of Harshad numbers.

A number N is a Moran number if N divided by the sum of its digits gives a prime number. For example some Moran numbers are 18, 21, 27, 42, 45 and so on.

Examples:



Input: N = 34
Output: No
Explanation:
34 is not a moran number because it is not completely divisible 7 (sum of its digits).

Input: N = 21
Output: Yes
Explanation:
21 is a moran number because 21 divided by the sum of its digits gives a prime number.

Approach: To solve the problem mentioned above we have to find the sum of digits of that number. Then find the quotient by dividing the number by the sum of its digits and check if the quotient is a prime then the given number is a Moran Number.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to check if
// the number is Moran number
import java.util.*;
import java.lang.*;
class GFG{
  
// Function to calculate digit sum
static int digSum(int a)
{
    int sum = 0;
    while (a != 0
    {
        sum += a % 10;
        a = a / 10;
    }
    return sum;
}
  
// Function to check if number is prime
static boolean isPrime(int r)
{
    boolean s = true;
  
    for (int i = 2; i * i <= r; i++) 
    {
        if (r % i == 0
        {
            s = false;
            break;
        }
    }
    return s;
}
  
// Function to check if
// number is moran number
static void moranNo(int n)
{
    int dup = n;
  
    // Calculate digit sum
    int sum = digSum(dup);
  
    // Check if n is completely
    // divisible by digit sum
    if (n % sum == 0
    {
  
        // Calculate the quotient
        int c = n / sum;
  
        // Check if the number is prime
        if (isPrime(c))
        {
            System.out.println("Yes");
            return;
        }
    }
    System.out.println("No");
}
  
// Driver code
public static void main(String[] args)
{
    int n = 21;
  
    moranNo(n);
}
}
  
// This code is contributed by offbeat

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation to check if 
# the number is Moran number 
  
# Function to calculate digit sum 
def digSum(a): 
  
    _sum = 0
  
    while (a): 
        _sum += a % 10
        a = a // 10
  
    return _sum 
  
# Function to check if number is prime 
def isPrime(r): 
  
    s = True
    i = 2
      
    while i * i <= r:
        if (r % i == 0): 
            s = False
            break
        i += 1
      
    return
  
# Function to check if 
# number is moran number 
def moranNo(n): 
  
    dup =
  
    # Calculate digit sum 
    _sum = digSum(dup) 
  
    # Check if n is completely 
    # divisible by digit sum 
    if (n % _sum == 0): 
  
        # Calculate the quotient 
        c = n // _sum 
  
        # Check if the number is prime 
        if (isPrime(c)): 
            print("Yes"
            return
  
    print("No"
  
# Driver code 
n = 21
  
moranNo(n) 
  
# This code is contributed by divyamohan123

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation to check if
// the number is Moran number
using System;
  
class GFG{
  
// Function to calculate digit sum
static int digSum(int a)
{
    int sum = 0;
    while (a != 0) 
    {
        sum += a % 10;
        a = a / 10;
    }
    return sum;
}
  
// Function to check if number is prime
static bool isPrime(int r)
{
    bool s = true;
  
    for(int i = 2; i * i <= r; i++) 
    {
       if (r % i == 0) 
       {
           s = false;
           break;
       }
    }
    return s;
}
  
// Function to check if
// number is moran number
static void moranNo(int n)
{
    int dup = n;
  
    // Calculate digit sum
    int sum = digSum(dup);
  
    // Check if n is completely
    // divisible by digit sum
    if (n % sum == 0) 
    {
  
        // Calculate the quotient
        int c = n / sum;
  
        // Check if the number is prime
        if (isPrime(c))
        {
            Console.Write("Yes");
            return;
        }
    }
    Console.Write("No");
}
  
// Driver code
public static void Main()
{
    int n = 21;
  
    moranNo(n);
}
}
  
// This code is contributed by Code_Mech

chevron_right


Output:

Yes

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.