Aryan PrajapatKnowledge Contributor
Write a Java Program to check if any number is a magic number or not. A number is said to be a magic number if after doing the sum of digits in each step and in turn doing the sum of digits of that sum, the ultimate result (when there is only one digit left) is 1.
Write a Java Program to check if any number is a magic number or not. A number is said to be a magic number if after doing the sum of digits in each step and in turn doing the sum of digits of that sum, the ultimate result (when there is only one digit left) is 1.
// Java program to check if
// a number is Magic number.
class GFG
{
public static boolean isMagic(int n)
{
int sum = 0;
// Note that the loop continues
// if n is 0 and sum is non-zero.
// It stops when n becomes 0 and
// sum becomes single digit.
while (n > 0 || sum > 9)
{
if (n == 0)
{
n = sum;
sum = 0;
}
sum += n % 10;
n /= 10;
}
// Return true if sum becomes 1.
return (sum == 1);
}
// Driver code
public static void main(String args[])
{
int n = 1234;
if (isMagic(n))
System.out.println(“Magic Number”);
else
System.out.println(“Not a magic Number”);
}
}
class InvalidAgeException extends Exception
{
public InvalidAgeException (String str)
{
// calling the constructor of parent Exception
super(str);
}
}