Vampire Number Program in Java


Posted on 28 December 2016

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 )

Vampire Number Program in java

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.out.println("Enter a 4 digit number:");
        x = s.nextInt();
        dummy = x;
            remainder = dummy%10;
            arr[i] = remainder;
            dummy = dummy/10;
        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]);
                            isvamp = true;
            System.out.println("Vampire number");
            System.out.println("Not a Vampire number");


Vampire Number program in java

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.

Thank you for reading

Tweet your queries and feedback to @PsychoCodes or leave a message on our Facebook page. You can also comment your questions below.

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

If you like this article, then please share it and help us grow.

If You Love this article, You Should Consider:

  • Like us on Facebook
  • Follow us on Instagram
  • Follow us on Twitter
  • Subscribe to our Newsletter.
  • Let us know your suggestions and queries in the comments below.

Thank you for your Love and Support

Share your thoughts