×

Magnanimous Number in java

Magnanimous Number

When the left and right halves of a majestic number are combined, the result is invariably a prime number, which must have at least two digits. The number's left and right sides can have any magnitude, but they can never be zero.

Magnanimous Number Example:

Take 359794 as an example. The following table lists both of its halves, left and right.

Left part

Right part

3

59794

35

9794

359

794

3597

94

35979

4

Currently, the user verifies that the sum of the left and right parts is correct.

3 + 59794 = 59797

35 + 9794 = 9829

359 + 794 = 1153

3597 + 94 = 3691

35979 + 4 = 35983

Here, Five of the following are prime numbers: 35983, 59797, 9829, 1153, and 3691. Consequently, the number 359794 is a Magnanimous number.

Take 200 once more. The left and right halves of the number 100 are:

Left Part

Right Part

2

00

20

0

The left and right sections added together are now:

2 + 00 = 2

20 + 0 = 20

Thus, we have two numbers, one of which is two and the other 20, and neither is a prime number. As a result, at least one Number that is not a prime number was obtained. Thus, 200 is not a particularly impressive figure.

A Magnanimous Number's Finding Process:

The procedures for locating the magnanimous Number are listed below.

  1. Convert a number (let's suppose the Number is num) to a string.
  2.  To get the entire left and right portions of the string, traverse the string. The string must be translated into integers for each left and appropriate component.
  3. When the left and right halves are combined, the sum is created.Find the prime numbers in each generated sum (located in step 4) by checking them.
  4. The integer num is the grand Number if every sum formed is a prime number; otherwise, it is not.

A program based on Magnanimous Number in java:

MagnanimousNumberExample. java

// A Java application that tests for
// magnanimous numbers between 20 and 30 (20 and 30 inclusive)
public class MagnanimousNumberExample 
{ 
// how to determine whether or not it is prime
public boolean isPrime(int no) 
{ 
// dealing with the base case
// In addition to 1 and 0, negative numbers are also not prime.
if (no <= 1) 
{ 
return false; 
} 
// The prime numbers 2 and 3
if (no <= 3) 
{ 
return true; 
} 
// There are no prime numbers if the Number can be divided by 2 and 3.
if (no % 3 == 0 || no % 2 == 0) 
{ 
return false; 
} 
// Until number 4, it has been covered
// Consequently, the user can begin with the number 5.
// Any numeric handling above that is divisible by 2 and 3
// Consequently, the user can able to jump by 6
for(int j = 5; j * j <= no; j = j + 6) 
{ 
if (no % j == 0 || no % (j + 2) == 0) 
{ 
return false; 
} 
} 
return true; 
} 
// A technique to determine whether a number is magnanimous or not
public boolean isMagnanimous(int num) 
{ 
// The Number is changed into a string.
String str = Integer.toString(num); 
// determining the string's length
int len = str.length(); 
// A one-digit number is never capable of being a magnanimous number.
if (len < 2) 
{ 
return false; 
} 
// Finding every left and right portion of the string str using a loop
for(int j = 0; j < len - 1; j++) 
{ 
// left part 
String l = str.substring(0, j + 1); 
// right part 
String r = str.substring(j + 1); 
// the left half is converted to a string.
int intLeft = Integer.valueOf(l); 
// the right half is converted to a string.
int intRight = Integer.valueOf(r); 
// Adding the left and right halves together requires a prime number.
if (!isPrime(intLeft + intRight)) 
{ 
return false; 
} 
} 
return true; 
} 
// Main code 
public static void main(String argvs[]) 
{ 
// constructing an object of the MagnanimousNumberExample class
MagnanimousNumberExample obj = new MagnanimousNumberExample(); 
for(int i = 20; i <= 30; i++) 
{ 
Boolean flag = obj.isMagnanimous(i); 
if(flag) 
{ 
System.out.println(i + " is the magnanimous number."); 
} 
else 
{ 
System.out.println(i + " is not the magnanimous number."); 
} 
} 
} 
} 

Output:

20 is the magnanimous number.
21 is the magnanimous Number.
22 is not the magnanimous Number.
23 is the magnanimous Number.
24 is not the magnanimous Number.
25 is the magnanimous Number.
26 is not an magnanimous Number.
27 is not an magnanimous Number.
28 is not an magnanimous Number.
29 is an magnanimous Number.
30 is an magnanimous Number.

Related Topics

How to add 24 Hours to Date in Java?

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

2 minutes read.

Java Pop

The array, linked list, stack, queue, and other data structures are supported by Java programming. The insertion, deletion, and element searching operations are available for every data structure. And Java...

4 minutes read.

Java program to count the occurrences of each character

The count of each character is the frequency of the characters. For example, the given String is “Programming”. In the given string, the count of the characters ‘P’ -1, ’r’-2,...

6 minutes read.

Array Programs in Java

Array Programs in Java: An array is a data structure that stores similar elements in a contiguous memory location. In Java, an array is an object that stores the same...

7 minutes read.

How to take Multiple String Input in Java using Scanner class

Scanner class is a class which takes the multiple input data or the single input data through an objects and methods. The Scanner class will be available in the java.util...

3 minutes read.

Diamond problem in Java

The Diamond Problem in Java is connected to multiple inheritances. It is also referred to as the "deadly diamond dilemma" or even the "deadly diamond of death”. The solution for...

5 minutes read.

How to compare two dates in different format in Java?

We need to compare two dates frequently when coding. Real-world examples include sorting a list of persons by age or keeping track of students' attendance. To compare two dates, we...

7 minutes read.

if-else Program in Java

if-else Program in Java The if-else program in Java controls which code snippet will execute. The if-else program is very basic and yet very important. Because it checks how well one...

19 minutes read.

How to calculate time complexity of any program in Java

Java : Java's syntax and principles are derived from the C and C++ languages. We know that java is one of the programming language. The main feature of java which is...

3 minutes read.

Java While Loop

A while loop is used to repeatedly execute a set of statements as long as its condition evaluates to true. This loop checks the condition before it starts the execution...

1 minute read.

Maximum length of string in java

In java, String can act as a data type and a class. The string can be defined as the collection of characters that are enclosed with double quotes(“ “). The...

3 minutes read.

Java copy file

There are for the most part 3 methods for duplicating documents utilizing java language. They are as given underneath: Utilizing File StreamUtilizing FileChannel ClassUtilizing Files class. 1. Using File Stream: Here we are...

5 minutes read.

Java Trim

Leading and leaving spaces are removed by the built-in Java String trim() method. Space seems to have the Unicode element of "x0040." Java trim() function looks for all of these...

3 minutes read.

Java Integer compareTo() method

The compareTo() method of Integer class compares two Integer objects numerically. Syntax public static int compareTo(int anotherInteger) Parameters The parameter ‘anotherInteger’ represents the Integer to be compared. Specified by This method is specified by compareTo in...

2 minutes read.

Java Integer getInteger() method

The getInteger() method of Integer class determines the integer value of the system property with the given name. Syntax` public static Integer getInteger(String nm) Parameters The parameter ‘nm’ represents the property name. Throws The getInteger ()...

1 minute read.

How to Install Java on MAC

There are many possible ways to install java on mac. This article is based on the installation of java on mac. The operating system platform is Mac OS X, macOS and...

3 minutes read.

Non-primitive data types in Java

The kind of data stored in the variable is determined by its type. The type describes the data category (different sizes and values). These are not already built into the devices....

4 minutes read.

How to remove special characters from String in Java

Strings in Java are Objects that are supported inside by a burn exhibit. Since exhibits are immutable (cannot develop), Strings are changeless too. A completely new String is made whenever...

5 minutes read.

System Class in Java

The System class provides features including a way to load files and libraries, standard input, standard output, and errors throughput streams, accessibility to outside defined characteristics and environment variables, and...

3 minutes read.

Java Command not found

Java Command not found error is displayed if Java is not installed on the computer or if the command prompt cannot find Java.exe to run the program. Our Java software...

3 minutes read.