KnowledgeBoat Logo

Computer Science

A Smith number is a composite number, whose sum of the digits is equal to the sum of its prime factors. For example:
4, 22, 27, 58, 85, 94, 121 ………. are Smith numbers.

Write a program in Java to enter a number and check whether it is a Smith number or not.

Sample Input: 666
Sum of the digits: 6 + 6 + 6 = 18
Prime factors are: 2, 3, 3, 37
Sum of the digits of the prime factors: 2 + 3 + 3 + (3 + 7) = 18
Thus, 666 is a Smith Number.

Java

Java Iterative Stmts

54 Likes

Answer

import java.util.Scanner;

public class KboatSmithNumber
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter number: ");
        int n = in.nextInt();

        if (n <= 0) {
            System.out.println(n + " is not a Smith Number.");
            return;
        }

        boolean isComposite = false;
        for (int i = 2; i < n; i++) {
            if (n % i == 0) {
                isComposite = true;
                break;
            }
        }

        if (isComposite && n != 1) {
            int sumDigits = 0;
            int t = n;
            while (t != 0) {
                int d = t % 10;
                sumDigits += d;
                t /= 10;
            }

            int sumPrimeDigits = 0;
            t = n;
            for(int i = 2; i < t; i++) {
                while(t % i == 0) {
                    t /= i;
                    int temp = i;
                    while (temp != 0) {
                        int d = temp % 10;
                        sumPrimeDigits += d;
                        temp /= 10;
                    }
                }
            }
            
            if(t > 2) {
                while (t != 0) {
                    int d = t % 10;
                    sumPrimeDigits += d;
                    t /= 10;
                }
            }

            if (sumPrimeDigits == sumDigits)
                System.out.println(n + " is a Smith Number.");
            else
                System.out.println(n + " is not a Smith Number.");
        }
        else {
            System.out.println(n + " is not a Smith Number.");
        }
    }
}

Output

BlueJ output of A Smith number is a composite number, whose sum of the digits is equal to the sum of its prime factors. For example: 4, 22, 27, 58, 85, 94, 121 ………. are Smith numbers. Write a program in Java to enter a number and check whether it is a Smith number or not. Sample Input: 666 Sum of the digits: 6 + 6 + 6 = 18 Prime factors are: 2, 3, 3, 37 Sum of the digits of the prime factors: 2 + 3 + 3 + (3 + 7) = 18 Thus, 666 is a Smith Number.BlueJ output of A Smith number is a composite number, whose sum of the digits is equal to the sum of its prime factors. For example: 4, 22, 27, 58, 85, 94, 121 ………. are Smith numbers. Write a program in Java to enter a number and check whether it is a Smith number or not. Sample Input: 666 Sum of the digits: 6 + 6 + 6 = 18 Prime factors are: 2, 3, 3, 37 Sum of the digits of the prime factors: 2 + 3 + 3 + (3 + 7) = 18 Thus, 666 is a Smith Number.

Answered By

16 Likes


Related Questions