PROGRAM: 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 such numbers are 4, 22, 58, 85, 94, 121 ………………..
For Example 58=>
5+8=13
2+(2+9) of 29 =13
Since sum of digits=sum of digits of prime factors 58 is a SMITH NUMBER
SYNTAX:
import java.io.*;
class smith_no
{ static int sum,sum1;
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n;int c=0;
System.out.println("Enter a Number");
n=Integer.parseInt(br.readLine());
int n1=n;
while(n1!=0){
int d=n1%10;
sum1+=d;
n1=n1/10;}
for(int i=2;i<n;i++)
{
if(n%i==0)
{
for(int j=2;j<i;j++)
{
if(i%j==0)
c++;
}
if(c==0)
pass(i);
}
}
check();
}
public static void pass(int n){
while(n!=0){
int d=n%10;
n=n/10;
sum+=d;
}
}
public static void check()
{ System.out.println("Sum of Digits: "+sum1);
System.out.println("Sum of Prime Factors: "+sum);
if(sum==sum1)
System.out.println("Yes THE NUMBER IS SMITH NUMBER");
else
System.out.println("NO THE NUMBER IS NOT SMITH NUMBER");
}
}
Comments
Post a Comment