×

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

How to check valid date in Java?

Every time we get data for any application, we must first ensure that it is accurate before continuing with any further processing. We might have to confirm the following while dealing...

4 minutes read.

CRC Program in Java

The acronym CRC stands for Cyclic Redundancy Check. It is invented by W. Wesley Peterson in 1961. It is an error detection technique that detects errors in digital networks (also...

5 minutes read.

Java vs Dot Net

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

3 minutes read.

Java URL Class with Example

Java URL Uniform Resource Locator To find any resource on the internet, you need to have an address of it. The URL and IP addresses are the pointers used for this purpose....

12 minutes read.

Java Integer toUnsignedLong() method

The toUnsignedLong() method of Java Integer class returns a long value by simply converting the given argument to long after an unsigned conversion. Syntax public static long toUnsignedLong (int  x) Parameters The parameter ‘x’...

1 minute read.

Find the Frequency of Each Element in the Array in Java

We may count the occurrence of each element in the array of items. Maintaining one array to store the counts of each array element is one strategy for solving this issue....

3 minutes read.

URLConnection class in Java

A communication channel between the URL and the program is represented by the Java URLConnection class. It may be utilized to read from and write to the given resource the...

4 minutes read.

Java Boolean toString() Method

The toString() method of Java Boolean class returns a String corresponding to this Boolean object. It returns a string value “true”, if the defined object is true else it returns...

2 minutes read.

XOR of Array Elements Except Itself in Java

A lot of the biggest IT organisations, including The Google, Amazon, TCS, and The Accenture, etc., question about this issue in their interview processes. By addressing the issue, one hopes to assess...

5 minutes read.

Java Open File

Java Desktop class give an open() strategy to open a filr. It has a place with a java.awt package. Work area execution is stage subordinate, so it is important to...

5 minutes read.

How to encrypt password in Java

Every software program needs a username and password to identify a legitimate user. A username can be any number of things, including an email address or a string of characters....

6 minutes read.

Java.net.ConnectionException

java.net.ConnectException: Connection rejected: the interface is the most continuous sort of happening, organizing special cases in Java at whatever point the product is in client-server engineering and attempting to make...

3 minutes read.

How to add 6 Months to the Current Date in Java?

In this tutorial, we will learn how to add 6 months to the local or current date in Java language. We will begin our topic with basic concepts and would...

3 minutes read.

this keyword in Java

The 'this' keyword is an essential notion in Java. We'll go through the 'this' keyword in depth and provide some examples of how it's used in Java. In Java, the term...

5 minutes read.

Quick Sort in Java

Quick Sort in Java Like merge sort, quick sort also uses the divide and conquer approach to sort the given array or list. In quick sort, the sorting of an array...

6 minutes read.

How to Convert long to String in Java

How to Convert long to String in java It is used if we have to display a long number in the text field because everything is displayed as a String. A...

3 minutes read.

Java Exception Propagation

Java Exception Propagation When an exception is being thrown from the peak of the stack and not getting caught, it runs down the stack to the previous method, which is sitting...

3 minutes read.

Lazy Propagation in Segment Tree in Java

The topic of segment trees in Java is continued by the topic of sluggish propagation in segment trees. It is suggested that readers first read through the section tree topic....

4 minutes read.

Generic Linked List in Java

A linear data structure known as a Linked List stores values in nodes. As we already know, each node has two properties: its value and a link to the node...

6 minutes read.

Sparse Numbers in Java

In this section, we will be very well acknowledged about the sparse numbers in Java, how a number can be verified if it is a sparse number or not. Sparse Numbers Any...

3 minutes read.