Magnanimous Number in JavaIn this tutorial, we are going to learn about the Magnanimous number in Java. Magnanimous NumberThe 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 ExampleLet's take the number 359794. Its left and the right part are mentioned in the following table.
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:
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 NumberFollowing 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 ProgramIterative ApproachThe 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 ApproachThe 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 |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India