Magic Composite Number in Java
Magic Composite Number
Question : Write a program to accept a number and check whether it is a Magic Composite Number or not.
Magic Composite Number : A Magic Composite Number is a positive integer which is composite as well as a magic number.
First 6 Magic Composite Numbers : 10, 28, 46, 55, 64, 82
Magic Number : A Number is said to be a Magic Number if the sum of its digits are calculated till a single digit is obtained by recursively adding the sum of its digits.
If the single digit comes to be 1 then the number is a Magic Number.
199 = 1 + 9 + 9 = 19 19 = 1 + 9 = 10 10 = 1 + 0 = 1
Composite Number : A composite number is a number that has more than two factors.
Program to check whether a number is Magic Composite Number or not :-
Program :-
import java.io.*;
import java.util.*;
class Magic_Composite
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int n;
System.out.println("Enter Number");
n= in.nextInt();
if(isComposite(n)==true && isMagic(n)==true)
{
System.out.println("It is a Magic Composite Number : "+n);
}
else
{
System.out.println("It is not a Magic Composite Number : "+n);
}
}// end of main method
public static boolean isComposite(int x)
{
int i,c=0;
for(i=1;i<=x;i++)
{
if(x%i==0)
{
c++;
}
}
if(c>2)
{
return true;
}
else
{
return false;
}
}// end of isComposite()
public static boolean isMagic(int y)
{
int a=0,s=0;
while(y>9)
{
s=0;
a=y;
while(a!=0)
{
s=s+(a%10);
a=a/10;
}
y=s;
}
if(y==1)
{
return true;
}
else
{
return false;
}
}// end of isMagic()
}//end of class
For Proper Understanding Watch the Video :-
All the best :)
Keep Learning :)
Comments
Post a Comment