Mischievous Numbers Program in Java

(Last Updated On: 13/10/2023)

Mischievous Numbers are a fascinating concept in the realm of programming. These numbers possess unique properties that make them intriguing for mathematicians and programmers alike. In this blog, we will delve into the world of Mischievous Numbers, exploring their definition, significance, and applications in programming.

What are Mischievous Numbers in Java?

Mischievous Numbers are positive integers that exhibit a specific behavior when raised to successive powers. A Mischievous Number, n, is defined by the property that for every positive integer, k, the result of raising n to the power of k will always end with the digits of n itself.

For example, let’s consider the number 5. When we raise 5 to the power of 1, we get 5. When we raise 5 to the power of 2, we get 25, and when we raise 5 to the power of 3, we get 125. Notice that the last digit of the result in each case is always 5, which is the same as the original number.

Mischievous Numbers in Java with Example 

Examples of Mischievous Numbers and non-Mischievous Numbers

Mischievous Numbers:

a. 25: 25^2 = 625 (6 + 5 = 11)

b. 121: 121^2 = 14641 (4 + 6 = 10)

c. 3249: 3249^2 = 10556801 (5 + 6 = 11)

d. 6889: 6889^2 = 47458321 (4 + 7 = 11)

Non-Mischievous Numbers:

a. 16: 16^2 = 256 (2 + 5 = 7)

b. 100: 100^2 = 10000 (1 + 0 = 1)

c. 49: 49^2 = 2401 (2 + 4 = 6)

d. 81: 81^2 = 6561 (6 + 5 = 11, but 6 is not in the middle)

Mischievous Numbers demonstrate a curious pattern, where the digit in the middle of the squared number is always equal to the sum of the digits on either side. This property sets them apart from regular numbers and makes them an intriguing topic for exploration in programming and mathematics.

By studying Mischievous Numbers and their properties, we can gain insights into number patterns, algorithms, and problem-solving techniques. Furthermore, implementing programs that identify Mischievous Numbers can serve as an excellent exercise in programming logic and the manipulation of numerical data.

Check out our blog on Bouncy Number in Java here!

Java program that determines whether a given number is a “Mystery Number” or not.

public class MysteryNumEx  
{  
//function that finds reverse of the given number  
static int reverse(int x)  
{  
//converts the given number into string      
String str = Integer.toString(x);  
//stores string  
String string="";  
for(int i=str.length()-1;i>=0;i--)  
{  
//stores the reverse of the string      
string=string+str.charAt(i);  
}  
//converts the string into integer  
int rev=Integer.parseInt(str);  
//returns the reverse number  
return rev;  
}  
//function that checks the number is mystery or not  
static boolean isMysteryNum(int n)  
{  
for (int i=1; i <= n/2; i++)  
{  
//calling the function that reverse a number and assign it to j  
int j = reverse(i);  
//compares the sum of two numbers is equal to given number or not  
if (i + j == n)  
{  
//prints a pair of numbers whose sum is the given number      
System.out.println( i + " " + j);  
System.out.println(n+ " is a mystery number.");  
//returns a boolean value if pair is found  
return true;  
}  
}  
System.out.println("The given number is not a mystery number.");  
//returns false if pair is not found  
return false;  
}  
//driver code  
public static void main(String args[])  
{  
Scanner sc=new Scanner(System.in);  
System.out.print("Enter a number: ");  
//reading an integer from the user  
int n = sc.nextInt();  
//calling the user-defined function to check the number is a mystery or not  
isMysteryNum(n);  
}  
}

Understand the Concept of Krishnamurthy Number in Java , Here!

Explanation of the code:

A Mystery Number is defined as a number that can be expressed as the sum of two of its reverse numbers.

The program starts by defining a reverse() function that takes an integer as input and returns its reverse. This function converts the given number into a string, iterates through the characters in reverse order, and creates a new string in reverse. It then converts the reversed string back to an integer and returns it.

Next, the isMysteryNum() function checks if the given number is a Mystery Number. It iterates from 1 to half of the given number and, for each iteration, finds the reverse of the current number using the reverse() function. It compares the sum of the current number and its reverse with the given number. If a pair is found, it prints the pair and declares the given number as a Mystery Number. If no pair is found, it prints that the given number is not a Mystery Number.

In the main() function, the program prompts the user to enter a number, reads the input using a Scanner, and calls the isMysteryNum() function to determine if it is a Mystery Number or not.

Get a Complete list of Online Certification Courses in India here!

Output:

Enter a number: 458
229 229
458 is a mystery number.

Importance and applications of Mischievous Numbers in programming

Mischievous Numbers have several applications in programming and number theory. They offer unique challenges and opportunities for exploration. Here are a few reasons why Mischievous Numbers are significant:

Mathematical Curiosity: Mischievous Numbers captivate mathematicians and enthusiasts due to their intriguing properties. They provide interesting avenues for research and investigation in number theory.

Algorithmic Development: Understanding Mischievous Numbers requires the development of efficient algorithms to identify and analyze them. Exploring these algorithms helps enhance programming skills and problem-solving abilities.

Cryptography: Mischievous Numbers find applications in certain cryptographic algorithms. In generating secure keys or as components in encryption algorithms, they add an extra layer of complexity and security to systems.

Data Validation: Mischievous Numbers can be utilized in data validation scenarios where integrity checks are required. By applying Mischievous Number tests to specific data sets, programmers can ensure the accuracy and reliability of the information being processed.

Puzzle Solving: Mischievous Numbers often appear in mathematical puzzles and brain teasers. By gaining a deeper understanding of Mischievous Numbers, programmers can enhance their puzzle-solving skills and engage in recreational mathematics.

In this blog, we explored the concept of Mischievous Numbers and their significance in the realm of programming. Mischievous Numbers are a mathematical curiosity that possess unique properties, making them an intriguing subject to study. We defined Mischievous Numbers and examined examples of both Mischievous Numbers and non-Mischievous Numbers during our discussion. By understanding their characteristics, we gained insight into their importance and relevance in programming.

About The Author

Leave a Reply