Smith Number

import java.util.*;
class SmithNumber
{
    // Smith number is a Composite Number whose sum of digits is equal
    // to the sum of digits of its prime factors
    // e.g. 666
    public static void main()
    {
        Scanner in=new Scanner(System.in);
        System.out.println("Enter a number");
        int n=in.nextInt();
        int s=0,sd=sum(n);
        if(isPrime(n))
            System.out.println("The Number is Prime");
        else
        {
            int k=2;            
            while(n>1)
            {
                if(isPrime(k) && n%k==0)
                {
                    n=n/k;
                    s=s+sum(k);                    
                }
                else
                    k++;
            }
            if(s==sd)
                System.out.println("Smith Number");
            else
                System.out.println("Not a Smith Number");
        }
    }
    public static boolean isPrime(int n)
    {
        for(int i=2;i<n;i++)
        if(n%i==0)
        return false;
        return true;
    }    
    public static int sum(int n)
    {
        int s=0,i;
        for(i=n;i>0;i/=10)
            s=s+(i%10);            
        return s;
    }
}

Comments
  1. ushika says:

    what is smith number please specify at the top

    • Neel says:

      a composite number whose sum of digits is equal to the sum of digits of its prime factors
      for e.g. 666 is a smith number
      as we see that sum of digits 6 + 6 + 6 = 18
      and the prime factors of 666 = 2,3,3,37
      So, 2+3+3+(3+7) = 18
      Hence 666 is a Smith number

Leave a comment