Magic Number
When the sum of all the given digits of a number and the reverse of that sum is multiplied, which is equal to the original number, and then the number is called a magic number.
For example, suppose we have a 1729 number and we need to validate whether the number is a magic number or not. So, first we need to get the sum of all digits of the number which is 19 (1 + 7 + 2 + 9 = 19). Reverse the sum of the number is 91, and then get the product of the sum of original digits with the reverse of that sum as 19 * 19 = 1729. So, it is the magic number.
For example, suppose we have a 1729 number and we need to validate whether the number is a magic number or not. So, first we need to get the sum of all digits of the number which is 19 (1 + 7 + 2 + 9 = 19). Reverse the sum of the number is 91, and then get the product of the sum of original digits with the reverse of that sum as 19 * 19 = 1729. So, it is the magic number.
Algorithm
- Declare an integer type variable and input a number from the user to store it in N.
- Get the sum of all digits of the original number and store it another variable A.
- Reverse the number of variable A and store into B.
- After that, multiply the values of variable A and B and store into C.
- Check whether C == N, then the number is a Magic Number.
- Else, the Number is not a Magic number.
Code
import java.util.Scanner;
public class MagicNumberExample2 {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter any number to check: ");
//reading an iteger from the user
int number = scanner.nextInt();
if(magicNumber(number))
System.out.println(number +" is a magic number.");
else
System.out.println(number +" is not a magic number.");
}
//user-defined method to check the number is magic or not
public static boolean magicNumber(int number) {
if( ((number - 1) % 9) == 0)
return true;
else
return false;
}
}
Output
Enter any number to check: 73 73 is a magic number.