0% found this document useful (0 votes)
3K views

Vampire Number in Java

This Java program checks if a number is a vampire number. It takes a user input number, converts it to a string and separates the digits into two arrays representing the two factors. It then checks if concatenating the two factors returns the original number by comparing the digit arrays. If all digits match, it's a vampire number. Otherwise, it's not a vampire number.

Uploaded by

Aishik Raysharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3K views

Vampire Number in Java

This Java program checks if a number is a vampire number. It takes a user input number, converts it to a string and separates the digits into two arrays representing the two factors. It then checks if concatenating the two factors returns the original number by comparing the digit arrays. If all digits match, it's a vampire number. Otherwise, it's not a vampire number.

Uploaded by

Aishik Raysharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

VAMPIRE NUMBER IN JAVA

import java.util.*;
public class Vampire
{
public static boolean isVamp(int n)
{
String s=Integer.toString(n);
int len=s.length();
int n1[]=new int[len];
int n2[]=new int[len];
int no=1,num=0,nn,c=0;
for(int t=n,i=0;t>0;t/=10)
{
n1[i]=t%10;
i++;
}
for(no=1;no<n;no++)
{
if(n%no==0)
{
num=n/no;

if(Integer.toString(no).length()==(len/2)&&Integer.toString(num
).length()==(len/2))
{
if((no%10)!=0||(num%10)!=0)
{

nn=Integer.parseInt(Integer.toString(no)+Integer.toString(num))
;
for(int j=0,temp=nn;j<len;j++,temp/=10)
{
n2[j]=temp%10;
}
for(int k=0;k<len;k++)
{
c=0;
for(int l=0;l<len;l++)
{
if(n1[k]==n2[l])
c++;
}
if(c==0)
break;
}
if(c!=0)
return true;
}
}
}
}
return false;
}
public static void main(String[]args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number: ");
int no=sc.nextInt();
if(isVamp(no))
System.out.println("Vampire number.");
else
System.out.println("Not Vampire number.");
}
}

You might also like