Back to Tutorials
Solved ProgramsISCClass 11Computer Science

Smith Number: simple & Easy

import java.io.*; class SmithNumber { static int s=0; public static int sumdig(int n) { if(n==0) { return 0; } else { int d=n%10; return(d+sumdig(n/10)); } } static int sum=0; public static in

Trushna Tejwani8 February 2021
Solved Programs

Smith Number the sum of digits is equal to the sum of the digits of all its prime factors.
One example of it is 666.
As 666= 2 * 3 * 3 * 37
Since 6+6+6(sum of digits)=2 + 3 + 3 + (3+7)(sum of factors digits) = 18

Watch java videos

import java.io.*;
class SmithNumber
{
    static int s=0;
    public static int sumdig(int n)
    {
        if(n==0)
        {
            return 0;
        }
        else
        {
            int d=n%10;
            return(d+sumdig(n/10));
        }
    }
    static int sum=0;
    public static int prime_sum(int n,int i)
    {
        if(n>1)
        {
            if(n%i==0)
            {
                sum=sum+sumdig(i);
                return(prime_sum(n/i,i));
            }
            else
            {
                return(prime_sum(n,i+1));
            }
        }
        else
        {
            return sum;
        }
    }

    public static void main()throws IOException
    {
        System.out.println("Enter the number");
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int num=Integer.parseInt(br.readLine()); int res1=sumdig(num);
        int res2=prime_sum(num,2);
        if(res1==res2)
        {
            System.out.println("This number is smith");
        }
        else
        {
            System.out.println("NOT SMITH NUMBER");
        }
    }
}

Output:
Enter the number
666
This number is smith

Download free Sample papers