GN-2014 Composite Magic Number
SOLUTION
import java.util.*;
class CompositeMagic
{
Scanner ob = new Scanner(System.in);
int n, m, s, i;
boolean x,y;
public CompositeMagic( ) // Constructor
{
s=0;
}
public void check( ) // Function to check for Composite Magic Number
{
System.out.println("Enter integer value for m ");
m=ob.nextInt(); // Input value for m
System.out.println("Enter integer value for n ");
n=ob.nextInt(); // Input value for n
if(m<n) // check if m is smaller than n
{
System.out.println("COMPOSITE MAGIC INTEGERS ARE:");
for(i=m;i<=n;i++) // generate loop from m to n
{
x=isComposite(i); // calling function to check composite number
y=isMagic(i); // calling function to check magic number
if(x==true && y==true)
{
System.out.print(i + "\t"); // printing composite magic number
s++; // counting frequency of composite magic number
}
}
System.out.println("\n FREQUENCY OF COMPOSITE MAGIC INTEGERS IS:" + s);
}
else System.out.println("INVALID INPUT");
}
boolean isComposite(int p)
{
int j, c=0;
for(j=2;j<p;j++)
if(p%j==0) return true;
return false;
}
boolean isMagic(int p)
{
int a, z;
while(p>9)
{
z=0;
while(p>0)
{
a=p%10;
z=z+a;
p=p/10;
}
p=z;
}
if(p==1) return true;
else return false;
}
public static void main(String arg[])
{
CompositeMagic obj = new CompositeMagic();
obj.check();
}
}