# Vampire Number Program in Java

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])

Let say we have a number 1361

now arr[] = {1,3,6,1}

We will try to obtain a number using above formula

(a[1] * 10 + a[3]) + (a[2]*10 + a[0]) = (3*10 + 1)*(6*10 + 1) = 31*61(we get a random order)

Now lets see the java code for above problem

``import java.util.*; class Vampire{   public static void main(String args[]){       int x,dummy,remainder,i=0,vampire;       boolean isvamp = false;       int arr[] = new int[4];       Scanner s = new Scanner(System.in);       System.out.println("Enter a 4 digit number:");       x = s.nextInt();       dummy = x;       while(dummy!=0){           remainder = dummy\%10;           arr[i] = remainder;           dummy = dummy/10;           i++;       }       for(int z=0;z<4;z++){           for(int j=0;j<4;j++){               for(int k=0;k<4;k++){                   for(int l=0;l<4;l++){                                              int fang = (arr[z]*10 + arr[j]) * (arr[k]*10 + arr[l]);                       if(fang==x){                           isvamp = true;                           break;                       }                   }               }           }       }       if(isvamp){           System.out.println("Vampire number");       }       else{           System.out.println("Not a Vampire number");       }   } }``

Output:

The above code will check only for four digit vampire numbers.

The above code contains bugs so please give your suggestion or improvements for the above version of the code in the comments down below.

Also, don’t forget to subscribe to our Newsletter.

Preorder and Postorder Traversal of binary tree in Python
02 September 2018

Binary Tree in Python
02 September 2018

Explaining Register variables in C with examples
17 August 2018

Data Autosave System using PHP, MySQL and AJAX
06 July 2018

Понравилась статья? Поделиться с друзьями: