2 different Java programs to check for Moran number

Java program to check if a number is a Moran number or not:

In this post, we will discuss how to check if a number is a Moran number or not. We will learn what is a Moran number and how to check if a number is Moran. The Java program will take one number as input from the user, check if the number is Moran or not and print the result.

What is a Moran number:

A number is called a Moran number if we divide the number by the sum of its digits, it returns a prime number. So, we have to calculate the sum of the digits and divide the number by this sum. If the result is a prime number, we can print that it is a Moran number. Else, it is not.

The program will take one number as input from the user and print if it is a Moran number or not.

Algorithm to check if a number is Moran:

We can use the below algorithm to check if a number is a Moran number or not:

• Take the number as input from the user.
• Find the sum of the digits of the number:
• Initialize one variable as 0 to hold the sum of digits of the number.
• Run one loop that will run until the value of the number becomes 0.
• On each iteration of the loop, find the last digit of the number. Add this to the sum variable.
• Remove the last digit from the number before moving to the next iteration.
• Once the loop will end, the sum variable will hold the sum of the digits of the number.
• Divide the number by the sum of digits. Check if it is a prime number or not.
• To check if the number is prime:
• Use a loop from i = 2 to i = number/2.
• On each iteration, check if the number is divisible by i or not.
• If yes, the number will be a prime number as it is divisible by 1 and another number that is not equal to the number itself.
• Else, the number is not a prime number.
• If the result is prime, the given number is a Moran number. Else, it is not a Moran number.

Let’s use the above algorithm to write a program in Java.

Method 1: Java program to check if a number is Moran or not:

The following program checks if a number is a Moran number or not:

``````import java.util.Scanner;

public class Main {
public static void main(String[] args) {
// part 1
int number;
Scanner scanner = new Scanner(System.in);

System.out.println("Enter a number: ");
number = scanner.nextInt();

// part 2
int sumOfDigits = 0, lastDigit, copyNumber = number;

while (copyNumber > 0) {
lastDigit = copyNumber % 10;
sumOfDigits += lastDigit;
copyNumber = copyNumber / 10;
}

int divisionResult = number / sumOfDigits;

// part 3
boolean isPrime = true;
for (int i = 2; i <= divisionResult / 2; i++) {
if (divisionResult % i == 0) {
isPrime = false;
break;
}
}

if (isPrime) {
System.out.println(number + " is a Moran number.");
} else {
System.out.println(number + " is not a Moran number.");
}

}
}``````

I divided the program into three parts. In part 1,

• The integer variable number is used to keep the user input number.
• The Scanner variable scanner is used to read user inputs. It asks the user to enter a number and by using the scanner object, it reads the number.

In part 2,

• It is finding the sum of the digits of the number.
• It creates a copy of the number, copyNumber and with a while loop, it finds the sum of the digits of the number.
• The last digit of the number is found by using %10.
• The last digit is added to the sumOfDigits variable, which is used to store the sum of the digits of the number.
• At the end of each iteration, it removes the last digit from the number by dividing the number by 10.
• It divides the number by the sum of digits of the number and assigns the value to the divisionResult variable.

In part 3,

• It is checking if the result, divisionResult is prime or not.
• It creates one boolean variable isPrime as true.
• It runs one for loop from i = 2 to i = divisionResult/2. On each iteration, it checks if it is divisible by i or not. If yes, it assigns false to the variable isPrime and breaks from the loop.
• If the value of isPrime is true, it prints that the number is a Moran number. Else, it is not a Moran number.

If you run this, it will print the below output:

``````Enter a number:
46
46 is not a Moran number.

Enter a number:
45
45 is a Moran number.``````

Method 2: By breaking the program into different methods:

Let’s break the above program into different methods. We can use separate methods to:

• Calculate the sum of the digits of the number.
• To check if a number is prime or not.
• To check if a number is a Moran number or not.

The method to check for the Moran number will call the other two methods.

``````import java.util.Scanner;

public class Main {
private static int getSum(int number) {
int sumOfDigits = 0, lastDigit, copyNumber = number;

while (copyNumber > 0) {
lastDigit = copyNumber % 10;
sumOfDigits += lastDigit;
copyNumber = copyNumber / 10;
}

return sumOfDigits;
}

private static boolean isPrime(int number) {
for (int i = 2; i <= number / 2; i++) {
if (number % i == 0) {
return false;
}
}
return true;
}

private static boolean isMoran(int number) {
int sumOfDigits = getSum(number);

int divisionResult = number / sumOfDigits;
return isPrime(divisionResult);
}

public static void main(String[] args) {
int number;
Scanner scanner = new Scanner(System.in);

System.out.println("Enter a number: ");
number = scanner.nextInt();

if (isMoran(number)) {
System.out.println(number + " is a Moran number.");
} else {
System.out.println(number + " is not a Moran number.");
}

}
}``````

Here,

• The getSum method is used to find the sum of the digits of a number. It takes one number as its parameter and returns the sum.
• The isPrime method checks if a number is a prime number or not. It takes one number as its parameter and returns one boolean value, true if it is a prime and false otherwise.
• The isMoran method checks if a number is a Moran number or not. It also takes one number as its parameter and returns one boolean value.

It will print similar output.