Magic number is the if the sum of its digits recursively are calculated till a single digit If the single digit is 1 then the number is a magic number. Magic number is very similar with Happy Number. 2+2+6=10 sum of digits is 10 then again 1+0=1 now we get a single digit number is 1.
import java.util.*;
class magic
{ //class magic opens
public static void main(String[]args)
{ //main method opens
Scanner sc=new Scanner(System.in);
System.out.println("Enter a no");
int n=sc.nextInt();//variable n to accept the no
int r=0,sum=0,k=0;//variable r to extract the digits,variable sum and k to store the sum
sum=n;
while(sum>9)
{
k=sum;
sum=0;
while(k>0)
{
r=k%10;
sum=sum+r;
k=k/10;
}
}
if(sum==1)
System.out.println(n+" is a magic no");
else
System.out.println(n+" is not a magic no");
}//main method closes
}//class closes
No comments:
Post a Comment
any problem in any program comment:-