-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMysteryNumber.java
More file actions
52 lines (52 loc) · 1.49 KB
/
MysteryNumber.java
File metadata and controls
52 lines (52 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.util.Scanner;
public class MysteryNumberExample1
{
//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 isMysteryNo(int num)
{
for (int i=1; i <= num/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 == num)
{
//prints a pair of numbers whose sum is the given number
System.out.println( i + " " + j);
System.out.println(num+ " 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 num = sc.nextInt();
//calling the user-defined function to check the number is a mystery or not
isMysteryNo(num);
}
}