Friday, 6 January 2017

Write a program to check whether given number is Magic or not?

Question : Write a program to check whether given number is Magic or not?

A Magic number said to be a magic when we add each digit until its reached less than 10 after that  if value is 1 then number is Magic.
Example :
    28 = 2+8
         =10
     10= 1+0
         =1 

Solution : 

import java.util.*;
public class Main
{
  
    public static void main(String[] args)
    {
       int m,n,j;
      Scanner sc=new Scanner(System.in);
      System.out.print("Enter Number");
      m=sc.nextInt();
      while(m>0)
      {
          if(m<10)
          {
              if(m==1)
                System.out.println("Magic Number");
              else
                  System.out.println("Not A Magic Number");
              break;
          }         
          n=m;
          j=0;
          while(n>0)
          {
              j=j+(n%10);
              n=n/10;
          }
          m=j;
      }
    }
}