Write a java program to check whether a given number is a magic number or not
Answers
Answer:
A number is said to be a magic number, if the sum of its digits are calculated till a single digit recursively by adding the sum of the digits after every addition. If the single digit comes out to be 1,then the number is a magic number.
For example-
Number= 50113
=> 5+0+1+1+3=10
=> 1+0=1
This is a Magic Number
For example-
Number= 1234
=> 1+2+3+4=10
=> 1+0=1
This is a Magic Number
plz follow me
Answer:
This program checks if a number is a Magic number in JAVA.
A number is said to be a Magic number if the sum of its digits are calculated till a single digit is obtained by recursively adding the sum of its digits.
If the single digit comes to be 1 then the number is a magic number.
Example- 199 is a magic number as 1+9+9=19 but 19 is not a sigle digit number so 1+9=10 and then 1+0=1 which is a single digit number and also 1.Hence it is a magic number.
Program -
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.");
}
}
}