# Java Program to Check Magic Number

In the previous article, we have discussed Java Program to Check Disarium Number

In this article we are going to understand what Magic number is and how we can check whether a number is Magic or not in Java with examples.

## Program to Check Magic Number

Magic numbers are numbers where the digits are added together and it is done again and again until there is only one digit number remaining. If that number is 1 then this number is a magic number.

Example:

100: 1+0+0=1 is Magic Number
52: 5+2 =7 is not Magic Number
72: 7+2 = 9 is not Magic number

In the above examples the numbers 52 and 72 are not Magic numbers as their digits do not add upto 1. 100 is a magic number however as it adds upto 1.

Let’s see different ways to check magic number.

Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.

Approach :

1. Enter/declare a number, which we store in an integer variable.
2. We then use a while loop to iterate through all the digits in the number.
3. All the digits are added again and again until there is only one digit remaining.
4. If the last digit is equal to 1 then it is a Magic number else not.

### Method-1: Java Program to Check Magic Number By Using Static Value

public class MagicNumber
{
public static void main(String[] args)
{
int n, r = 1, num, sum = 0;
//A number declared;
n = 1000;
num = n;
//Loop that runs until there is one digit remaining
while (num > 9)
{
// Loop to iterate and add the digits
while (num > 0)
{
r = num % 10;
sum = sum + r;
num = num / 10;
}
num = sum;
sum = 0;
}
if (num == 1)
{
System.out.println(n+" is a magic number");
}
else
{
System.out.println(n+" is not a magic number");
}
}
}

Output:

1000 is a magic number

### Method-2: Java Program to Check Magic Number By User Input Value

import java.util.Scanner;

public class MagicNumber
{
public static void main(String[] args)
{
int n, r = 1, num, sum = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter number = ");
n = sc.nextInt();
num = n;
//Loop that runs until there is one digit remaining
while (num > 9)
{
// Loop to iterate and add the digits
while (num > 0)
{
r = num % 10;
sum = sum + r;
num = num / 10;
}
num = sum;
sum = 0;
}
if (num == 1)
{
System.out.println(n+" is a magic number");
}
else
{
System.out.println(n+" is not a magic number");
}
}
}

Output:

Case-1

Enter number = 100
100 is a magic number

Case-2

Enter number = 404
404 is not a magic number

### Method-3: Java Program to Check Magic Number By Using User Defined Method

import java.util.Scanner;

public class MagicNumber
{
public static void main(String[] args)
{
int n, num;
Scanner sc = new Scanner(System.in);
System.out.prinln("Enter number = ");
n = sc.nextInt();
num = n;
//calling the user defined method
//to check Magic number or not.
checkNumber(num,n);
}

//checkNumber() method to check magic number
public static void checkNumber(int num,int n)
{
int r=1;
int sum=0;
//Loop that runs until there is one digit remaining
while (num > 9)
{
// Loop to iterate and add the digits
while (num > 0)
{
r = num % 10;
sum = sum + r;
num = num / 10;
}
num = sum;
sum = 0;
}
if (num == 1)
{
System.out.println(n+" is a magic number");
}
else
{
System.out.println(n+" is not a magic number");
}
}
}
Output:

Enter number = 100
100 is a magic number

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Related Java Programs: