A Smith number is a number in which the sum of digits of a number is equal to sum digits of the prime factors(excluding 1) of that number.
import java.util.Scanner;
class Smith {
public static void main(String[] args) {
Smith a = new Smith();
a.input();
}
void input() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Number: ");
int num = sc.nextInt();
if (check(num))
System.out.println("Is a Smith number");
else
System.out.println("Is not a Smith number");
sc.close();
}
boolean check(int num) {
int sm = dig_sum(num); // sum of digits of the number
int pr_sm = 0; // sum of the digits of the prime number
int d = 2;
while (num != 1) {
if (num % d == 0) {
pr_sm += dig_sum(d);
num /= d;
} else
d++;
}
return pr_sm == sm;
}
int dig_sum(int n) {
int r = 0;
while (n != 0) {
r += (n % 10);
n /= 10;
}
return r;
}
}
Name | Type | Uses |
---|---|---|
void main() | ||
a | Smith | Object to call the methods |
void input() | ||
sc | Scanner | Object to take user input |
num | int | to store entered number |
boolean check() | ||
num | int | argument :- number to check if smith |
sm | int | to store sum of digits of 'num' |
pr_sm | int | to store sum of digits of prime factors of 'num' |
d | int | this is the starting index of prime numbers to be evaluated |
int dig_sum() | ||
n | int | argument :- number whose sum of digits is to be found |
r | int | to store sum of digits |
dig | int | to store the extracted digit |