Computer Science, asked by siidgah, 1 year ago

Plz help me tomorrow is my test.

Java program to check for Smith number. plz


Incredible29: what is smith number
siidgah: Sum of the digits of the number should be equal sum of prime factors of number
siidgah: Btt suppose if prime factors are 3&11 then their sum should be 3+1+1not 3+11

Answers

Answered by varun2395
2
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 1). The first few such numbers are 4, 22, 27, 58, 85, 94, 121 ………………..

Examples:

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

2.   4937775

Prime factors are 3, 5, 5, 65837
Sum of the digits are (4+9+3+7+7+7+5) = 42
Sum of the digits of the factors (3+5+5+(6+5+8+3+7)) = 42

Write a program to input a number and display whether the number is a Smith number or not.

Sample data:

Input             94          Output             SMITH Number

Input             102        Output             NOT SMITH Number

Input             666        Output             SMITH Number

Input             999        Output             NOT SMITH Number

Programming Code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

/**

* The class Smith inputs a number and checks whether it is a Smith Number or not

* @author : www.javaforschool.com

* @Program Type : BlueJ Program - Java

* @Question Year : ISC Practical 2008 Question 1

*/

 

import java.io.*;

class Smith

{

    static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

    //function for finding sum of digits

    int sumDig(int n)

    {

        int s=0;

        while(n>0)

        {

            s=s+n%10;

            n=n/10;

        }

        return s;

    }

     

    //function for generating prime factors and finding their sum

    int sumPrimeFact(int n)

    {

        int i=2, sum=0;

        while(n>1)

        {

            if(n%i==0)

            {

                sum=sum+sumDig(i); //Here 'i' is the prime factor of 'n' and we are finding its sum

                n=n/i;

            }

            else

                i++;

        }

        return sum;

    }

 

public static void main(String args[]) throws IOException

{

    Smith ob=new Smith();

    System.out.print("Enter a Number : ");

    int n=Integer.parseInt(br.readLine());

    int a=ob.sumDig(n);// finding sum of digit

    int b=ob.sumPrimeFact(n); //finding sum of prime factors

     

    System.out.println("Sum of Digit = "+a);

    System.out.println("Sum of Prime Factor = "+b);

     

    if(a==b)

    System.out.print("It is a Smith Number");

    else

    System.out.print("It is Not a Smith Number");

}

}

Output:

1. Enter a Number : 94
Sum of Digit = 13
Sum of Prime Factor = 13
It is a Smith Number

2. Enter a Number : 102
Sum of Digit = 3
Sum of Prime Factor = 13
It is Not a Smith Number

3. Enter a Number : 4937775
Sum of Digit = 42
Sum of Prime Factor = 42
It is a Smith Number

Answered by MacTavish343
4
Hellooo!!


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 1).
The first few such numbers are 4, 22, 27, 58, 85, 94, 121 ……


import java.util.*;
class Smith_Number
{
//function for finding sum of digits
public int sumDig(int n)
{
int s=0,rem;
while(n>0)
{
rem=n%10;
s=s+rem;
n=n/10;
}
return s;
}
//function for generating prime factors and finding their sum
public int sumPrimeFact(int n)
{
int i=2, sum=0;
while(n>1)
{
if(n%i==0)
{
sum=sum+sumDig(i); //Here 'i' is the prime factor of 'n' and we are finding its sum
n=n/i;
}
else
i++;
}
return sum;
}
public static void main(String args[])
{
int n,a,b;
Scanner sc=new Scanner(System.in);
Smith_Number ob=new Smith_Number();
System.out.print("Enter a Number : ");
n=sc.nextInt();
a=ob.sumDig(n);// finding sum of digit
b=ob.sumPrimeFact(n); //finding sum of prime factors
System.out.println("Sum of Digit = "+a);
System.out.println("Sum of Prime Factor = "+b);

if(a==b)
System.out.print("It is a Smith Number");
else
System.out.print("It is Not a Smith Number");
}

siidgah: Thx
MacTavish343: welcome
Similar questions