This is a program in java to check whether a number is a smith number or not.

import java.io.*; class smithnumber { static int sumdigit(int x) //to find the sum of digits { int sum=0; if(x!=0) sum+=x%10+sumdigit(x/10); return sum; } static int sumfact(int x) { int i,fact=0; for(i=2;i<=x;i++) { if(prime(i)) { while(x%i==0) { fact+=sumdigit(i); x/=i; } } } return fact; } static boolean prime(int x) { int i,count=0; for(i=2;i<x;i++) { if(x%i==0) count++; } if(count==0) return true; else return false; } public static void main(String args[])throws IOException { int a,sdig,sfact; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("enter a number to check if it is a smith number or not"); a=Integer.parseInt(br.readLine()); sdig=sumdigit(a); sfact=sumfact(a); if(sdig==sfact) System.out.println("The number "+a+" is a Smith number"); else System.out.println("Not a smith number"); } }

OUTPUT:

Tags: Program for smith number in java, java program for smith number.