PROGRAM 1:- WRITE A PROGRAM TO ACCEPT A NUMBER AND CHECK WHETHER IT
IS A SMITH NUMBER OR NOT. A SMITH NUMBER IS A NUMBER WHICH IS COMPOSITE,
THE SUM OF WHOSE DIGITS IS SUM OF THE DIGITS OF ITS PRIME FACTORS
OBTAINED AS A RESULT OF PRIME FACTORIZATION(EXCLUDING 1).
Example- 666
Prime factors are 2, 3, 3, and 37
Sum of the digits are (6+6+6) = 18
Sum of the digits of the factors (2+3+3+(3+7)) = 18
import java.util.*;
class smithnumber
{ // class opens
int sumdigit(int n) // method to find sum of the digits
{ // method sumdigit opened
int s=0;
while(n>0)
{
s=s+(n%10); // storing sum of the digits
n=n/10;
}
return s; // sending the sum of the digits
} // method sumdigit closed
int sumprimefact(int n) // method to find the sum of prime factors
{ // method sumprimefact opened
int i=2,sum=0;
while(n>1)
{
if(n%i==0)
{
sum=sum+sumdigit(i); // sum is stored in the sum of digit of the
prime factors
n=n/i;
}
else
i++;
}
return sum;
} // method sumprimefact closed
public static void main(String[]args)
{ // main method opened
Scanner sc=new Scanner(System.in);
smithnumber obj=new smithnumber();
System.out.println("Enter a number: "); // accepting a number
int n=sc.nextInt();
int a=obj.sumdigit(n); // storing sum of digits
int b=obj.sumprimefact(n); // storing sum of prime factors
System.out.println("Sum of the digits: "+a);
System.out.println("Sum of the primefactor: "+b);
if(a==b)
System.out.println(n+" is a Smith Number");
else
System.out.println(n+" is not a Smith Number");
} // main method closed
} // class closed
DOCUMENTATION:-
DATA TYPE VARIABLE NAME PURPOSE
int n Storing a number
int s Storing the sum of digits
int i To modulus with the digit
int sum To store the sum of prime digits
int a To store the sum of the digits
int b To store the sum of prime factors
OUTPUTS:-