AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-programming: java program to check given number is Smith number or not?

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 ).

corejava x 336
programming x 153
Posted On : 2017-09-30 16:24:15.0
profile Divesh - anyforum.in Divesh
8700
up-rate
5
down-rate

Answers


import java.util.Scanner;


public class SmithNumber {
public static void main(String[] args) {
System.out.println("Please Enter the number");
Scanner input=new Scanner(System.in);
System.out.println(isSmithNumber(input.nextInt()));
}

public static boolean isSmithNumber(int number){
return (sumdigit(number)==sumPrimeFactors(number))?true:false;
}

private static boolean isPrime(int num) {
if (num < 2) return false;
if (num == 2) return true;
if (num % 2 == 0) return false;
for (int i = 3; i * i <= num; i += 2)
if (num % i == 0) return false;
return true;
}

public static int sumdigit(int x) //to find the sum of digits
{
int sum=0;
if(x!=0)
sum+=x%10+sumdigit(x/10);
return sum;
}

public static int sumPrimeFactors(int x)
{
int i,fact=0;
for(i=2;i<=x;i++){
if(isPrime(i)){
while(x%i==0){
fact+=sumdigit(i);
x/=i;
}
}
}
return fact;
}
}

Posted On : 2017-10-01 15:11:44
Satisfied : 0 Yes  0 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523185319307
Reply This Thread
up-rate
0
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in