write a program to check whether number is magic number or not in java with if statement
Answers
Answer:
Please become my follower
Explanation:
import java.util.*;
public class MagicNumberCheck
{
public static void main(String args[])
{
Scanner ob=new Scanner(System.in);
System.out.println("Enter the number to be checked.");
int n=ob.nextInt();
int sum=0,num=n;
while(num>9)
{
sum=num;int s=0;
while(sum!=0)
{
s=s+(sum%10);
sum=sum/10;
}
num=s;
}
if(num==1)
{
System.out.println(n+" is a Magic Number.");
}
else
{
System.out.println(n+" is not a Magic Number.");
}
}
}
Answer:
import java.util.Scanner;
class MagicNum
{
public static void main()
{
Scanner in=new Scanner(System.in);
int n;
System.out.println("Enter a number");
n=in.nextInt();
if(n%9==1)
System.out.println("Magic Number");
else
System.out.println("Not a Magic Number");
}
}
Explanation: