Smith Number in JAVA

Smith Number in JAVA

Smith Number : A Smith Number is a composite number, the sum of whose digits is the sum of the digits of its prime factors obtained as a result of Prime Factorization (excluding 1).
 
The first few Smith Numbers are : 4, 22, 27, 58, 85, 94, 121........ Etc.

For Example :-
 
Input                                                : 22
Sum of digits                                 : 2 + 2 = 4
Sum of digits of Prime Factors  : 2 + 2 = 4
Output                                             : It is a Smith Number  
 
Input                                                  : 666
Sum of digits                                   : 6 + 6 + 6 = 18
Sum of digits of Prime Factors     : 2 + 3 + 3 + (3 + 7)  = 18
Output                                               : It is a Smith Number


Program to check & print whether a number is Smith Number or not :-

Program :-

import java.io.*;
import java.util.*;
class Smith
{
    boolean comp(int n) //function to check if the number is composite or not
    {
        int i,c=0;
        for(i=1;i<=n;i++)
        {
            if(n%i==0)
            {
                c++;
            }
        }
        if(c>2)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    int Digit(int n)//function to calculate sum of digit of the number
    {
        int rev,s=0;
        while(n>0)
        {
            rev = n%10;
            s=s+rev;
            n=n/10;
        }
        return s;
    }
    int Primefactor(int n)//function to calculate the sum of prime factorization
    {
        int i=2,sum=0;
        while(n>1)
        {
            if(n%i==0)
            {
                sum=sum+Digit(i);
                n=n/i;
            }
            else
            {
                i++;
            }
        }
        return sum;
    }
    public static void main(String args[])
    {
        Smith obj = new Smith();
        Scanner in = new Scanner(System.in);
        int n,x,y;
        System.out.println("Enter Number");
        n= in.nextInt();
        if(obj.comp(n)==true)
        {
            x= obj.Digit(n); //storing sum of digits of the number
            y= obj.Primefactor(n);//storing sum of prime factors of the number
            if(x==y)
            {
                System.out.println("It is a Smith Number : "+n);
                System.out.println("Sum of Digits : "+x);
                System.out.println("Sum of Prime Factors : "+y);
            }
            else
            {
                System.out.println("It is not a Smith Number : "+n);
                System.out.println("Sum of Digits : "+x);
                System.out.println("Sum of Prime Factors : "+y);
            }
        }
        else
        {
            System.out.println("It is not a Composite Number : "+n);
        }
    }// end of main method
}// end of class
 

For Proper Understanding Watch the Video :-



Watch this video : Smith Number in JAVA

All the best :)
Keep Learning :)



Comments

Popular posts from this blog

Frequency of each digit of a number in Java

Trimorphic Number in JAVA

Tri-Automorphic Number in JAVA