VAMPIRE NUMBER
JAVA Program to check if a number is a vampire number or not.And print all combination which
make the number vampire.
Computer generated Output
Coding:
import java.io.*;
class vampire
{
int A[]=new int[100];int x;String s;
void display(String s1,String s2)
{
if(s2.length()<=1)
{
String n=s1+s2;
int a=Integer.parseInt(n.substring(0,((n.length()/2))));
int b=Integer.parseInt(n.substring(n.length()/2));
if(a*b==Integer.parseInt(s))
{A[x++]=a;A[x++]=b;
}
}
else
{ for(int i=0;i<s2.length();i++)
{ String x=s2.substring(i,i+1);
String y=s2.substring(0,i);
String z=s2.substring(i+1);
display(s1+x,y+z);
}
}
}
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("\t INPUT\t\t:\n Enter the number : ");vampire ob=new vampire();
ob.s=br.readLine();
if(ob.s.length()%2!=0)
{ System.out.println("\nINVALID INPUT");
System.exit(0);
}
ob.display("",ob.s);
if(ob.x>0)
{ System.out.println("\nIt is a vampire number.\nThe combinations is /are : ");
for(int i=0;i<=ob.x;i=i+2)
{
if(ob.A[i]>0)
{ for(int j=i+2;j<=ob.x;j=j+2)
{ if(ob.A[i+1]==ob.A[j]||ob.A[i+1]==ob.A[j+1])
{ ob.A[j]=-1;
ob.A[j+1]=-1;
}
}
System.out.println(ob.A[i]+"*"+ob.A[i+1]);
}
}
}
else
System.out.println("\nIt is NOT a vampire number.SORYY!!!!");
}
}
/*
Outputs are:
1>
INPUT :
Enter the number : 1260
It is a vampire number.
The combinations is /are :
21*60
2>
INPUT :
Enter the number : 125460
It is a vampire number.
The combinations is /are :
246*510
204*615
3>
INPUT :
Enter the number : 13078260
It is a vampire number.
The combinations is /are :
1863*7020
1620*8073
2070*6318
4>
INPUT :
Enter the number : 12345
INVALID INPUT
*/
make the number vampire.
Computer generated Output
Coding:
import java.io.*;
class vampire
{
int A[]=new int[100];int x;String s;
void display(String s1,String s2)
{
if(s2.length()<=1)
{
String n=s1+s2;
int a=Integer.parseInt(n.substring(0,((n.length()/2))));
int b=Integer.parseInt(n.substring(n.length()/2));
if(a*b==Integer.parseInt(s))
{A[x++]=a;A[x++]=b;
}
}
else
{ for(int i=0;i<s2.length();i++)
{ String x=s2.substring(i,i+1);
String y=s2.substring(0,i);
String z=s2.substring(i+1);
display(s1+x,y+z);
}
}
}
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("\t INPUT\t\t:\n Enter the number : ");vampire ob=new vampire();
ob.s=br.readLine();
if(ob.s.length()%2!=0)
{ System.out.println("\nINVALID INPUT");
System.exit(0);
}
ob.display("",ob.s);
if(ob.x>0)
{ System.out.println("\nIt is a vampire number.\nThe combinations is /are : ");
for(int i=0;i<=ob.x;i=i+2)
{
if(ob.A[i]>0)
{ for(int j=i+2;j<=ob.x;j=j+2)
{ if(ob.A[i+1]==ob.A[j]||ob.A[i+1]==ob.A[j+1])
{ ob.A[j]=-1;
ob.A[j+1]=-1;
}
}
System.out.println(ob.A[i]+"*"+ob.A[i+1]);
}
}
}
else
System.out.println("\nIt is NOT a vampire number.SORYY!!!!");
}
}
/*
Outputs are:
1>
INPUT :
Enter the number : 1260
It is a vampire number.
The combinations is /are :
21*60
2>
INPUT :
Enter the number : 125460
It is a vampire number.
The combinations is /are :
246*510
204*615
3>
INPUT :
Enter the number : 13078260
It is a vampire number.
The combinations is /are :
1863*7020
1620*8073
2070*6318
4>
INPUT :
Enter the number : 12345
INVALID INPUT
*/
Comments
Post a Comment