Write a program to take input of a number and check whether it is a Smith number or not.
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
if(isSmithNumber(num)){
System.out.println(num+" is Smith Number");
}else{
System.out.println(num+" is not Smith Number");
}
}
public static boolean isSmithNumber(int num) {
int sumOfDigits = getSumOfDigits(num);
int sumOfPrimeFactors = getSumOfPrimeFactors(num);
if (sumOfDigits == sumOfPrimeFactors) {
return true;
} else {
return false;
}
}
public static int getSumOfPrimeFactors (int num){
int sum=0;
int numCopy = num;
int divisor=2;
while(divisor<numCopy && divisor>1){
if(num%divisor==0){
sum = sum + getSumOfDigits(divisor);
num=num/divisor;
divisor--;
}
divisor++;
}
return sum;
}
public static int getSumOfDigits (int num){
int sum = 0;
while (num > 0) {
int digit = num % 10;
sum = sum + digit;
num = num / 10;
}
return sum;
}
}
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
if(isSmithNumber(num)){
System.out.println(num+" is Smith Number");
}else{
System.out.println(num+" is not Smith Number");
}
}
public static boolean isSmithNumber(int num) {
int sumOfDigits = getSumOfDigits(num);
int sumOfPrimeFactors = getSumOfPrimeFactors(num);
if (sumOfDigits == sumOfPrimeFactors) {
return true;
} else {
return false;
}
}
public static int getSumOfPrimeFactors (int num){
int sum=0;
int numCopy = num;
int divisor=2;
while(divisor<numCopy && divisor>1){
if(num%divisor==0){
sum = sum + getSumOfDigits(divisor);
num=num/divisor;
divisor--;
}
divisor++;
}
return sum;
}
public static int getSumOfDigits (int num){
int sum = 0;
while (num > 0) {
int digit = num % 10;
sum = sum + digit;
num = num / 10;
}
return sum;
}
}