Magic Number.
Program to check whether a given number is Magic number or not.
- 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.
For example 199 is a magic number as 1+9+9=19 but 19 is not a single 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.
SOURCE CODE
import java.io.*;
import java.util.*;
public class Magic
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number :");
int n=sc.nextInt();
int s=n;
while(s>9)
{
n=s;s=0;
while(n>0)
{
int rem=n%10;
s=s+rem;
n=n/10;
}
}
if(s==1)
System.out.println("Yes, it is a magic number");
else
System.out.println("No, it is not a magic number");
}
}
OUTPUT
Click Here to download the Program
If you have any question then leave a comment below I will do my best to answer that question.
Comments
Post a Comment