×

Moran Numbers in Java

In this article, we will be acknowledged about the moran numbers in Java, how they are formed, what are the approaches to achieve the moran numbers.

Moran Number

A moran numbers are numbers that produces a prime number when its digits are added together and divided. Keep in mind that the number's factor should equal the sum of its digits.

Let us discuss an example to make it much clear.

Example1:

Consider a number 18.

The digits of 18 are 1 and 8. Their sum would be 1+8=9, and when 18 is divided by 9 that is 

18/9, we get the result as 2. Hence 2 is a prime number that implies 18 is a moran  number.

Example 2:

Now consider another integer 22.

The digits of 22 are 2 and 2 again. Their sum would be 2+2=4, and when divided by 4 that is,

22/4, we get the result as 5.5. Since 5.5 is not a prime number, it can be concluded that 22 cannot be a moran number.

For checking if the numbers is a moran number, there is only one approach and that is the same approach we discussed the above examples.

Approach

  • Calculate the number n's digits added together.
  • Verify whether or not the factor of a number n is represented by the summation of the digits.
  • We can conclude that perhaps the number n cannot be a Moran number if the factor is not the sum of both the digits.
  • If the factor is the addition of the digits, we then determine whether or not the result of dividing the original number by the sum of digits is a prime number.
  • The provided number has become a Moran number if the result of the division is really a prime number; otherwise, it is not.

Let us discuss an example program for verifying if a number is a moran number or not.

File name: Moran.java

public class MoranNum   
{  
  
// a method that computes the sum of the digits of the number  
// num  
public int findSumDigits(int num)  
{  
    int sum = 0;  
      
    while(num != 0)  
    {  
        sum = sum + (num % 10);  
        num = num / 10;  
    }  
      
    return sum;  
}  
  
// a method that checks whether the number num  
// is a prime number or not  
public boolean isPrime(int num)  
{  
    for(int i = 2; i <= (int)Math.sqrt(num); i++)  
    {  
        if((num % i) == 0)  
        {  
            return false;  
        }  
    }  
      
    return true;  
}  
  
// a method that returns true if the number n  
// is a moran number  
public boolean isMoran(int n)   
{  
    int sumDig = findSumDigits(n);  
      
    // if number n is not completely divided by the   
    // sumDig, then n can never be a moran number  
    if((n % sumDig) != 0)  
    {  
        return false;  
    }  
      
    // reaching here means sumDig a factor of  
    // the number n. Now we find the quotient or   
    // or answer of the division  
    int div = n / sumDig;  
      
    // if the answer of the division is a prime  
    // number, then n is a moran number;   
    if(isPrime(div))  
    {  
        return true;  
    }  
  
    // reaching here means div is not a prime number  
    // hence, n is not a moran number  
    return false;  
}  
      
//This is the main method
public static void main(String[] argvs)  
{  
//Defining an object for the class Moran  
MoranNum obj = new MoranNum();  
  
// Considering the 1st output
int num = 45;  
  
boolean ans = obj.isMoran(num);  
  
if(ans)  
{  
    System.out.println("The number " + num + " is a Moran number.");  
}  
else  
{  
    System.out.println("The number " + num + " is not a Moran number.");      
}  
  
System.out.println();  
  
// Considering the 2nd input  
num = 36;  
  
ans = obj.isMoran(num);  
  
if(ans)  
{  
    System.out.println("The number " + num + " is a Moran number.");  
}  
else  
{  
    System.out.println("The number " + num + " is not a Moran number.");      
}  
  
System.out.println();  
  
// Considering the 3rd input 
num = 22;  
  
ans = obj.isMoran(num);  
  
if(ans)  
{  
    System.out.println("The number " + num + " is a Moran number.");  
}  
else  
{  
    System.out.println("The number " + num + " is not a Moran number.");      
}  
  
}  
}

Output

The number 18 is a Moran number.
The number 56 is not a Moran number.
The number 22 is not a Moran number.

Related Topics

Hybrid Inheritance in Java

The most crucial OOPs concept in Java is inheritance, which enables the transfer of a class's properties to another class. It describes the Is-A relationship generally. We can create a...

3 minutes read.

Set Matrix Zeros in Java

In coding round interviews, it is frequently asked as the most significant challenge. an m*n matrix is provided. Set the entire column and row of the matrix to 0 if any...

6 minutes read.

Iccanobif Numbers in Java

In this article, you will be very well equipped with the knowledge of iccanobif numbers in Java. You will also know how they are formed and basic example programs on...

3 minutes read.

Hidden classes in Java

There specifically are some APIs available in the market that generally is harmful to be used in our programs specifically literally, and until JDK 15, there, for all intents and...

4 minutes read.

Print Pencil Shape Pattern in Java

Another pattern made from asterisk symbols that use loops and other logical concepts is the pencil pattern. It is usually requested to draw a pattern using a program. To write the...

6 minutes read.

Bean class in Java

The web applications that are created with the help of the JSP or Java Server Pages generally have fairly more functionality than the web pages that specifically are created with...

5 minutes read.

Finding middle node of a linked list in Java

To find the middle node of a linked list we have various methods in Java. Method 1 In this method two pointers are used, one of which advances quickly, and the other of...

6 minutes read.

URLConnection Class

What is the URL? URL stands for Uniform Resource Locator, is used to specify addresses on the World Wide Web. A URL relates to the identification of any resource connected to the web. URL syntax: Protocol://hostname/other_information(files...

6 minutes read.

House Numbers in Java

In this section, we will discuss about house number in Java. It is a sum of cubes, each of which has a dimension of h + 1. There is a...

3 minutes read.

HashMap Vs HashTable

HashMap HashMap is the basic implementation of the map interface in Java. HashMap stores the data in key and value pairs. Keys are used to access the value of the element. It...

5 minutes read.

Java Set to List

In this article, you will be acknowledged about how the process of conversion from Set or HashSet to LinkedList happens and what are the possible ways involved in conversion process. First...

4 minutes read.

Java String substring() method

Java String substring() method returns a part of the String. Syntax: public String substring(int startIndex) public String substring(int startIndex, int endIndex) Parameters: startIndex : starting index endIndex : ending index Returns: It returns a specified String. Throws: StringIndexOutOfBoundsException if start...

2 minutes read.

Differences between Set and List in Java

Set in Java: The Java. util package contains an interface called the set. The set interface expands the Collection interface. A collection interface is an unordered collection of List where duplicates...

6 minutes read.

How to find characters with the maximum number of times in a string java

Problem statement In this problem, users want to find the maximum count of a character from the string and return the character along with its count. Your task is to create...

3 minutes read.

Difference between String, StringBuffer and StringBuilder in java

What is a string in Java? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

Lombok Java

What is Lombok java? A well-liked and widely-used Java framework that is used to reduce or eliminate boilerplate code is called Project Lombok. Both time and effort are saved. We may...

7 minutes read.

Check if the given array is mirror inverse in Java

The primary objective is to check whether the given array is mirror inverse or not. The values and position of the specified array are switched, and a duplicate array is created....

3 minutes read.

Collections in Java

Collection framework is one of Java's most powerful subsystems. It is a set of classes and interfaces that is all-the-rage technology for superintending objects and a group of objects. In...

6 minutes read.

Java Math sinh() Method

The sinh() method of Java Math class returns the hyperbolic sine of the specified double value. Syntax: public static double sinh(double x) Parameters: The parameter ‘a’ represents the number whose hyperbolic sine is to...

2 minutes read.

Java Integer valueOf() method

The valueOf() method of Java Integer class returns an Integer object holding the specified int value. The second method returns an Integer object holding the specified String value. The third syntax returns...

2 minutes read.