Smith Number In Java ISC
A Smith number is a composite number, the sum of whose digits is the sum of the digits o its prime factors obtained as a result of prime factorization.
import java.util.*;
class Smith
{
//start of class
public int sumDig(int n)
{
int s1=0;
while(n>0)
{
s1=s1+n%10;
n=n/10;
}
return s1;
}
public int sumPrime(int n)
{
int i=2, s2=0;
while(n>1)
{
if(n%i==0)
{
s2=s2+sumDig(i);
n=n/i;
}
else
i++;
}
return s2;
}
public static void main(){
Scanner sc=new Scanner(System.in);
Smith sd=new Smith();
System.out.print("Enter a Number : ");
int n=sc.nextInt();
int a=sd.sumDig(n);
int b=sd.sumPrime(n);
System.out.println("Sum of Digit = "+a);
System.out.println("Sum of Prime Factor = "+b);
if(a==b)
System.out.print("It is a Smith Number");
else
System.out.print("It is Not a Smith Number");
}
}//end of class
OUTPUT---
Enter a Number : 666
Sum of Digit = 18
Sum of Prime Factor = 18
It is a Smith Number
import java.util.*;
class Smith
{
//start of class
public int sumDig(int n)
{
int s1=0;
while(n>0)
{
s1=s1+n%10;
n=n/10;
}
return s1;
}
public int sumPrime(int n)
{
int i=2, s2=0;
while(n>1)
{
if(n%i==0)
{
s2=s2+sumDig(i);
n=n/i;
}
else
i++;
}
return s2;
}
public static void main(){
Scanner sc=new Scanner(System.in);
Smith sd=new Smith();
System.out.print("Enter a Number : ");
int n=sc.nextInt();
int a=sd.sumDig(n);
int b=sd.sumPrime(n);
System.out.println("Sum of Digit = "+a);
System.out.println("Sum of Prime Factor = "+b);
if(a==b)
System.out.print("It is a Smith Number");
else
System.out.print("It is Not a Smith Number");
}
}//end of class
OUTPUT---
Enter a Number : 666
Sum of Digit = 18
Sum of Prime Factor = 18
It is a Smith Number
Comments
Post a Comment