Saturday, 21 September 2013

SMITH number

import java.io.*;//a java program to check whether the number is a smith number or not
class smith//class name
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n,m,d,e,i,j,c,s=0,p=0,l;//variables required
System.out.println("Enter the number to check whether the number is smith or not : -");
n=Integer.parseInt(br.readLine());
m=n;
while(m>0)//the sum of the digit is found out
{
d=m%10;
s=s+d;
m=m/10;
    }
    for(i=1;i<=n;i++)//the for loop is run to find out the prime factors of the number
    {
        c=0;
        if(n%i==0)
        {
            for(j=1;j<=i;j++)//number of factors is calculated
            {
                if(i%j==0)
                {
                    c++;
                }
            }      
                if(c==2)//if there are two factors ,the number is prime
                {
                    l=i;
                    while(l>0)//the sum of the prime factors is found out
                    {
                        e=l%10;
                        p=p+e;
                        l=l/10;
                    }
                    n=n/i;
                    i=1;
                }    
        }
}
    if(s==p)//If the sum of prime factors is equal to the sum of the digits,the number is smith
    {
     System.out.println("The number is smith number");
    }
    else
    {
        System.out.println("The number is  not smith number");
    }
}
    }