Q-WAP TO CHECK IF A NUMBER IS MAGIC OR NOT?

import java.io.*;
class mag
{
static boolean mag(int n)
{
int s=0,a=0;
while(n>9)
{
s=0;
while(n>0)
{
a=n%10;
s=s+a;
n=n/10;
}
n=s;
}
if(n==1)
{
return true;
}
else
{
return false;
}
}
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("enter n");
int n=Integer.parseInt(br.readLine());
boolean f=mag(n);
if(f==true)
{
System.out.println("Magic");
}
else
{
System.out.println("Not Magic");
}
}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s