# Magnanimous Number in Java

In this tutorial, we are going to learn about the Magnanimous number in Java.

## Magnanimous Number

The magnanimous number are those numbers that contain at least 2 digits and always generates a prime number when the left part of the number is added with the right part of the number. The left and the right part of the number can be of any size but not zero.

### Magnanimous Number Example

Let's take the number 359794. Its left and the right part are mentioned in the following table.

Left Part Right Part
3 59794
35 9794
359 794
3597 94
35979 4

Now, we check the addition of the left part and the right part.

3 + 59794 = 59797

35 + 9794 = 9829

359 + 794 = 1153

3597 + 94 = 3691

35979 + 4 = 35983

The numbers 59797, 9829, 1153, 3691, and 35983 are prime numbers. Therefore, 359794 is a Magnanimous number.

Let's take another number 200. The left and the right parts of the number 100 are:

Left Part Right Part
2 00
20 0

Now the sum of the left and right parts are:

2 + 00 = 2 and 20 + 0 = 20.

Thus, we got two numbers one is 2, and another is 20, in which 2 is a prime number, and 20 is not a prime number. Thus, we got at least one number, which is not a prime number. Hence, 200 is not a Magnanimous number.

## Steps for Finding a Magnanimous Number

Following are the steps involved in finding the magnanimous number.

Step 1: Take a number (let's say the number is num) and convert it into a string.

Step 2: Do the traversal of the string to get all of the left part and the right part of the string.

Step 3: Convert each and every left and the right part of the string into an integer.

Step 4: Generate the sum of the left and the right part.

Step 5: Check each of the generated sums (found in step 4) for the prime numbers.

Step 6: If each and every generated sum is a prime number, then the number num is the magnanimous number; otherwise, not.

## Magnanimous Number Java Program

### Iterative Approach

The following program uses the iterative approach to check for the Magnanimous number using the steps defined above.

FileName: MagnanimousNumberExamples.java

Output:

```10 is not the magnanimous number.
11 is the magnanimous number.
12 is the magnanimous number.
13 is not the magnanimous number.
14 is the magnanimous number.
15 is not the magnanimous number.
16 is the magnanimous number.
17 is not the magnanimous number.
18 is not the magnanimous number.
19 is not the magnanimous number.
20 is the magnanimous number.
```

### Recursive Approach

The following program uses the recursive approach to check for the Magnanimous number.

FileName: MagnanimousNumberExample1.java

Output:

```10 is not the magnanimous number.
11 is the magnanimous number.
12 is the magnanimous number.
13 is not the magnanimous number.
14 is the magnanimous number.
15 is not the magnanimous number.
16 is the magnanimous number.
17 is not the magnanimous number.
18 is not the magnanimous number.
19 is not the magnanimous number.
20 is the magnanimous number.
```

Next TopicBinary Tree Java

### Feedback   