Write a program to print a vampire number in Java
Answers
Answer:
import java. util. Scanner;
class NaturalNumbers.
{
Public static void main (String args [])
{
Scanner s = new Scanner (System. in);
System. out. println(“Enter the limit : ”);
int n=s. nextInt();
Answer:
Vampire number is a composite natural number with even number of digits, which can be broken down into two numbers of equal length, and the multiplication of those two numbers will be equal to the original number for example
1260(4 digit number )
Can be broken down into (12,60), (21,60), (61,20)..........and so on, the order of the numbers is not decided, one random order is to be chosen. The pair which can give us the original number is known as fangs
In the above case order (21,60) are the fangs because if we multiply 21 and 60 we will get
21 * 60 = 1260(the original number) hence the above number is vampire number.
In this tutorial, we are going to write a Java program to check whether a 4 digit user entered number is vampire number or not.
The main logic behind the program is that we will first break the four-digit number individually and store them in an array after that we will try arranging the array numbers in different orders to obtain every possible order in which the original number can arrange itself.
For example 2345 possible orders → (2345), (2354), (2543), (5243)............and so on there is a huge list of such arrangements.
Now we will use the following formulae to obtain the number
(arr[z] * 10 + arr[j]) * (arr[k] * 10 + arr[l])